当我清除内容时,我的动态组合框引用文本框不会清除

时间:2016-09-30 12:19:34

标签: excel vba excel-vba

我有一个组合框(DescBox1),它根据在文本框中输入的值引用动态范围。

Private Sub DescBox1_Change()
    Range("C25").Value = DescBox1.Value
End Sub

Private Sub UserForm_Initialize()        
    Me.DescBox1.RowSource = "CCList1"    
End Sub

当我们在TextBox(ClassCode1)中输入值时,它将值传输到活动工作簿单元格B25。

B25然后通过vlookup生成动态列表,然后变为CCList1。

Private Sub ClassCode1_Change()
    Range("B25").Value = ClassCode1.Value
End Sub

当我使用我的clearfields代码时,似乎首先清除所有文本框,删除动态查找引用,使其无法清除组合框(DescBox1)。

Private Sub ClearFields_Click()

    Dim ctl As Control

    For Each ctl In Me.Controls
        If ctl.Name = "ExpModFactor" Then
            Me.ExpModFactor.Value = 1
        Else
            If ctl.Name = "SurchargeTextBox" Then
                Me.SurchargeTextBox.Value = 100
            Else
                Select Case TypeName(ctl)
                    Case "ComboBox"
                        ctl.ListIndex = -1
                    Case "TextBox"
                        ctl.Value = ""
                    Case "CheckBox"
                        ctl.Value = False
                End Select
            End If
        End If
    Next ctl

End Sub

2 个答案:

答案 0 :(得分:0)

会这样做吗?

for i = me.controls.count to 1 step -1  
    with me.controls.item(i)
        (your code)
    end with
next

答案 1 :(得分:0)

我明白了。我将参考文献分开,以便按正确的顺序清除。我还添加了.ClearContents操作,用于清除工作表中的特定字段。

Private Sub ClearFields_Click()


Dim ctl As Control

For Each ctl In Me.Controls
    If TypeOf ctl Is MSForms.ComboBox Then
        ctl.Text = ""
    End If
Next ctl
For Each ctl In Me.Controls
    If TypeName(ctl) = "TextBox" Then
        ctl.Text = ""
    End If
Next ctl
For Each ctl In Me.Controls
    If TypeName(ctl) = "TextBox" Then
        ctl.Text = ""
    End If
Next ctl
For Each ctl In Me.Controls
    If TypeOf ctl Is MSForms.CheckBox Then
        ctl.Value = False
    End If
Next ctl
For Each ctl In Me.Controls
    If ctl.Name = "ExpModFactor" Then
        Me.ExpModFactor.Value = 1
    Else
    If ctl.Name = "SurchargeTextBox" Then
        Me.SurchargeTextBox.Value = 100
    End If
    End If
Next ctl


Range("B25:D48").ClearContents
Range("B6").ClearContents
Range("B10").ClearContents
相关问题