SELECT CASE with a list of strings in CASE expression

boim

Board Regular
Joined
Dec 14, 2009
Messages
54
Hi,

I want to process table columns based on the column name. Some columns will be deleted, some will have its format changed, etc. I try the SELECT statement; however, I also want to make the delimited list in the CASE statement dynamic (not hard-coded).

Ex:
Code:
For Each col in myTable.ListColumns
    Select Case col.Name
        Case ListColName1
            do this...
        Case ListColName2
            do that...
    End Select
Next

The problem is that if I define ListColName as a string of """Name1"",""Name2"",""Name3""", it doesn't work because it's apparently regarded as just one string by VBA instead of a delimited list of strings.

So,
1. How do I go about this?
2. Perhaps a different way of doing the same job?

Thanks
 
Dim ListColName as Variant

ListColName = Array("Name1","Name2","Name3")

then your case statements are like

Case ListColName(0) 'Name1

Case ListColName(1) 'Name2

Case ListColName(2) 'Name3


Hope that helps..
 
Upvote 0
Okay, after posting the problem it just occurred to me to do it the following way, I hope it can also be of help to others:

Code:
For Each col in myTable.ListColumns
    Select Case True
        Case 0 < InStr(1, ListColName1, col.Name, vbTextCompare)
            do this...
        Case 0 < InStr(1, ListColName2, col.Name, vbTextCompare)
            do that...
    End Select
Next

But if anyone has another trick, my second question is still open.

@jonmo:
Thanks, but that won't work because in each Case statement, the ListColName can contain more than one string / column name.
 
Upvote 0

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