If then VBA (multiple) - better way

rdw72777

Well-known Member
Joined
Apr 5, 2005
Messages
723
I know there must be a better way, but i can't quite get the syntax right. I'm sure the array or inlist functions must work but I can't quite figure it out. I want something where I write myTA inlist ("Division A', Division C", etc) so I don't have to have 5-10 "or myTA =...." segments of code....

Code:
if MyTA = "Division A" or myTA ="Division c" or myTA="Division D" or myTA = "Division Q" then
        Sheets(myTA).Visible = False
End If
 
Last edited:

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try like this

Code:
Dim ws As Worksheet
For Each ws In Worksheets(Array("Division A", "Division c", "Division D", "Division Q"))
        ws.Visible = False
End If
 
Upvote 0
Vog, thanks, how would i differentiate this even further. Say if myTA is equal to one of the values I gave (Division A, Division c"...etc), how could I hide a sheet named "Sheet1".

(FYI, myTA is defiend as a variant and is a value set by a named range. So while I might want to hide a sheet name myTA, I'd also like to hide some other sheets based on the value of myTA, btu the other sheets won't have the same name as myTA)
 
Upvote 0
This should work as expected, i use these statements all the time.

If its failing, you need to debug it to see where it is failing. I would guess it would be your var MyTA.
 
Upvote 0
I don't think I understand quite what you are asking. Perhaps you mean something like

Code:
If IsNumeric(Application.Match(myTA, Array("Division A", "Division c", "Division D", "Division Q"), 0)) Then
        Sheets(myTA).Visible = False
End If
 
Upvote 0
Exactly what i needed. I jsut wanted to hide a sheet with a name other than myTA

Code:
If IsNumeric(Application.Match(myTA, Array("Division A", "Division c", "Division D", "Division Q"), 0)) Then
        Sheets("Div by Region").Visible = False
End If


Thanks,
 
Upvote 0

Forum statistics

Threads
1,224,557
Messages
6,179,504
Members
452,917
Latest member
MrsMSalt

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