如何在VBA中的userform中使用计时器

时间:2017-01-08 10:44:38

标签: vba excel-vba excel

我有这样的场景:我有5个复选框和两个组合框。单击任何复选框时,组合框中应显示相应的标题,其余复选框标题应出现在combobox2中。我尝试按如下方式执行。

Private Sub CheckBox1_Click() 
  ButtonOneClick = True 
  Call ComboBox5_Change 
  Call ComboBox6_Change 
End Sub

Private Sub CheckBox2_Click() 
  ButtonOneClick = True 
  Call ComboBox5_Change Call 
  ComboBox6_Change 
  End Sub

Private Sub CheckBox3_Click() 
  ButtonOneClick = True 
  Call ComboBox5_Change 
  Call ComboBox6_Change 
End Sub

Private Sub CheckBox4_Click() 
  ButtonOneClick = True 
  Call ComboBox5_Change 
  Call ComboBox6_Change 
End Sub

Private Sub CheckBox5_Click() 
  ButtonOneClick = True 
  Call ComboBox5_Change 
  Call ComboBox6_Change 
End Sub

Private Sub ComboBox5_Change()
  If ButtonOneClick = True Then 
    Dim StringVar As String 
    Dim StringVar1 As String 
    Dim StringVar2 As String 
    Dim StringVar3 As String 
    Dim StringVar4 As String 
    Dim StringVar5 As String 
    Dim StringVar6 As String 
    Dim StringVar7 As String 
    Dim StringVar8 As String 
    Dim StringVar9 As String 
    Dim StringVar10 As String

    If ComboBox5.ListIndex > -1 Then 
      Do While ComboBox5.ListCount > 0 
        ComboBox5.RemoveItem (0) 
      Loop 
      Do While ComboBox6.ListCount > 0 
        ComboBox6.RemoveItem (0) 
      Loop 
    End If

    If Me.CheckBox1.Value = True And Me.CheckBox5.Value = True And Me.CheckBox3.Value = True And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption 
      StringVar1 = Me.CheckBox1.Caption & "\" & Me.CheckBox4.Caption 
      StringVar2 = Me.CheckBox1.Caption & "\" & Me.CheckBox5.Caption 
      StringVar3 = Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption 
      StringVar4 = Me.CheckBox3.Caption & "\" & Me.CheckBox5.Caption 
      StringVar5 = Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      StringVar6 = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption 
      StringVar7 = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption & "\" & Me.CheckBox5.Caption 
      StringVar8 = Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      StringVar9 = Me.CheckBox1.Caption & "\" & Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      StringVar10 = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox3.Caption 
        .AddItem Me.CheckBox4.Caption 
        .AddItem Me.CheckBox5.Caption 
        .AddItem StringVar 
        .AddItem StringVar1 
        .AddItem StringVar2 
        .AddItem StringVar3 
        .AddItem StringVar4 
        .AddItem StringVar5 
        .AddItem StringVar6 
        .AddItem StringVar7 
        .AddItem StringVar8 
        .AddItem StringVar9 
        .AddItem StringVar10 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = True And Me.CheckBox3.Value = True And Me.CheckBox4.Value = False Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption 
      StringVar1 = Me.CheckBox1.Caption & "\" & Me.CheckBox5.Caption 
      StringVar2 = Me.CheckBox3.Caption & "\" & Me.CheckBox5.Caption 
      StringVar3 = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox3.Caption 
        .AddItem Me.CheckBox5.Caption 
        .AddItem StringVar 
        .AddItem StringVar1 
        .AddItem StringVar2 
        .AddItem StringVar3 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = True And Me.CheckBox3.Value = False And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox4.Caption 
      StringVar1 = Me.CheckBox1.Caption & "\" & Me.CheckBox5.Caption 
      StringVar2 = Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      StringVar3 = Me.CheckBox1.Caption & "\" & Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox4.Caption 
        .AddItem Me.CheckBox5.Caption 
        .AddItem StringVar 
        .AddItem StringVar1 
        .AddItem StringVar2 
        .AddItem StringVar3 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = True And Me.CheckBox3.Value = False And Me.CheckBox4.Value = False Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox5.Caption 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem StringVar 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = False And Me.CheckBox3.Value = True And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption 
      StringVar1 = Me.CheckBox1.Caption & "\" & Me.CheckBox4.Caption 
      StringVar2 = Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption 
      StringVar3 = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox3.Caption 
        .AddItem Me.CheckBox4.Caption 
        .AddItem StringVar 
        .AddItem StringVar1 
        .AddItem StringVar2 
        .AddItem StringVar3 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = False And Me.CheckBox3.Value = True And Me.CheckBox4.Value = False Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox3.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox3.Caption 
        .AddItem StringVar 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = False And Me.CheckBox3.Value = False And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox1.Caption & "\" & Me.CheckBox4.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox4.Caption 
        .AddItem StringVar 
      End With 
    ElseIf Me.CheckBox1.Value = True And Me.CheckBox5.Value = False And Me.CheckBox3.Value = False And Me.CheckBox4.Value = False Then 
      With ComboBox5 
        .AddItem Me.CheckBox1.Caption 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = True And Me.CheckBox3.Value = True And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption 
      StringVar1 = Me.CheckBox3.Caption & "\" & Me.CheckBox5.Caption 
      StringVar2 = Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      StringVar3 = Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox3.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox4.Caption 
        .AddItem Me.CheckBox5.Caption 
        .AddItem StringVar 
        .AddItem StringVar1 
        .AddItem StringVar2 
        .AddItem StringVar3 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = True And Me.CheckBox3.Value = True And Me.CheckBox4.Value = False Then 
      StringVar = Me.CheckBox3.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox3.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox5.Caption 
        .AddItem StringVar 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = True And Me.CheckBox3.Value = False And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox4.Caption & "\" & Me.CheckBox5.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox4.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox5.Caption 
        .AddItem StringVar 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = True And Me.CheckBox3.Value = False And Me.CheckBox4.Value = False Then 
      With ComboBox5 
        .AddItem Me.CheckBox5.Caption 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = False And Me.CheckBox3.Value = True And Me.CheckBox4.Value = True Then 
      StringVar = Me.CheckBox3.Caption & "\" & Me.CheckBox4.Caption 
      With ComboBox5 
        .AddItem Me.CheckBox3.Caption 
        .AddItem Me.CheckBox2.Caption 
        .AddItem Me.CheckBox4.Caption 
        .AddItem StringVar 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = False And Me.CheckBox3.Value = True And Me.CheckBox4.Value = False Then 
      With ComboBox5 
        .AddItem Me.CheckBox3.Caption 
      End With 
    ElseIf Me.CheckBox1.Value = False And Me.CheckBox5.Value = False And Me.CheckBox3.Value = False And Me.CheckBox4.Value = True Then 
      With ComboBox5 
        .AddItem Me.CheckBox4.Caption 
      End With 
    ElseIf Me.CheckBox2.Value = True Then 
      With ComboBox5 
        .AddItem Me.CheckBox2.Caption 
      End With 
    End If 
  End If
