0

我创建了一个带有多页的用户表单。在其中一个多页上,我结合了TextBoxesComboBoxes。总共有9 个盒子彼此相邻。它们都是源自其背后的 excel 表的控件。当用户在第一个框中输入数据时,它会动态创建另外 9 个对应于下一行的框。这个过程重复。现在,第9 个框是一个TextBox,其可见性设置为false。第8 个框是ComboBox。如果第8 个框中的值为“Existing” ,那么第 9 个框的可见性框是True

这就是我动态创建附加文本框/组合框的方式:

Private Sub Worksheet_Change(ByVal Target As Range)
    Select Case Left(Target.Address, 2)
        Case Is = "$A"
        Dim cCntrl As Control
        Dim i As Integer
        For i = 0 To 8
        //skipping to case 7 and 8
        Case Is = 7
            Set cCntrl = UserForm1.Frame13.Controls.Add("Forms.ComboBox.1", "Login" & i & Target.Row + 1, True)
            With cCntrl
                .Width = UserForm1.ComboBox240.Width
                .Height = UserForm1.ComboBox240.Height
                .Top = (10 + UserForm1.ComboBox240.Height) * (Target.Row - 1)
                .Left = UserForm1.ComboBox240.Left
                .ControlSource = "'Multi Branch'!" & Chr(65 + i) & Target.Row + 1
                .List = Worksheets("Setup").Range("BILLING").Value
                .Style = fmStyleDropDownList
            End With
            Case Is = 8
                Set cCntrl = UserForm1.Frame13.Controls.Add("Forms.TextBox.1", "Login" & i & Target.Row + 1, True)
            With cCntrl
                .Width = UserForm1.TextBox287.Width
                .Height = UserForm1.TextBox287.Height
                .Top = (10 + UserForm1.TextBox287.Height) * (Target.Row - 1)
                .Left = UserForm1.TextBox287.Left
                .ControlSource = "'Multi Branch'!" & Chr(65 + i) & Target.Row + 1
                .Name = "BTwo" & (Target.Row - 1)
                .Visible = False
            End With
        End Select
    Next

以下是我目前更改可见性的方式:

        Case Is = "$H"
        If Target.Value = "Existing" Then
            UserForm1.Controls("BTwo" & Target.Row - 2).Visible = True
        Else
        If Target.Row = 2 Then Exit Sub
            UserForm1.Controls("BTwo" & Target.Row - 2).Visible = False
        End If
    End Select

这种方法在技术上有效,但只有在我离开第 8 个盒子之后。这似乎是一种 OnExit 的举动。第一行我只是做了一个工作正常的 onchange 事件。关于如何更改更改的可见性而不必选择另一个框的任何想法

4

0 回答 0