Simplify or reduce VBA code

Aretradeser

Board Regular
Joined
Jan 16, 2013
Messages
176
Office Version
  1. 2013
Platform
  1. Windows
How can I simplify VBA codes, concerning the hiding or visibility of different objects?
EXAMPLE 1:
Rich (BB code):
Private Sub UserForm_Initialize()
    With Sheets("BDATOS")
        n = .Range("B" & Rows.Count).End(xlUp).Row
        va = .Range("G2:G" & n) 'ComboBox1 = By country of destination
        vb = .Range("H2:H" & n) 'ComboBox2 = By destination city
        vc = .Range("B2:B" & n) 'ComboBox3 = By name
    End With
    Set dar = CreateObject("System.Collections.Arraylist")
    Label2.Visible = False
    Label3.Visible = False
    TextBox1.Visible = False
    TextBox2.Visible = False
    TextBox3.Visible = False
    CheckBox2.Visible = False
    CheckBox3.Visible = False
    CheckBox4.Visible = False
    Label4.Visible = False
    Label5.Visible = False
    Label7.Visible = False
    ComboBox1.Visible = False
    ComboBox2.Visible = False
    ComboBox3.Visible = False
End Sub
EXAMPLE 2
Rich (BB code):
Private Sub OptionButton2_Click()
OptionButton1.Visible = False
OptionButton4.Visible = False

    CheckBox2.Visible = True
    CheckBox3.Visible = True
    CheckBox4.Visible = True
    Label4.Visible = True
    Label5.Visible = True
    Label7.Visible = True
    ComboBox1.Visible = True
    ComboBox2.Visible = True
    ComboBox3.Visible = True
    Label1.Visible = False
    TextBox1.Visible = False
    Label8.Visible = False
    Label2.Visible = False
    TextBox2.Visible = False
    Label3.Visible = False
    TextBox3.Visible = False
    TextBox1 = ""
    ListBox1.RowSource = ""
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I don't see anything that decides what is visible when, and they're not in the same order, so it's a bit too difficult to discern the difference between those 2 codes. Perhaps loop over the collection of controls involved whose tag property you have set to some value (e.g. Required) if you want to hide/unhide just some of them according to some condition. If all of them need to be altered according to a condition, then don't worry about the tag property.
 
Upvote 0
Hi,
try creating a table of the controls with their visible state for each requirement in your project listed in column(s)

Copy this table to a worksheet named "Form Controls"

07-04-2022.xls
ABC
1ControlState 1State 2
2OptionButton1FALSEFALSE
3OptionButton4FALSEFALSE
4CheckBox2TRUEFALSE
5CheckBox3TRUEFALSE
6CheckBox4TRUEFALSE
7ComboBox1TRUEFALSE
8ComboBox2TRUEFALSE
9ComboBox3TRUEFALSE
10TextBox1FALSEFALSE
11TextBox2FALSEFALSE
12TextBox3FALSEFALSE
13Label1FALSETRUE
14Label2FALSEFALSE
15Label3FALSEFALSE
16Label4TRUEFALSE
17Label5TRUEFALSE
18Label7TRUEFALSE
19Label8FALSETRUE
Form Controls
Cells with Data Validation
CellAllowCriteria
B2:C19ListTrue,False


Place following code In a STANDARD module

VBA Code:
Enum ControlState
    State1 = 2
    State2
End Enum
Sub ControlsVisible(ByVal Form As Object, ByVal State As ControlState)
    Dim arr         As Variant
    Dim rng         As Range
   Dim i           As Long

    With ThisWorkbook.Worksheets("Form Controls")
        Set rng = .Range("A1").CurrentRegion
       ' .Visible = xlSheetVeryHidden '< optional
    End With
    Set rng = rng.Offset(1).Resize(rng.Rows.Count - 1)
    arr = rng.Value2
    For i = 1 To UBound(arr, 1)
        Form.Controls(arr(i, 1)).Visible = arr(i, State)
    Next
End Sub

