vba 将日期加载到用户窗体文本框后更改日期格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22071452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change date's format after loading it to UserForm textbox
提问by lowak
I have a userform with textbox. When textbox is initialized it's getting filled with actual date. What I want to do is to fill it with custom date format = DD-MM-YYYY
我有一个带文本框的用户表单。当文本框被初始化时,它会被实际日期填满。我想要做的是用自定义日期格式填充它=DD-MM-YYYY
I wrote code below and something is wrong about it but I have no idea what is wrong. Code has msgbox before inserting value, MsgBox
shows date in a custom format but when it is passed to textbox.value
it's like M/DD/YYY
.
我在下面写了代码,但我不知道哪里出了问题。代码在插入值之前有 msgbox MsgBox
,以自定义格式显示日期,但是当它传递给textbox.value
它时就像M/DD/YYY
.
Dim year As Long, year_control As Date
year = Format(Date, "yyyy")
year_control = Format(Date, "dd-mm-yyyy")
MsgBox (year_control)
textbox.Value = year_control
(...)
(……)
If year_control < "01-04-" & year Then
Me.Controls("rok1").Value = True
Else
Me.Controls("rok2").Value = True
End If
采纳答案by user2140261
You cannot "Format" a date variable:
您不能“格式化”日期变量:
year_control As Date
year_control = Format(Date, "dd-mm-yyyy")
The above code does nothing because a Date variable is simply holing a date more specifically VBA stores Date variables as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59.
上面的代码什么都不做,因为 Date 变量只是简单地保留一个日期 VBA 将 Date 变量存储为 IEEE 64 位(8 字节)浮点数,表示从 100 年 1 月 1 日到 9999 年 12 月 31 日的日期和从 0 的时间:00:00 到 23:59:59。
No matter what you do to this variable it will always display dates according to the short date format recognized by your computer. Times display according to the time format (either 12-hour or 24-hour) recognized by your computer.
无论您对此变量做什么,它始终会根据您的计算机识别的短日期格式显示日期。时间根据计算机识别的时间格式(12 小时制或 24 小时制)显示。
So while you can change the internal value that is held by the Date
Variable you cannot store its format inside of the same vairable.
因此,虽然您可以更改Date
变量保存的内部值,但您无法将其格式存储在同一变量中。
You can however display it however you would like inside of a string variable. So, if you used:
但是,您可以在字符串变量中以任何方式显示它。所以,如果你使用:
Dim year As Long, year_control As Date
Dim strYear_control As string
year = Format(Date, "yyyy")
year_control = Format(Date, "dd-mm-yyyy")
strYear_control = Format(year_control , "dd-mm-yyyy")
MsgBox (strYear_control)
textbox.Value = strYear_control
It should work as you are expecting. As the Format()
function will return a Variant (String) containing an expression formatted according to instructions contained in a format expression.
它应该按您的预期工作。由于该Format()
函数将返回一个 Variant (String),其中包含根据格式表达式中包含的指令格式化的表达式。
As a side note you may also wish to use
作为旁注,您可能还希望使用
Format$(year_control , "dd-mm-yyyy")
as it will be much faster, You also can use FormatDateTime
to format your date in other various ways.
因为它会快得多,您还可以使用FormatDateTime
其他各种方式来格式化您的日期。