gpt4 book ai didi

vba - 在多页中动态设置焦点

转载 作者:行者123 更新时间:2023-12-04 21:04:50 35 4
gpt4 key购买 nike

我已经制作了一个所有字段都必须填写的用户表单。此用户表单上有 5 个页面。我需要将重点放在验证为空的字段上。我尝试使用 IsError 语句来做到这一点

将 i 调暗为整数

  For Each ctrl In Controls 'loop through Controls and search for Control with the right name

i = 0
If ctrl.Value = "" Then
MsgBox ctrl.Name, vbExclamation, "Input Data"


While IsError(ctrl.SetFocus)
UserForm1.MultiPage1.Value = i
i = (i + 1) Mod 5
Wend
ctrl.SetFocus
Exit Sub

End If
Next

我也尝试对错误处理程序做同样的事情,但没有成功
Dim i As Integer

For Each ctrl In Controls 'loop through Controls and search for Control with the right name
i=0

If ctrl.Value = "" Then
MsgBox ctrl.Name, vbExclamation, "Input Data"
On Error GoTo ErrHandler:

ctrl.SetFocus
ErrHandler:
UserForm1.MultiPage1.Value = i
i = (i + 1) Mod 5
Resume
Exit Sub
End If
Next

任何帮助将不胜感激

最佳答案

看看下面。这是一种方法。

Sub example()
Dim pageIndx As Integer
Dim firstPage As String
pageIndx = 0 'leave at 0, first page
CurrentPage = "Page1" 'Make sure all pages are named the same,
'with an increasing number at the end(it is like this by default)

For Each ctrl In UserForm1.Controls
On Error Resume Next
Debug.Print ctrl.Name
Debug.Print ctrl.Parent.Name
If (ctrl.Parent.Name Like "Page*" And ctrl.Parent.Name > CurrentPage) Then
pageIndx = pageIndx + 1
End If
If ctrl.Value = "" Then
UserForm1.MultiPage1.Value = pageIndx: UserForm1.Show
MsgBox ctrl.Name, vbExclamation, "Input Data"
ctrl.Value = "Input Data"
Exit Sub
End If
Next ctrl

End Sub
  • 然后在 中使用以下代码每个 textBox _MouseDown sub,用户窗体代码部分(在此示例中,它是 TextBox3)。当用户单击它进行输入时,这将清除文本框。
    If (Me.TextBox3.Value = "Input Data") Then
    Me.TextBox3.Value = ""
    End If
  • 关于vba - 在多页中动态设置焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25971931/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com