简体   繁体   中英

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.

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"? Thank you very much

Edit

try after you've replaced the .value with .text , it should look like this:

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

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

With the value you're setting your subitems to the underlying value of the cell

End of Edit


You could use the numberformat property

 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

Check this

Regards Claudio

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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