VBA: Use IF/OR to validate multiple String values

zero269

Board Regular
Joined
Jan 16, 2023
Messages
219
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

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
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,214,586
Messages
6,120,402
Members
448,958
Latest member
Hat4Life

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