简体   繁体   English

更改 VBA 中的日期格式

[英]Change the Format of the Date in VBA

I think this is a basic question but I do not find a way to solve my problem我认为这是一个基本问题,但我找不到解决问题的方法

I created this code:我创建了这段代码:

Sub test()

Range("A11") = CDate(Evaluate("WORKDAY(TODAY(),-1)"))
Range("A11").NumberFormat = "yyyymmdd"
MsgBox Range("A11")


End Sub

But my problem is that the MsgBox do not take into account the NumberFormat "yyyymmdd" , it shows "3/5/2021" instead of "20210305" .但我的问题是 MsgBox 没有考虑 NumberFormat "yyyymmdd" ,它显示"3/5/2021"而不是"20210305" It there a way to change the code so that when I write MsgBox Range("A11") it shows directly "20210305" ?有没有办法更改代码,以便在我编写MsgBox Range("A11")时直接显示"20210305"

Thank you for your help.谢谢您的帮助。

If you click the cell, you will most likely see 3/5/2021 as the value of the cell.如果单击单元格,您很可能会看到3/5/2021作为单元格的值。 Because it hasn't really changed, just the cell formatting.因为它并没有真正改变,只是单元格格式。

MsgBox Range("A11") is the same as MsgBox Range("A11").Value , which will give you the value of the cell, without the formatting. MsgBox Range("A11")MsgBox Range("A11").Value相同,它将为您提供单元格的值,而无需格式化。
Try using MsgBox Range("A11").Text instead.尝试使用MsgBox Range("A11").Text代替。

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

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