1

I have 20 checkboxes named checkbox1 to checkbox20.

I would like to record the username in column B when each checkbox is checked.

For example, when a user clicks on checkbox1, B1 would populate the username, if checkbox2 is clicked, B2 would populate username.

I know how to write code for each checkbox and copy it 20 times. How can I combine them into one sub?

Private Sub CheckBox1_Click()

Dim i As Integer

For i = 1 To 20

    If Me.CheckBox & i = True Then

        Range("b1" & i).Value = Environ("USERNAME")

    Else: Range("b1" & i).Value = ""

    End If

Next

End Sub
2
  • This should be helpful. Commented Jan 5, 2020 at 1:31
  • You may create wrapper class and declare Public WithEvents ctl As MSForms.CheckBox and create Private Sub ctl_Click() or Private Sub ctl_Change() which will handle checkbox event, then create 20 instances of the class and assign each checkbox to each instance within loop. Note you should use Form Controls Check Boxes. Commented Jan 5, 2020 at 2:43

1 Answer 1

0

In a module write following sub and then call this sub from CheckBox_Click event.

Sub LoopCheckBoxes()
Dim i As Long
    For i = 1 To 20
        If ActiveSheet.Shapes("Check Box " & i).OLEFormat.Object.Value = 1 Then
            Range("B" & i) = Environ("USERNAME")
        End If
    Next i
End Sub

See below screenshot to call above sub from checkbox click event.

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.