# Visual Basic > Office Development >  [Excel] Looking for quick way to loop through Command Buttons

## Chillamba

I have a userform and when it is initialized, I need to reset the buttons back to default. I'm doing it like the below example.


```
With UserFormCalendar
        .CommandButton1.Caption = ""
        .CommandButton2.Caption = ""
        .CommandButton3.Caption = ""
        .CommandButton4.Caption = ""
        .CommandButton5.Caption = ""
        .CommandButton6.Caption = ""
        .CommandButton7.Caption = ""
        .CommandButton8.Caption = ""
        .CommandButton9.Caption = ""
        .CommandButton10.Caption = ""
        .CommandButton11.Caption = ""
        .CommandButton12.Caption = ""
        .CommandButton13.Caption = ""
        ...continued...
```

I have been testing out other methods of doing so (like using a loop for example), but I can't figure out how to get it working. Please let me know if something like the below example is possible.


```
With UserFormCalendar
        For h = 1 To 42
            tempBtnName = ".CommandButton" & h & ".Caption"
            Application.Run tempBtnName = "A"
        Next h
End With
```

----------


## Dan_W

Hi. In terms of turning this into a loop in order to wipe the captions of the Command Buttons, here are two possible ways of doing it:



```
Sub ResetButtons_Option1()
    Dim ButtonNumber As Long
    Dim LastButtonNumber As Long
    
    LastButtonNumber = 42
    
    For ButtonNumber = 1 To LastButtonNumber
        Me.Controls("CommandButton" & ButtonNumber).Caption = ""
    Next 
End Sub
```

This will loop from 1 to 42 (the LastButtonNumber), and it will access the UserForm control called CommandButton# and replace the caption with an empty string. Note that you with this approach, you would need to specify the first and last numbers of the numbered command buttons, and that there isn't any error handling, which you may want to consider using, just in case you happen to delete or rename one/some of the control(s). Also, another change I did was use the keyword Me. I did this assuming that the code would be located within the userform, in which case it is better to use Me rather than the userforms name.

The second method uses a different loop - the For Each loop. In this loop, it loops through all the userforms controls, and (1) first, checks if the control type is a CommandButton; and (2) if so, checks to see if the control name is like "CommandButton*" (here, asterisk is a wildcard of indefinite length). With this approach, it is limited to only those controls on the userform at any given time (no need to update LastButtonNumber variables and you won't have any difficulties with "missing" Command Buttons. 



```
Sub ResetButtons_Option2()
    Dim UFControl     As MSForms.Control
    Dim TargetUF      As MSForms.UserForm
    
    Set TargetUF = Me
    
    For Each UFControl In TargetUF.Controls
        If TypeName(UFControl) = "CommandButton" Then
            If UFControl.Name Like "CommandButton*" Then
                UFControl.Caption = ""
            End If
        End If
    Next
End Sub
```

Hope that helps.

----------

