2017-08-02 68 views
0

我想請使用4個多項選擇複選框爲單元格賦值,如果它們的條件爲真,則每個複選框的值爲1。我想總結它們在鏈接單元格中的價值,以便單元格的值可以變化。如果如:通過使用4個多項選擇複選框爲單元格賦值

  1. 所有複選框條件是真實的在鏈接的單元格的值是4
  2. 他們幾個是真正在鏈接的單元格的值可以從1-3
  3. 所有的變化他們都是假的鏈接的單元格的值是0

     If CheckBox1.Value = True Then Range("D2").Value = 1 
    
    
        If CheckBox1.Value = False Then Range("D2").Value = 0 etc. 
    

我希望通過使用VBA宏來解決這個問題。

回答

0

這是回答我的問題:

顯式的選項

Sub CheckBox1_Click() 


Dim count As Integer 
If (count = Null) Then 
count = 0 
End If 

count = 0 
If ActiveSheet.Shapes("Check Box 1").ControlFormat = xlOn Then count = count + 1 
If ActiveSheet.Shapes("Check Box 2").ControlFormat = xlOn Then count = count + 1 
If ActiveSheet.Shapes("Check Box 3").ControlFormat = xlOn Then count = count + 1 
If ActiveSheet.Shapes("Check Box 4").ControlFormat = xlOn Then count = count + 1 
Range("A1").Value = count 

末次

0

你可以這樣做。基於鏈接的單元格獲得1如果爲真或零如果爲假。然後總和所有值。

enter image description here

enter image description here

用於VBA解決方案

Private Sub CheckBox1_Click() 
    Dim str As Integer 
    str = 0 
    If CheckBox1.Value = True Then str = str + 1 
    If CheckBox2.Value = True Then str = str + 1 
    If CheckBox3.Value = True Then str = str + 1 
    If CheckBox4.Value = True Then str = str + 1 
    Range("D2").Value = str 
    End Sub 
+0

非常感謝,但我更願意使用vba宏對它進行排序。你可以想象這個問題並不包含大局。 – Tom

+0

我已經更新了答案。請檢查 – Maddy

+0

嗨馬迪感謝,但我已經插入你的代碼,而當我點擊複選框時,我不想改變單元格D2。你對此有任何見解嗎? – Tom

0

點擊之前就設置宏的foreach複選框。

Public count As Integer 

Public Sub btn_Click() 
Dim cbName As String 

If (count = Null) Then 
    count = 0 
End If 

cbName = Application.Caller 

If (Sheets("Tabelle1").Shapes(cbName).ControlFormat.Value = xlOn And count < 4) Then 
    count = count + 1 
ElseIf (count > 0) Then 
    count = count - 1 
End If 
    Range("A1").Value = count 
End Sub 

enter image description here

enter image description here

+0

我有問題Tizzu與類型不匹配 – Tom

+0

您使用ActiveX複選框我猜這使得它更難 https://stackoverflow.com/questions/38282148/add-identical-code-to-multiple-combo-盒 如果你不關心格式使用FormControll複選框 – Tizzu

+0

你會建議什麼呢? – Tom

相關問題