VBA Excel 在文本框中提供当前日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5875418/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:26:20  来源:igfitidea点击:

VBA Excel Provide current Date in Text box

excelvbadatetextbox

提问by Jesse Smothermon

I have a dialog box that appears when the user clicks a macro button. This dialog box is mostly filled out (date, email, producer, website, etc are filled) and all the user needs to do is enter their name. The problem is that the date entered is static, so if I entered "3/3/11" it'll stay that way until someone changes it.

我有一个对话框,当用户单击宏按钮时会出现该对话框。这个对话框主要是填写(日期、电子邮件、生产者、网站等),用户需要做的就是输入他们的名字。问题是输入的日期是静态的,所以如果我输入“3/3/11”,它会保持这种状态,直到有人更改它。

I was wondering if there was some way for that text box to always display the current date (unless the user decides to change it for whatever reason). I've tried putting different things into the "Value" section of the text box (such as "getDate()" and "= Date()") but so far haven't been successful.

我想知道是否有某种方法可以让该文本框始终显示当前日期(除非用户出于任何原因决定更改它)。我尝试将不同的内容放入文本框的“值”部分(例如“getDate()”和“= Date()”),但到目前为止还没有成功。

Thank you,

谢谢,

Jesse Smothermon

杰西·斯莫瑟蒙

回答by Graham

Use the form Initialize event, e.g.:

使用表单初始化事件,例如:

Private Sub UserForm_Initialize()
    TextBox1.Value = Format(Date, "mm/dd/yyyy")
End Sub

回答by Lance Roberts

The easy way to do this is to put the Date function you want to use in a Cell, and link to that cell from the textbox with the LinkedCell property.

执行此操作的简单方法是将要使用的日期函数放在单元格中,然后使用 LinkedCell 属性从文本框中链接到该单元格。

From VBA you might try using:

从 VBA,您可以尝试使用:

textbox.Value = Format(Date(),"mm/dd/yy")

回答by Tim Williams

Set the value from code on showing the form, not in the design-timeProperties for the text box.

在显示表单的代码中设置值,而不是在文本框的 design-timeProperties 中设置值。

Private Sub UserForm_Activate()
    Me.txtDate.Value = Format(Date, "mm/dd/yy")
End Sub

回答by Todd Coffman

Here's a more simple version. In the cell you want the date to show up just type

这是一个更简单的版本。在您希望日期显示的单元格中,只需键入

=Today()      

Format the cell to the date format you want and Bob's your uncle. :)

将单元格格式化为您想要的日期格式,Bob 是您的叔叔。:)

回答by George Gru

Actually, it is less complicated than it seems.

实际上,它没有看起来那么复杂。

Sub 
  today_1()
  ActiveCell.FormulaR1C1 = "=TODAY()"
  ActiveCell.Value = Date
End Sub

回答by Casey M

I know this is extremely old post, but I've used this before

我知道这是非常老的帖子,但我以前用过这个

You can always adjust to activate. Now method is another way. Just an option

您可以随时调整以激活。现在方法是另一种方法。只是一个选择

Private Sub UserForm_Initialize()

Textbox1.Text = Format(Now(), "mmddyyyhhmmss")

End Sub

回答by Arnoldiusss

You were close. Add this code in the UserForm_Initialize()event handler:

你很接近。在UserForm_Initialize()事件处理程序中添加以下代码:

tbxDate.Value = Date