Place a Button Caption in a Random Empty Excel UserForm Text Box
Question details
Place the caption of a clicked command button into a randomly selected empty text box on an Excel UserForm.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A UserForm has five command buttons and five text boxes. Each button click should fill one randomly chosen empty text box.
- Observed behavior
- Text boxes that already contain text should be excluded, and the user should be notified when none remain empty.
Open the workbook in a version of Excel that supports VBA UserForms. Check the control names in the UserForm’s Properties window; the example uses TextBox1 through TextBox5 and CommandButton1 through CommandButton5.
Use VBA to select a random empty UserForm text box
Use a shared procedure to collect empty text boxes and assign the clicked button’s caption to one of them.
The procedure treats a text box as empty only when it contains no characters. Text boxes containing spaces are considered occupied.
In Excel, select Developer > Visual Basic. In the VBA editor, select your UserForm in Project Explorer and open its code window.
Paste the following code into the UserForm’s code window. If the controls have different names, update the names in the code to match the Properties window. Private Sub PutCaptionInRandomEmptyBox(ByVal buttonCaption As String) Dim emptyBoxes As Collection Dim box As Object Dim i As Long Dim choice As Long Set emptyBoxes = New Collection For i = 1 To 5 Set box = Me.Controls("TextBox" & i) If Len(box.Text) = 0 Then emptyBoxes.Add box Next i If emptyBoxes.Count = 0 Then MsgBox "All text boxes are filled." Exit Sub End If Randomize choice = Int(Rnd * emptyBoxes.Count) + 1 emptyBoxes(choice).Value = buttonCaption End Sub Private Sub CommandButton1_Click() PutCaptionInRandomEmptyBox Me.CommandButton1.Caption End Sub Private Sub CommandButton2_Click() PutCaptionInRandomEmptyBox Me.CommandButton2.Caption End Sub Private Sub CommandButton3_Click() PutCaptionInRandomEmptyBox Me.CommandButton3.Caption End Sub Private Sub CommandButton4_Click() PutCaptionInRandomEmptyBox Me.CommandButton4.Caption End Sub Private Sub CommandButton5_Click() PutCaptionInRandomEmptyBox Me.CommandButton5.Caption End Sub
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm). In the VBA editor, select the UserForm and choose Run > Run Sub/UserForm, then click the buttons to check that only empty boxes are filled.

Work with Excel-format spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Excel formats, making it easy to move everyday spreadsheet work. The Excel VBA UserForm solution above should be run in Excel; macro and UserForm compatibility in WPS can vary by version and configuration.

Frequently Asked Questions
What happens when all five text boxes are filled?
The procedure checks the collection’s Count before choosing a random index. If it is zero, a message appears and the procedure exits without changing a text box.
How do I use different control names?
Select each control on the UserForm and check its Name in the Properties window. Update the TextBox name pattern and the CommandButton references in the code to match.
Can the same button caption appear more than once?
Yes. Each click selects from the currently empty text boxes, so clicking the same button again can place its caption in another empty box.
Why should I save the workbook as .xlsm?
The .xlsm format preserves VBA code. Use File > Save As and select Excel Macro-Enabled Workbook; enable macros only when you trust the workbook.




