简化 if 语句
SImplifying if statements
我正在制作一个 VBA 项目并希望简化以下 if 语句,因为我需要为每个人做 11 次以上的后续操作。
'CTK opportunities
If result = "CTK" And ToggleButton1 = True Then Range("B4").Value = "X"
If result = "CTK" And ToggleButton2 = True Then Range("C4").Value = "X"
If result = "CTK" And ToggleButton3 = True Then Range("D4").Value = "X"
If result = "CTK" And ToggleButton4 = True Then Range("E4").Value = "X"
If result = "CTK" And ToggleButton5 = True Then Range("F4").Value = "X"
If result = "CTK" And ToggleButton6 = True Then Range("G4").Value = "X"
If result = "CTK" And ToggleButton7 = True Then Range("H4").Value = "X"
If result = "CTK" And ToggleButton8 = True Then Range("I4").Value = "X"
If result = "CTK" And ToggleButton9 = True Then Range("J4").Value = "X"
If result = "CTK" And ToggleButton10 = True Then Range("K4").Value = "X"
If result = "CTK" And ToggleButton11 = True Then Range("L4").Value = "X"
If result = "CTK" And ToggleButton12 = True Then Range("M4").Value = "X"
'Next person
If result = "CTK" Then
If ToggleButton1 = True Then
Range("B4").Value= "X"
ElseIf ToggleButton2 = True Then
Range("C4").Value = "X"
ElseIf...........etc
除了分解 "CTK"
上的测试之外,您还可以为 ToggleButtons 定义一个数组并对其进行迭代:
toggleButtons = Array(ToggleButton1, ToggleButton2, .... , ToggleButton12)
If(result = CTK) Then
For i = 0 to UBound(toggleButtons)
If toggleButtons[i] = True Then Cells(4, i+2).Value = "X"
Next
End If
你可以试试这个:
Dim ctrl As Control
If result = "CTK" Then
For Each ctrl In Me.Controls
If InStr(ctrl.Name, "ToggleButton") > 0 Then
If ctrl.Value Then Cells(4, CInt(Replace(ctrl.Name, "ToggleButton", "")) + 1).Value = "X"
End If
Next ctrl
End If
我正在制作一个 VBA 项目并希望简化以下 if 语句,因为我需要为每个人做 11 次以上的后续操作。
'CTK opportunities
If result = "CTK" And ToggleButton1 = True Then Range("B4").Value = "X"
If result = "CTK" And ToggleButton2 = True Then Range("C4").Value = "X"
If result = "CTK" And ToggleButton3 = True Then Range("D4").Value = "X"
If result = "CTK" And ToggleButton4 = True Then Range("E4").Value = "X"
If result = "CTK" And ToggleButton5 = True Then Range("F4").Value = "X"
If result = "CTK" And ToggleButton6 = True Then Range("G4").Value = "X"
If result = "CTK" And ToggleButton7 = True Then Range("H4").Value = "X"
If result = "CTK" And ToggleButton8 = True Then Range("I4").Value = "X"
If result = "CTK" And ToggleButton9 = True Then Range("J4").Value = "X"
If result = "CTK" And ToggleButton10 = True Then Range("K4").Value = "X"
If result = "CTK" And ToggleButton11 = True Then Range("L4").Value = "X"
If result = "CTK" And ToggleButton12 = True Then Range("M4").Value = "X"
'Next person
If result = "CTK" Then
If ToggleButton1 = True Then
Range("B4").Value= "X"
ElseIf ToggleButton2 = True Then
Range("C4").Value = "X"
ElseIf...........etc
除了分解 "CTK"
上的测试之外,您还可以为 ToggleButtons 定义一个数组并对其进行迭代:
toggleButtons = Array(ToggleButton1, ToggleButton2, .... , ToggleButton12)
If(result = CTK) Then
For i = 0 to UBound(toggleButtons)
If toggleButtons[i] = True Then Cells(4, i+2).Value = "X"
Next
End If
你可以试试这个:
Dim ctrl As Control
If result = "CTK" Then
For Each ctrl In Me.Controls
If InStr(ctrl.Name, "ToggleButton") > 0 Then
If ctrl.Value Then Cells(4, CInt(Replace(ctrl.Name, "ToggleButton", "")) + 1).Value = "X"
End If
Next ctrl
End If