VBA: Use IF/OR to validate multiple String values

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm looking to see if there is a way to combine multiple string values to validate against in a more concise way than I am currently.
I use a macro that clears all filters. However, there are three Tables in particular that I often want to Sort afterwards.
Maybe I can use an array with those three string values. Something that says IF the TableName is any of the following: ("tblBooks", "tblStudents", "tblMSBs") Then Sort
Although I can achieve these results with the following, I was hoping I didn't have to list them three times:
VBA Code:
Sub ClearFilters()
   
    'Clear Filters
    On Error Resume Next
    ActiveSheet.ShowAllData
   
    'Sort by Title
    Dim TableName As String
    TableName = ActiveCell.ListObject.Name
    If TableName = "tblBooks" Or _
            TableName = "tblStudents" Or _
            TableName = "tblMSBs" Then
        SortTitle 'Calling Sub
        ActiveWindow.ScrollRow = 1
        ActiveSheet.ListObjects(1).HeaderRowRange(1).Select
    End If
   
End Sub

Thank you,
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
This doesn't apply to my main topic. Just wanted to add that I moved the following after End If
VBA Code:
ActiveWindow.ScrollRow = 1
ActiveSheet.ListObjects(1).HeaderRowRange(1).Select
This way the window still scrolls back to the top and the first Header cell is selected.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

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