From your forms CODE Page

Rich (BB code):
Private Sub OptionButton2_Click()
   
    ControlsVisible Me, State2
   
End Sub

Private Sub UserForm_Initialize()
    With Sheets("BDATOS")
        n = .Range("B" & Rows.Count).End(xlUp).Row
        va = .Range("G2:G" & n) 'ComboBox1 = By country of destination
        vb = .Range("H2:H" & n) 'ComboBox2 = By destination city
        vc = .Range("B2:B" & n) 'ComboBox3 = By name
    End With
    Set dar = CreateObject("System.Collections.Arraylist")
   
    ControlsVisible Me, State1
End Sub

Solution is untested & you may have to update the visible State of each control in the table as required.
Table can be expanded to include other permutations if needed

Dave
 
Upvote 0
Solution
I don't see anything that decides what is visible when, and they're not in the same order, so it's a bit too difficult to discern the difference between those 2 codes. Perhaps loop over the collection of controls involved whose tag property you have set to some value (e.g. Required) if you want to hide/unhide just some of them according to some condition. If all of them need to be altered according to a condition, then don't worry about the tag property.
Thank you very much for your answer.
 
Upvote 0
Hi,
try creating a table of the controls with their visible state for each requirement in your project listed in column(s)

Copy this table to a worksheet named "Form Controls"

07-04-2022.xls
ABC
1ControlState 1State 2
2OptionButton1FALSEFALSE
3OptionButton4FALSEFALSE
4CheckBox2TRUEFALSE
5CheckBox3TRUEFALSE
6CheckBox4TRUEFALSE
7ComboBox1TRUEFALSE
8ComboBox2TRUEFALSE
9ComboBox3TRUEFALSE
10TextBox1FALSEFALSE
11TextBox2FALSEFALSE
12TextBox3FALSEFALSE
13Label1FALSETRUE
14Label2FALSEFALSE
15Label3FALSEFALSE
16Label4TRUEFALSE
17Label5TRUEFALSE
18Label7TRUEFALSE
19Label8FALSETRUE
Form Controls
Cells with Data Validation
CellAllowCriteria
B2:C19ListTrue,False


Place following code In a STANDARD module

VBA Code:
Enum ControlState
    State1 = 2
    State2
End Enum
Sub ControlsVisible(ByVal Form As Object, ByVal State As ControlState)
    Dim arr         As Variant
    Dim rng         As Range
   Dim i           As Long

    With ThisWorkbook.Worksheets("Form Controls")
        Set rng = .Range("A1").CurrentRegion
       ' .Visible = xlSheetVeryHidden '< optional
    End With
    Set rng = rng.Offset(1).Resize(rng.Rows.Count - 1)
    arr = rng.Value2
    For i = 1 To UBound(arr, 1)
        Form.Controls(arr(i, 1)).Visible = arr(i, State)
    Next
End Sub

From your forms CODE Page

Rich (BB code):
Private Sub OptionButton2_Click()
  
    ControlsVisible Me, State2
  
End Sub

Private Sub UserForm_Initialize()
    With Sheets("BDATOS")
        n = .Range("B" & Rows.Count).End(xlUp).Row
        va = .Range("G2:G" & n) 'ComboBox1 = By country of destination
        vb = .Range("H2:H" & n) 'ComboBox2 = By destination city
        vc = .Range("B2:B" & n) 'ComboBox3 = By name
    End With
    Set dar = CreateObject("System.Collections.Arraylist")
  
    ControlsVisible Me, State1
End Sub

Solution is untested & you may have to update the visible State of each control in the table as required.
Table can be expanded to include other permutations if needed

Dave
Thank you very much, Dave, as always a pleasure to have you and your always accurate help.
 
Upvote 0
Thank you very much, Dave, as always a pleasure to have you and your always accurate help.

many thanks for generous feedback it is very much appreciated but would add I don't always get it right - do be mindful that whilst not all contributors may produce a working solution they offer help in a generous spirit.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top