问题
Following Userform
has 4 (Four) Select All
Check-boxes
.
SelectAllE for Eastern Europe
SelectAllA for Middle East & Africa
SelectAllL for Latin America & Caribbean
SA for Select All Regions (Working)
only the fourth one is working correctly
Following are codes for each Checkboxes
Private Sub SA_Click() ' this one is working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Bulgaria.Value = True
Me.Croatia.Value = True
Me.Croatia.Value = True
Me.czechrepublic.Value = True
Me.Estonia.Value = True
Me.Hungary.Value = True
Me.Latvia.Value = True
Me.Lithuania.Value = True
Me.Macedonia.Value = True
Me.Poland.Value = True
Me.Romania.Value = True
Me.Russia.Value = True
Me.Ukraine.Value = True
Me.Bahrain.Value = True
Me.CoteD.Value = True
Me.Egypt.Value = True
Me.Ghana.Value = True
Me.Iraq.Value = True
Me.Kenya.Value = True
Me.Kuwait.Value = True
Me.Morocco.Value = True
Me.Namibia.Value = True
Me.Nigeria.Value = True
Me.Oman.Value = True
Me.Palestine.Value = True
Me.Qatar.Value = True
Me.Rwanda.Value = True
Me.SaudiArabia.Value = True
Me.Turkey.Value = True
Me.Uganda.Value = True
Me.UAE.Value = True
Me.Zimbabwe.Value = True
Me.Chile.Value = True
Me.Mexico.Value = True
Me.Colombia.Value = True
Else
Me.Bulgaria.Value = False
Me.Croatia.Value = False
Me.Croatia.Value = False
Me.czechrepublic.Value = False
Me.Estonia.Value = False
Me.Hungary.Value = False
Me.Latvia.Value = False
Me.Lithuania.Value = False
Me.Macedonia.Value = False
Me.Poland.Value = False
Me.Romania.Value = False
Me.Russia.Value = False
Me.Ukraine.Value = False
Me.Bahrain.Value = False
Me.CoteD.Value = False
Me.Egypt.Value = False
Me.Ghana.Value = False
Me.Iraq.Value = False
Me.Kenya.Value = False
Me.Kuwait.Value = False
Me.Morocco.Value = False
Me.Namibia.Value = False
Me.Nigeria.Value = False
Me.Oman.Value = False
Me.Palestine.Value = False
Me.Qatar.Value = False
Me.Rwanda.Value = False
Me.SaudiArabia.Value = False
Me.Turkey.Value = False
Me.Uganda.Value = False
Me.UAE.Value = False
Me.Zimbabwe.Value = False
End If
End If
Next
End Sub
Private Sub SelectallE_Click() ' this one is not working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Bulgaria.Value = True
Me.Croatia.Value = True
Me.Croatia.Value = True
Me.czechrepublic.Value = True
Me.Estonia.Value = True
Me.Hungary.Value = True
Me.Latvia.Value = True
Me.Lithuania.Value = True
Me.Macedonia.Value = True
Me.Poland.Value = True
Me.Romania.Value = True
Me.Russia.Value = True
Me.Ukraine.Value = True
Else
Me.Bulgaria.Value = False
Me.Croatia.Value = False
Me.Croatia.Value = False
Me.czechrepublic.Value = False
Me.Estonia.Value = False
Me.Hungary.Value = False
Me.Latvia.Value = False
Me.Lithuania.Value = False
Me.Macedonia.Value = False
Me.Poland.Value = False
Me.Romania.Value = False
Me.Russia.Value = False
Me.Ukraine.Value = False
Me.Chile.Value = False
Me.Mexico.Value = False
Me.Colombia.Value = False
End If
End If
Next
End Sub
Private Sub SelectAllA_Click() ' this one is not working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Bahrain.Value = True
Me.CoteD.Value = True
Me.Egypt.Value = True
Me.Ghana.Value = True
Me.Iraq.Value = True
Me.Kenya.Value = True
Me.Kuwait.Value = True
Me.Morocco.Value = True
Me.Namibia.Value = True
Me.Nigeria.Value = True
Me.Oman.Value = True
Me.Palestine.Value = True
Me.Qatar.Value = True
Me.Rwanda.Value = True
Me.SaudiArabia.Value = True
Me.Turkey.Value = True
Me.Uganda.Value = True
Me.UAE.Value = True
Me.Zimbabwe.Value = True
Else
Me.Bahrain.Value = False
Me.CoteD.Value = False
Me.Egypt.Value = False
Me.Ghana.Value = False
Me.Iraq.Value = False
Me.Kenya.Value = False
Me.Kuwait.Value = False
Me.Morocco.Value = False
Me.Namibia.Value = False
Me.Nigeria.Value = False
Me.Oman.Value = False
Me.Palestine.Value = False
Me.Qatar.Value = False
Me.Rwanda.Value = False
Me.SaudiArabia.Value = False
Me.Turkey.Value = False
Me.Uganda.Value = False
Me.UAE.Value = False
Me.Zimbabwe.Value = False
End If
End If
Next
End Sub
Private Sub SelectAllL_Click() ' this one is not working Correctly
Dim ctl As Control
Dim j As Long
For Each ctl In Me.Controls
If TypeOf ctl Is MSForms.CheckBox Then
If Me.Controls(ctl.Name).Value = True Then
Me.Chile.Value = True
Me.Mexico.Value = True
Me.Colombia.Value = True
Else
Me.Chile.Value = False
Me.Mexico.Value = False
Me.Colombia.Value = False
End If
End If
Next
End Sub
回答1:
Your code is too complicated, why not keeping it more simple? Since you are inside the Click event you know the control and don't need to loop each control of the userform!
Private Sub SA_Click()
SelectAllA.Value = SA.Value
SelectAllL.Value = SA.Value
SelectallE.Value = SA.Value
End Sub
Private Sub SelectallE_Click()
Bulgaria.Value = SelectallE.Value
Croatia.Value = SelectallE.Value
Croatia.Value = SelectallE.Value
czechrepublic.Value = SelectallE.Value
Estonia.Value = SelectallE.Value
Hungary.Value = SelectallE.Value
Latvia.Value = SelectallE.Value
Lithuania.Value = SelectallE.Value
Macedonia.Value = SelectallE.Value
Poland.Value = SelectallE.Value
Romania.Value = SelectallE.Value
Russia.Value = SelectallE.Value
Ukraine.Value = SelectallE.Value
End Sub
Private Sub SelectAllA_Click()
Bahrain.Value = SelectAllA.Value
CoteD.Value = SelectAllA.Value
Egypt.Value = SelectAllA.Value
Ghana.Value = SelectAllA.Value
Iraq.Value = SelectAllA.Value
Kenya.Value = SelectAllA.Value
Kuwait.Value = SelectAllA.Value
Morocco.Value = SelectAllA.Value
Namibia.Value = SelectAllA.Value
Nigeria.Value = SelectAllA.Value
Oman.Value = SelectAllA.Value
Palestine.Value = SelectAllA.Value
Qatar.Value = SelectAllA.Value
Rwanda.Value = SelectAllA.Value
SaudiArabia.Value = SelectAllA.Value
Turkey.Value = SelectAllA.Value
Uganda.Value = SelectAllA.Value
UAE.Value = SelectAllA.Value
Zimbabwe.Value = SelectAllA.Value
End Sub
Private Sub SelectAllL_Click()
Chile.Value = SelectAllL.Value
Mexico.Value = SelectAllL.Value
Colombia.Value = SelectAllL.Value
End Sub
来源:https://stackoverflow.com/questions/37315117/more-than-one-select-all-checkboxes-for-each-frame