简体   繁体   English

在列表视图中更改日期格式EXCEL VBA

[英]Change date format in list view EXCEL VBA

I have a listview but when macro is launched, it changes date format from "[h]:mm:ss" to General. 我有一个listview,但是启动宏时,它将日期格式从“ [h]:mm:ss”更改为“常规”。

My code says this 我的代码说

    itmX.SubItems(4) = Range("F" & i).Value
    itmX.SubItems(5) = Range("G" & i).Value
    itmX.SubItems(6) = Range("H" & i).Value
    itmX.SubItems(7) = Range("I" & i).Value

Is there any way to fix it? 有什么办法可以解决? In order to get format "[h]:mm:ss"? 为了获得格式“ [h]:mm:ss”? Thank you very much 非常感谢你

Edit 编辑

try after you've replaced the .value with .text , it should look like this: 在将.value with .text替换为.value with .text之后,请尝试如下所示:

timX.SubItems(4) = Range("F" &  i).Text

times and dates are just masks for a number in excel today's date or a time value are just numbers with a format mask like below 时间和日期只是excel中今天日期的数字的掩码,或者时间值只是具有以下格式掩码的数字

26/01/16 = 42395
1:02:03 = 0,0430902777777778

With the value you're setting your subitems to the underlying value of the cell 使用该value您可以将子项设置为单元格的基础值

End of Edit 编辑结束


You could use the numberformat property 您可以使用numberformat属性

 ThisWorkbook.Worksheets("Name of your worksheet").Range("A1").NumberFormat = "[H]:mm:ss" 

maybe you can apply this 也许你可以应用这个

myTimeString.Text = Format(Now, "MM/dd/yyyy h:mm:ss tt")

ie output 即输出

myTimeString will display 03/24/2014 12:49:23 PM myTimeString将显示2014/03/24下午12:49:23

Check this 检查一下

Regards Claudio 问候克劳迪奥

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM