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

标签 vba excel number-formatting string-parsing

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

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

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

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" 

我遇到的问题是,当 A 列中的单元格值为 2 10(10 月 2 日)时,我的数字格式确实强制它变为 0210,但单元格本身的值仍然是 210,所以我的字符串解析不会'不工作我怎样才能绕过这个问题?

最佳答案

正如评论中提到的,Split在细胞的.Text property .

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" 

关于vba - Numberformat 不会更改 vba excel 中的单元格值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48938842/

相关文章:

c# - ASP.NET 应用程序的 Excel 插件

sql - 将数据从访问导入到打开的 Excel 电子表格? (Excel VBA)

c++ - %05d 与 std::stringstream 等价于负数?

variables - 通过变量引用工作簿和工作表

vba - Excel 公式或 VBA : Find match's address in separate table with 2 column criteria - No helper columns

Excel VBA : How to write column name with commercial at "@" in it

flutter - Dart/Flutter 中 Intl 包的 NumberFormat 的反直觉结果

json - JSON 是否允许数字的正号?

VBA - (Excel) 为什么我不能在不出现类型不匹配错误的情况下为用户窗体设置文本框类型?

excel - 将使用的范围复制到文本文件