Display or Hide Controls on UserForm Using Option Buttons

Excel 2007 Excel 2003 Macros Excel 2007 Macros Excel 2003 Excel 2010







A message from a website visitor:
I am working with user-forms in MS Excel. I have two textboxes on my form which I wish to hide till an optionbutton is selected. How can this be done using VBA code?

  1. We create a form with two textbox controls and two option buttons. You could also create only one option button
  2. We create the code for the form when it is first opened _form initialization). Here we want only the option buttons to be visible. Here we use the text function left() to extract the word 'Text' from the names of the 'TextBox1' and 'TextBox2' to make them invisible.
  3. Next we code the option buttons so that on clicking on option button 1 the textbox controls are displayed and on clicking on option button 2 the text box controls are hidden. We use a 'for... next' loop to display or hide the controls on the form. We also ensure that when the textbox controls are hidden our option button controls still remain visible.
  4. The code is given below.

VBA Code for the various user-form controls:

Private Sub OptionButton1_Click()
For Each objCtrl In Me.Controls
If OptionButton1.Value Then objCtrl.Visible = True
Next

End Sub

Private Sub OptionButton2_Click()
For Each objCtrl In Me.Controls
If OptionButton2.Value Then objCtrl.Visible = False
Next
OptionButton1.Visible = True
OptionButton2.Visible = True
End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
Dim objCtrl As Control

OptionButton1.Value = False
OptionButton2.Value = False

For Each objCtrl In Me.Controls
If Left(objCtrl.Name, 4) = "Text" Then objCtrl.Visible = False
Next
End Sub

The training video below explains the details of displaying or hiding the controls on an Excel user-form as desired by the user:


More Tips (Solutions):
How do I view and edit Microsoft Excel files on an iPad?
Inventory Soluton
How to rename and color worksheet tabs
Copy Worksheet Quickly
Criteria Range
Animations in Microsoft Excel
Create two charts at once
How to avoid errors while working in Excel
How to benefit from Microsoft Excel Templates
How to use Microsoft Excel 2007 with Word 2007 - create mail merge labels
Landscape Oriented Worksheet Template
How to-make a worksheet fit a printed page
How to make your charts more impressive
Perpetual Calendar from 1900 to 9999
How to create your own custom add-in
How to clear  conditional formattng  in Excel worksheets
How to speed up data entry of decimal numbers
How to change the color of the gridlines of an Excel worksheet or hide them
How to use Autosum in Multiple Worksheet Cells Quickly
How to have your free personal assistant in Mcrosoft Excel who reads out the data to you
How to use the status bar in Microsoft Excel to do a quick data analysis
How to calculate equal monthly payments or instalments using a mathematical equation
Data Forms to Enter and Edit Data
View 2 worksheets in same workbook side by side
Accessing a specific worksheet in large workbooks with multiple worksheets
Referencing a cell in another worksheet
How to perform a what-if analysis using a scrollbar form control
An interesting payroll solution
Future Value Solution in Microsoft Excel
Another Interesting Conditional Formatting Solution
DSUM and Array Formulas for addition solutions
IF Function Question
How to use command buttons on a splash screen in MS-Excel
Copy Paste Data from one Excel Worksheet to Another
How to separate comma separated values (csv) in a worksheet cell in Microsoft Excel into rows or columns
Another Interesting Solver solution in Microsoft Excel
Find duplicates in two different Excel worksheets using a macro
How to count data based on multiple criteria - countifs function
How to convert 6/5/2011 into Sunday, 6/5/2011
User Form to perform calculations
An interesting solution using conditional formatting in Microsoft Excel 2007
Show/Hide Controls on a User-Form using a Checkbox
Populate Listbox and Textboxes with Excel data using VBA and Vlookup
Protecting Specific Worksheets in an Excel Workbook using VBA
How to calculate the difference in hours between two date-time values
How do I match data from 2 worksheets and highlight the differences using MS-Excel VBA?
How to focus on a specific control (TextBox1) on a user-form in MS-Excel
Interesting sumproduct solution
COMPARE DATA LISTS USING VLOOKUP AND ISNA FUNCTIONS
VLOOKUP SOLUTION USING LIST IN MS EXCEL
How to generate random numbers in MS Excel
Lottery game in Microsoft Excel
Nested If Function to Determine Project Complexity
Power Billing Solution in Microsoft Excel
Solver Excel - An easy to understand example
Another Interest Calculation Solution
Interesting Microsoft Excel Formula Based on Date and Row Functions
How to subtract dates in Microsoft Excel
Automating rounding of decimal numbers in MS-Excel
How to Calculate Profit Loss of security transactions in MS Excel
How to print comments in Microsoft Excel
How to Display Results in a Specific Way in Excel
How to Filter or Hide Rows & Display Excel Data in Excel Using A Macro
Automate Chart Creation Using Excel Macros (VBA)
Add Text To Displayed Excel Numerical Value
How to develop an advanced function or formula in MS-Excel
Apply Autofilter Across Multiple Excel Worksheets
Inventory Solution in Microsoft Excel
How to open a password protected Excel worksheet whose password has been lost
Hyperlinks and VBA Code to Navigate worksheets in Large Workbooks
Count Total Between Two Dates Using COUNTIFS Function
Dice Game in MS-Excel
Where is my autofill handle?
Payment Calculations based on Timestamps
How to convert an Excel File into a Text File
How to consolidate data from multiple workbooks or files
How to display the value of a number in words in Excel
Copy Paste Data from one Excel Worksheet to another using an Inputbox
How to Calculate Amount Due on Delayed Invoice Payments
Interesting Use of Combo-Box Form Control
How to Use Excel - MROUND() Function
How to print formulas in Excel
Excel VBA Example
Custom Formulas - Excel Financial Calculator
Interesting use of Advanced Filter in MS Excel
Find Duration in Hours between two Dates using VBA
Excel 2007 Excel 2003 Macros Excel 2007 Macros Excel 2003 Excel 2010