将 2 个文本框组合成另一个文本框
Combine 2 Textboxes into another Textbox
所以我还是新手,但我正在尝试根据其他两个文本框自动填充第三个文本框。我有一个文本框询问出生日期 (mm/dd/yyyy),第二个文本框询问他们的姓氏。所以第三个文本框会自动生成一个组合值。我知道我的基本等式是
=Left([Last Name],3)+Right([DOB],7)
示例:生日 - 10/01/1978
姓氏 - 史密斯
结果 - Smi01/1978
任何帮助都会很棒
这是您可以粘贴到用户表单的代码 sheet 中的代码。
Option Explicit
Private Sub TextBox1_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox1_AfterUpdate()
' runs when the focus leaves TextBox1
SetTbx3 True
End Sub
Private Sub TextBox2_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox2_AfterUpdate()
' runs when the focus leaves TextBox2
SetTbx3 True
End Sub
Private Sub SetTbx3(ByVal Complete As Boolean)
If IsDate(TextBox2.Value) And (Complete = True) Then
TextBox3.Value = UCase(Left(TextBox1.Value, 3)) & Format(CDate(TextBox2.Value), "mm/yyyy")
Else
TextBox3.Value = Left(TextBox1.Value, 3) & Right(TextBox2.Value, 7)
End If
End Sub
有3个文本框,TextBox1(名称),TextBox2(DOB)和TextBox3(文件名)。
TextBox1和TextBox2分别有两个事件过程,Change
和AfterUpdate
。当您在文本框中键入一个字符时,会发生 Change 事件。当您在编辑后将光标移出文本框时,将发生 Update 事件。这仅用于演示。你可能不需要两者。
所有这些事件都调用 Sub SetTbx3
,它需要一个参数,"Complete",如果为真,表示调用来自 AfterUpdate事件。 SetTbx3
将根据参数是否完整为 TextBox3 生成不同的字符串,但如果输入的日期未被识别为日期,仍会根据输入的字符串而不是输入的 DOB 日期提供字符串.
所以我还是新手,但我正在尝试根据其他两个文本框自动填充第三个文本框。我有一个文本框询问出生日期 (mm/dd/yyyy),第二个文本框询问他们的姓氏。所以第三个文本框会自动生成一个组合值。我知道我的基本等式是
=Left([Last Name],3)+Right([DOB],7)
示例:生日 - 10/01/1978 姓氏 - 史密斯 结果 - Smi01/1978
任何帮助都会很棒
这是您可以粘贴到用户表单的代码 sheet 中的代码。
Option Explicit
Private Sub TextBox1_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox1_AfterUpdate()
' runs when the focus leaves TextBox1
SetTbx3 True
End Sub
Private Sub TextBox2_Change()
' runs after every character typed
SetTbx3 False
End Sub
Private Sub TextBox2_AfterUpdate()
' runs when the focus leaves TextBox2
SetTbx3 True
End Sub
Private Sub SetTbx3(ByVal Complete As Boolean)
If IsDate(TextBox2.Value) And (Complete = True) Then
TextBox3.Value = UCase(Left(TextBox1.Value, 3)) & Format(CDate(TextBox2.Value), "mm/yyyy")
Else
TextBox3.Value = Left(TextBox1.Value, 3) & Right(TextBox2.Value, 7)
End If
End Sub
有3个文本框,TextBox1(名称),TextBox2(DOB)和TextBox3(文件名)。
TextBox1和TextBox2分别有两个事件过程,Change
和AfterUpdate
。当您在文本框中键入一个字符时,会发生 Change 事件。当您在编辑后将光标移出文本框时,将发生 Update 事件。这仅用于演示。你可能不需要两者。
所有这些事件都调用 Sub SetTbx3
,它需要一个参数,"Complete",如果为真,表示调用来自 AfterUpdate事件。 SetTbx3
将根据参数是否完整为 TextBox3 生成不同的字符串,但如果输入的日期未被识别为日期,仍会根据输入的字符串而不是输入的 DOB 日期提供字符串.