Excel VBA Userform 如果焦点改变触发
Excel VBA Userform If Focus Changes Trigger
我在 VBA 中有几个用户表单,我想添加一个功能,当用户第一次单击文本框(将焦点更改到它)时,其中的任何文本都会被选中。当您第一次单击 URL 栏时,我在某些会计应用程序和 Web 浏览器中看到了此功能。它本质上意味着用户可以立即覆盖文本字段。想知道如何在 VBA 中做同样的事情,但我还是个新手。我查看了几个子触发器(不知道正确的术语)但没有看到任何。当您单击文本框时,我找到了一个,但我不希望每次单击该字段时都不断地选择文本。
谢谢
将其放入您的文本框事件中
Dim checked As Boolean
Private Sub TextBox1_Change()
If checked = True Then
TextBox1.SelStart = 0
TextBox1.SelLength = 0
checked = True
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
checked = False
End Sub
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If checked = False Then
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1)
checked = True
End If
End Sub
单击文本框后,它会突出显示文本,如果单击文本,它将取消突出显示并允许您修改文本。如果您在该文本框外单击并在内部单击,将重新突出显示整个文本。
我在 VBA 中有几个用户表单,我想添加一个功能,当用户第一次单击文本框(将焦点更改到它)时,其中的任何文本都会被选中。当您第一次单击 URL 栏时,我在某些会计应用程序和 Web 浏览器中看到了此功能。它本质上意味着用户可以立即覆盖文本字段。想知道如何在 VBA 中做同样的事情,但我还是个新手。我查看了几个子触发器(不知道正确的术语)但没有看到任何。当您单击文本框时,我找到了一个,但我不希望每次单击该字段时都不断地选择文本。 谢谢
将其放入您的文本框事件中
Dim checked As Boolean
Private Sub TextBox1_Change()
If checked = True Then
TextBox1.SelStart = 0
TextBox1.SelLength = 0
checked = True
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
checked = False
End Sub
Private Sub TextBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
If checked = False Then
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1)
checked = True
End If
End Sub
单击文本框后,它会突出显示文本,如果单击文本,它将取消突出显示并允许您修改文本。如果您在该文本框外单击并在内部单击,将重新突出显示整个文本。