Numberformat不会更改vba excel中的单元格值

时间:2022-09-15 15:30:34

I have a range of numbers formatted as text in column A that represent date:

我有一系列数字格式化为A列中的文本,代表日期:

02 10 'which means 02 October 
03 11 'which means 03 November
04 12 'which means 04 December

What I'm trying to do is to parse this string in order to turn it into a proper date format here is my code:

我想要做的是解析这个字符串,以便将其转换为正确的日期格式,这是我的代码:

Dim l As Long
Dim s As String
Dim d As Date
Columns("a").NumberFormat = "0000"
l = Range("A3").Value
s = CStr(l)
d = DateSerial(CInt(2017), CInt(Left(s, 2)), CInt(Right(s, 2)))
Range("A19").Value = d
Range("a19").NumberFormat = "mm/dd/yyyy" 

The problem that I encounter is when a cell value in column A is 2 10 (second of October) my number-format does force it to become 0210 but the value of the cell itself is still 210 so my string parsing doesn't work how can I bypass this problem?

我遇到的问题是当A列中的单元格值是2 10(10月的第二个)时,我的数字格式会强制它变为0210但是单元格本身的值仍然是210因此我的字符串解析不起作用我可以绕过这个问题吗?

1 个解决方案

#1


3  

As mentioned in comments, Split on the cell's .Text property.

正如评论中所提到的,拆分单元格的.Text属性。

Range("A19").Value = dateserial(2017, split(Range("A1").Text, chr(32))(1), split(Range("A1").Text, chr(32))(0))
Range("A19").NumberFormat = "mm/dd/yyyy" 

#1


3  

As mentioned in comments, Split on the cell's .Text property.

正如评论中所提到的,拆分单元格的.Text属性。

Range("A19").Value = dateserial(2017, split(Range("A1").Text, chr(32))(1), split(Range("A1").Text, chr(32))(0))
Range("A19").NumberFormat = "mm/dd/yyyy"