[英]Excel 2010 Userform: Date entered in Textbox 3 will automatically record day of the week in Textbox 7 (i.e. Mon)
我有一个用户窗体,我需要用户在文本框3中输入日期,然后在文本框7中指定实际的星期几(缩写为ddd)。Textbox3中的示例12/7/2014将在其中显示“ Sun”文字框7。
有人能帮忙吗?
为此,您将需要一些不同的组件。 另外,请注意,为了将来的读者,我在此示例中使用了TextBox1和TextBox2。 请在适用的地方应用您自己的文本框名称。
输入值后,您将需要一个事件处理程序来运行代码。 “_AfterUpdate”
确保文本框中包含文本。 理想情况下,您还要确保它也是一种日期格式。 为了这个例子,我只是检查一下Textbox是否为空。
将他们输入的文本转换为日期。 CDATE(文本)
格式化为“ ddd”
将值返回到TextBox2
测试:
Private Sub TextBox1_AfterUpdate()
Dim tDate As Date
If TextBox1.Text <> "" Then
tDate = CDate(TextBox1.Text)
TextBox2.Text = Format(tDate, "ddd")
End If
End Sub
总结列出的功能:
TextBox1_AfterUpdate 'Handles the event that when the user is done updating, code runs. As
'opposed to _Change, where it runs as soon as you type at all.
CDate(text)
Format(tdate, "ddd")
其他一些选择:
Weekday(date, firstDayOfWeek (Optional)) ' There are other options for this as well
WorksheetFunction.Text(text, format) 'This worksheet function has many uses. See link.
链接:
编辑:添加链接-更新答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.