End Sub

1 个答案:

答案 0 :(得分:0)

根据您的“情景”:

  

我有5个复选框和两个组合框。当任何复选框是   点击相应的标题应出现在combobox1和   其余的复选框标题应出现在combobox2

您可以从UserForm代码窗格中的以下代码开始:

Option Explicit

Private Sub CheckBox1_Click()
    SetComboboxes
End Sub

Private Sub CheckBox2_Click()
    SetComboboxes
End Sub

Private Sub CheckBox3_Click()
    SetComboboxes
End Sub

Private Sub CheckBox4_Click()
    SetComboboxes
End Sub

Private Sub CheckBox5_Click()
    SetComboboxes
End Sub


Private Sub SetComboboxes()
    With Me.ActiveControl
        If .value Then '<--| if current checkbox is "checked"
            Me.ComboBox1.AddItem .Caption '<--| add its caption to ComboBox1 entries
            RemoveChkBoxCaption Me.ComboBox2, .Caption '<--| remove its caption from ComboBox2 entries
        Else '<--| if current checkbox is "unchecked"
            Me.ComboBox2.AddItem .Caption '<--| add its caption to ComboBox2 entries
            RemoveChkBoxCaption Me.ComboBox1, .Caption '<--| remove its caption from ComboBox1 entries
        End If
    End With
End Sub

Private Sub RemoveChkBoxCaption(cb As MSForms.ComboBox, chkboxCaption As String)
    Dim cbIndex As Long

    With cb
        If .ListCount = 0 Then Exit Sub
        For cbIndex = 0 To UBound(.List)
            If .List(cbIndex) = chkboxCaption Then
                .RemoveItem cbIndex
                Exit For
            End If
        Next
    End With
End Sub

Private Sub UserForm_Initialize()
    Dim ctrl As Control

    With Me.ComboBox2
        For Each ctrl In Me.Controls
            If TypeName(ctrl) = "CheckBox" Then .AddItem ctrl.Caption
        Next
    End With
End Sub