VBA IF with multiple conditions

RedOctoberKnight

Board Regular
Joined
Nov 16, 2015
Messages
152
Office Version
  1. 2016
Platform
  1. Windows
Good Morning,

I'm currently working on a project for work and slowly trying to teach myself VBA in the process. I have a Userform set up and I want to be able to show another textbox based on certain values in a combo box.

I have the following code that I've been experimenting with. I can get it to work for the one condition but I want it to be able to work for all the conditions. I want the text box to appear when Text1, 2 and 3 are selected but hide when text4, 5, and 6 are selected.


VBA Code:
Private Sub ComboBox1_change()

        If ComboBox1 = "TEXT1" Then
               TextBox1.Visible = True
         Else
    TextBox1.Visible = False
    End If
    
  
    
End Sub

Any help would be much appreciated.

Thanks,
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi
try this update to your code & see if does what you want

VBA Code:
Private Sub ComboBox1_change()
    Dim ShowWhenSelected As Variant
    
    ShowWhenSelected = Array("TEXT1", "TEXT2", "TEXT3")
    
    Me.TextBox1.Visible = Not IsError(Application.Match(Me.ComboBox1.Value, ShowWhenSelected, 0))
End Sub

Dave
 
Upvote 0
Works perfectly! Thanks!

If I had a range of what "text" I'm looking for on a settings sheet, would I be able to use that so incase I add or subtract specific text that I'm looking for, it will automatically update?

Thanks,
 
Upvote 0
Works perfectly! Thanks!

If I had a range of what "text" I'm looking for on a settings sheet, would I be able to use that so incase I add or subtract specific text that I'm looking for, it will automatically update?

Thanks,

If you want the array to read from a dynamic range on a worksheet then try this modified code & see if does what you want

VBA Code:
Private Sub ComboBox1_change()
    Dim ShowWhenSelected As Variant
    Dim ws               As Worksheet
    
    Set ws = Worksheets("Sheet1")
    
    ShowWhenSelected = ws.Cells(1, 1).Resize(ws.Cells(ws.Rows.Count, "A").End(xlUp).Row).Value
    
    Me.TextBox1.Visible = Not IsError(Application.Match(Me.ComboBox1.Value, ShowWhenSelected, 0))

End Sub

Change the worksheet name as required

Dave
 
Upvote 0
Solution
If you want the array to read from a dynamic range on a worksheet then try this modified code & see if does what you want

VBA Code:
Private Sub ComboBox1_change()
    Dim ShowWhenSelected As Variant
    Dim ws               As Worksheet
   
    Set ws = Worksheets("Sheet1")
   
    ShowWhenSelected = ws.Cells(1, 1).Resize(ws.Cells(ws.Rows.Count, "A").End(xlUp).Row).Value
   
    Me.TextBox1.Visible = Not IsError(Application.Match(Me.ComboBox1.Value, ShowWhenSelected, 0))

End Sub

Change the worksheet name as required

Dave
My apologies, I realized I forgot to say Thank you!
 
Upvote 0
My apologies, I realized I forgot to say Thank you!
The marked solution has been changed accordingly. In your future questions, please mark the post as the solution that actually answered your question, instead of your feedback message as it will help future readers. No further action is required for this thread.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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