[英]VBA - error in .NumberFormat for date
My problem is that the format of my column DATE
is Mar 16 2016 3:19PM
. 我的问题是,我的
DATE
列的格式为Mar 16 2016 3:19PM
。 The format that I need for my code to run is 3/16/2016
and now I'm try to convert it. 运行我的代码所需的格式是
3/16/2016
,现在我尝试将其转换。 This is the error I get with using my code: 这是我使用代码时遇到的错误:
Run-time error '13' Type mismatch
运行时错误“ 13”类型不匹配
this is the line that i got the error 这是我收到错误的行
data = Intersect(Sheet2.Columns("O"), Sheet2.UsedRange).NumberFormat = "mm/dd/yyyy"
Here's my code: 这是我的代码:
Public Sub Selection()
Dim file2 As Excel.Workbook
Dim Sheet2 As Worksheet, data(), i&
Set Sheet2 = Workbooks.Open(TextBox2.Text).Sheets(1)
' load the data from column O
data = Intersect(Sheet2.Columns("O"), Sheet2.UsedRange).NumberFormat = "mm/dd/yyyy"
'set the title
data(1, 1) = "Month"
' extract the month
For i = 2 To UBound(data)
If VarType(data(i, 1)) = vbDate Then
data(i, 1) = Month(data(i, 1))
End If
Next
' write the data back to the sheet
Sheet2.UsedRange.Columns(Sheet2.UsedRange.Columns.Count + 1) = data
End Sub
I've tried using: 我试过使用:
outputWksht.Columns(24).NumberFormat = "mm/dd/yyyy"
and 和
Cells(1, 1).Value = Format(StartDate, "dd/mm/yyyy")
There are no errors but its not working, I wonder why?? 没有错误,但是没有用,我想知道为什么吗?
你可以试试这个吗?
Cells(1, 5).NumberFormat = "dd/mm/yyyy"
Try 尝试
Dim data As Range
Set data = Intersect(Sheet2.Columns("O"), Sheet2.UsedRange)
data.NumberFormat = "mm/dd/yyyy"
Try changing this: 尝试更改此:
For i = 2 To UBound(data)
If VarType(data(i, 1)) = vbDate Then
data(i, 1) = Month(data(i, 1))
End If
Next
to this: 对此:
For i = 2 To UBound(data)
data(i, 1) = Month(CDate(data(i, 1)))
Next i
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.