VBA & Macro Problems

Show a Text Box from a Multi-Select List Box in Excel VBA

Jason MillerJason Miller Nov 14, 2026 868 views

Question details

Show a hidden UserForm text box when any selected list-box item begins with EF.

Show a Text Box Based on a Multi-Select List Box in Excel VBA
Product
Microsoft Excel VBA UserForm
Device & OS
not provided
Scenario
A UserForm has a multi-select list box and an initially hidden text box. The selection should be checked when the user clicks OK.
Observed behavior
The text box should be visible if at least one selected item starts with EF and hidden otherwise.
Before you start

Open the UserForm in the VBA editor and check the names of its list box, text box, and OK button. In the list box Properties window, set MultiSelect to a multi-select option.

Solution 1Recommended

Check selected list-box items when the user clicks OK

Use this approach to show the text box after OK is clicked if any selected item starts with EF.

This example uses the control names lstMyList, txtEF, and cmdOK. Replace them with your actual control names if necessary. The prefix comparison is case-insensitive.

1
Open the form code

Open the Visual Basic Editor, select the UserForm in Project Explorer, and choose View > Code.

2
Hide the text box initially

Add this event procedure to the UserForm code, or add its statement to your existing UserForm_Initialize procedure: Private Sub UserForm_Initialize() txtEF.Visible = False End Sub

3
Check selections on OK

Add this procedure to the same UserForm code. If cmdOK_Click already exists, incorporate the statements into it instead of creating a duplicate: Private Sub cmdOK_Click() Dim i As Long txtEF.Visible = False For i = 0 To lstMyList.ListCount - 1 If lstMyList.Selected(i) Then If StrComp(Left$(CStr(lstMyList.List(i)), 2), "EF", vbTextCompare) = 0 Then txtEF.Visible = True Exit For End If End If Next i End Sub

4
Test the form

Run the UserForm, select one or more list items, and click OK. Try a selection beginning with EF and another without an EF item to verify both outcomes.

Check selected list-box items when the user clicks OK
When visibility changes: The selection is checked when OK is clicked, not immediately when an item is selected. The code also hides the text box again if a later check finds no matching selection.
Free Microsoft Office alternative

Work with Excel files in WPS Office

WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing spreadsheets. Its Excel format compatibility helps you move everyday workbook tasks to WPS, although it does not fix an Excel VBA UserForm.

Open and edit Microsoft Excel XLSX workbooksUse a familiar spreadsheet interface for list-based dataMove everyday spreadsheet work from Excel with compatible file formatsUse a lightweight office suite for common document tasks
microsoft office alternative - wps office

Frequently Asked Questions

How do I allow multiple selections in the list box?

Select the list box in the UserForm designer and set its MultiSelect property to a multi-select option in the Properties window.

Why does the text box stay hidden after I select an EF item?

The example checks selections only when cmdOK is clicked. Verify that the button is named cmdOK, the text box is named txtEF, and the selected item's first two characters are EF.

Does the prefix check distinguish between uppercase and lowercase?

No. The example uses vbTextCompare, so items beginning with EF, ef, or mixed-case equivalents match.

Can I show the text box as soon as the selection changes?

Yes. Move the selection-checking logic into an appropriate list-box event, such as lstMyList_Change, and test it with your form's selection behavior.