在多页中动态设置焦点

时间:2014-09-22 10:30:38

标签: excel vba excel-vba

我创建了一个必须包含所有字段的用户表单。此用户表单上有5个页面。我需要将重点放在对空白字段的验证上。我尝试使用IsError语句

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"


                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

任何帮助将不胜感激

3 个答案:

答案 0 :(得分:0)

看看下面的内容。这是一种做法。

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子中使用以下代码,即userform代码部分(在本例中,它是TextBox3)。这将在用户单击它以键入时清除textBox。

    If (Me.TextBox3.Value = "Input Data") Then
        Me.TextBox3.Value = ""
    End If
    

答案 1 :(得分:0)

这是一种方式 - 它假定任何列表框/组合框都需要从列表中进行选择:

Private Sub CommandButton1_Click()
    Dim ctl                   As MSForms.Control
    Dim pg                    As MSForms.Page
    Dim bFoundOne             As Boolean

    For Each pg In Me.MultiPage1.Pages
        For Each ctl In pg.Controls
            Select Case TypeName(ctl)
                Case "TextBox"
                    If ctl.Value = vbNullString Then
                        bFoundOne = True
                        FlagInvalid pg.Index, ctl
                        Exit For
                    End If
                Case "ListBox", "ComboBox"
                    If ctl.ListIndex = -1 Then
                        FlagInvalid pg.Index, ctl
                        bFoundOne = True
                        Exit For
                    End If

            End Select
        Next ctl
        If bFoundOne Then Exit For
    Next pg
End Sub
Sub FlagInvalid(lngIndex As Long, ctl As MSForms.Control)
    MsgBox "Please fill out ALL controls"
    Me.MultiPage1.Value = lngIndex
    ctl.SetFocus
End Sub

答案 2 :(得分:0)

You can only SetFocus to an Enabled control that is on the currently selected MultiPage. If you check MultiPage.Value before attempting to SetFocus, you can choose to set the focus only when the relevant MultiPage page is in focus, or you could change MultiPage.Value to the page on which the control is, then use SetFocus. You may also have to SetFocus first to any frame in which the control lives.

You might also want to consider getting the name of the first control which fails validation whilst you're performing the validation and then simply using

Controls(<nameofcontrol>).SetFocus

Here's some example code - I use a Public Enum for the MultiPage values and mvarlMeasuresControlSets is a counter for Measures Control Sets, which are created dynamically every time the User needs to enter a new Measure:

' Ensure that fraMeasures has focus and then SetFocus to txtName:
If MultiPage.value = VMT_DASHBOARD_PAGE_MEASURES Then
  If Controls("txtMeasureName" & mvarlMeasuresControlSets).Enabled Then
    Controls("fraMeasures").SetFocus
    Controls("txtMeasureName" & mvarlMeasuresControlSets).SetFocus
  End If
End If