2014-03-13 359 views
1

我有一個Excel工作表,其中兩列CheckBoxes,其中每個的第一個CheckBox是「主」複選框,用於切換所有其他CheckBoxes。
我從This Tutorial得到了密碼。VBA Excel CheckBox選擇工作表上的某些特定複選框

它工作正常,直到我將代碼複製到第二列。
當激活第一個或第二個「Master」複選框時,它激活所有複選框。
第一個「主」複選框被稱爲「MCB1」,第二個在該代碼副本(與另一個子名稱)中被稱爲MCB2。

這裏是我的代碼:

Sub SelectAll_Read() 
Dim CB As CheckBox 
For Each CB In ActiveSheet.CheckBoxes 
    If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name Then 
    CB.Value = ActiveSheet.CheckBoxes("MCB1").Value 
    End If 
Next CB 
End Sub 

Sub Mixed_ReadState() 
Dim CB As CheckBox 
For Each CB In ActiveSheet.CheckBoxes 
    If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name And CB.Value <> ActiveSheet.CheckBoxes("MCB1").Value And ActiveSheet.CheckBoxes("MCB1").Value <> 2 Then 
    ActiveSheet.CheckBoxes("MCB1").Value = 2 
Exit For 
    Else 
    ActiveSheet.CheckBoxes("MCB1").Value = CB.Value 
    End If 
Next CB 
End Sub 

回答

1

首先你需要從第1列differenciate複選框那些列2

例如,你可以在第1列作爲MCB1.1名追隨者的複選框,MCB1.2MCB1.3等等。在第2欄的複選框同樣的事情:MCB2.1MCB2.2MCB2.3 ...

「大師」 在第1列複選框和2可以被命名爲MCB1MCB2

那麼你的代碼需要進行如下修改:(沒有時間來測試它)

Sub SelectAll_Read() 
Dim CB As CheckBox 
    For Each CB In ActiveSheet.CheckBoxes 
     If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name And CB.Name <> ActiveSheet.CheckBoxes("MCB2").Name Then 
      If Mid(CB.Name, 4, 1) = "1" 
       CB.Value = ActiveSheet.CheckBoxes("MCB1").Value 
      ElseIf Mid(CB.Name, 4, 1) = "2" 
       CB.Value = ActiveSheet.CheckBoxes("MCB2").Value 
      End If 
     End If 
    Next CB 
End Sub 

Sub Mixed_ReadState() 
Dim CB As CheckBox 
Dim i As String 

    For Each CB In ActiveSheet.CheckBoxes 
     i = Mid(CB.Name, 4, 1) 
     If CB.Name <> ActiveSheet.CheckBoxes("MCB" & i).Name And CB.Value <> ActiveSheet.CheckBoxes("MCB" & i).Value And ActiveSheet.CheckBoxes("MCB" & i).Value <> 2 Then 
      ActiveSheet.CheckBoxes("MCB" & i).Value = 2 
      Exit For 
     Else 
      ActiveSheet.CheckBoxes("MCB" & i).Value = CB.Value 
     End If 
    Next CB 
End Sub 
+0

在你的第二個段落,我覺得你的意思是:'...對於第2欄中的複選框:MCB2.1,MCB2.2,MCB2.3.' – ARich

+0

@ARICH是的,你是對的。謝謝! – hstay

1

這種替代方法爲我工作

Sub SelectAll_CHECK_BOX() 
    Dim CB As CheckBox 
    Dim CB1 As Range 
     Set CB1 = ActiveSheet.Range("A1:A30") 
    For Each CB In ActiveSheet.CheckBoxes 
      If Not Intersect(CB.TopLeftCell, CB1) Is Nothing Then 
       CB.Value = ActiveSheet.CheckBoxes("MCB1").Value 
      End If 
    Next CB 
    Dim CB2 As Range 
     Set CB2 = ActiveSheet.Range("B1:B30") 
    For Each CB In ActiveSheet.CheckBoxes 
      If Not Intersect(CB.TopLeftCell, CB2) Is Nothing Then 
       CB.Value = ActiveSheet.CheckBoxes("MCB2").Value 
      End If 
    Next CB 
End Sub 

Sub Mixed_ReadState() 
Dim CB As CheckBox 
    For Each CB In ActiveSheet.CheckBoxes 
     If CB.Name <> ActiveSheet.CheckBoxes("MCB1").Name And CB.Value <> ActiveSheet.CheckBoxes("MCB1").Value And ActiveSheet.CheckBoxes("MCB1").Value <> 2 Then 
     ActiveSheet.CheckBoxes("MCB1").Value = 2 
     Exit For 
     Else 
     ActiveSheet.CheckBoxes("MCB1").Value = CB.Value 
     End If 
    Next CB 
End Sub` 



Sub Mixed_ReadState() 
Dim CB As CheckBox 
    For Each CB In ActiveSheet.CheckBoxes 
     If CB.Name <> ActiveSheet.CheckBoxes("MCB2").Name And CB.Value <> ActiveSheet.CheckBoxes("MCB2").Value And ActiveSheet.CheckBoxes("MCB2").Value <> 2 Then 
     ActiveSheet.CheckBoxes("MCB2").Value = 2 
     Exit For 
     Else 
     ActiveSheet.CheckBoxes("MCB2").Value = CB.Value 
     End If 
    Next CB 
End Sub