View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
JE McGimpsey JE McGimpsey is offline
external usenet poster
 
Posts: 4,624
Default Comparing Cell Contents using a Case statement

Use a series of If...Then's instead. I.e., if your current comparison is:


Select Case foo
Case Is = "apple"
Msgbox "It's an apple"
Case Is = "pear"
Msgbox "It's a pear"
Case Else
Msgbox "It's some other fruit"
End Select

you can use

If foo Like "*apple*" Then
MsgBox "It's an apple"
ElseIf foo Like "*pear*" Then
MsgBox "It's a pear"
Else
MsgBox "It's some other frult"
End If

This has the disadvantage of evaluating the argument at each If/Elseif,
but it allows you to use the Like operator.


In article ,
"todd" wrote:

I am writing a macro that will compare cell contents, using a case statement.
Does a method exist where a comparison can be made on a portion of the cell
contents? For example, I might want to act on a cell that contains the word
"apple". The caveat is that "apple" is not the only thing in the cell
string. I tried to use "like" and "is", but received a compile error -
apparently "like" and "is" are not valid comparison operators. What else
could I try?