Passing a string to Case statement

G

Guest

My current code reads:


Select Case Cells(i, 8)
Case "ABC", "DEF", "GHI"
......
End select

Since I have different of these cases I wonder if it is possible to use a
variable as follows:


Dim CaseVariable as variant
Set CaseVariable= "ABC"
Select Case Cells(i, 8)
Case CaseVariable
......
End select

So far I cannot get this working. Is it possible? Please advise.
 
P

papou

Hello Beertje
Declare your variable as String and may be constant as well:
Const CaseVariable As String = "ABC"
Select Case Cells(i,8)
Case CaseVariable
'....

HTH
Cordially
Pascal
 
G

Guest

For 1 entry no problem but how to get this work for 2 or more like:
"ABC", "DEF", "GHI"

thx.
 
P

papou

Beertje
Dunno if this could be useful in your case but anyway:
Dim CaseVariable(0 To 2)
CaseVariable(0) = "ABC"
CaseVariable(1) = "DEF"
CaseVariable(2) = "GHI"
Select Case Cells(1, 8)
Case CaseVariable(0) To CaseVariable(2)
MsgBox "match found"
End Select

HTH
Cordially
Pascal
 
G

Guest

Got it working.
thx
using
Case casevar(1), casevar(2), casevar(3), casevar(4)
works better, since I can then have also an empty string.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top