SImplifying if statements

I'm making a VBA-project and want to simplify the following if-statements, because i need to this following 11 times more, for each person.

'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

Upvotes: 0

Views: 331

Answers (3)

user3598756
user3598756

Reputation: 29421

you could try this:

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

Upvotes: 1

A.S.H
A.S.H

Reputation: 29352

In addition to factorizing the test on "CTK", you can also define an array for your ToggleButtons and iterate on it:

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 

Upvotes: 1

jjislam
jjislam

Reputation: 563

If result = "CTK" Then
    If ToggleButton1 = True Then
        Range("B4").Value= "X"
    ElseIf ToggleButton2 = True Then
        Range("C4").Value = "X"
    ElseIf...........etc

Upvotes: 0

Related Questions