Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 177
Default Comparing Cell Contents using a Case statement

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Comparing Cell Contents using a Case statement

if lcase(cell.Value) like "*apple*" then


from the immediate window:

ActiveCell.Value = "Johnny Appleseed"
? lcase(activecell.Value) Like "*apple*"
True
--
Regards,
Tom Ogilvy



"todd" wrote in message
...
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?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 441
Default Comparing Cell Contents using a Case statement

This doesn't use a Select...Case, but it works and may get you going:

Private Function CellCompare()

Dim rCell As Range
Dim sCheck As String
sCheck = "Apple"
For Each rCell In ActiveSheet.UsedRange.Cells
If UCase(rCell.FormulaR1C1) Like "*" & UCase(sCheck) & "*" Then
MsgBox rCell.Address & " contains the word apple!"
End If
Next rCell
End Function

HTH/

"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?

  #4   Report Post  
Posted to microsoft.public.excel.programming
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?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Comparing Cell Contents using a Case statement

Sorry, didn't read clearly:

Sub ABC()
l = "Johhny Appleseed"
Select Case True
Case InStr(1, l, "apple", vbTextCompare)
MsgBox "apple"
Case InStr(1, l, "pear", vbTextCompare)
MsgBox "Pear"
End Select


End Sub

--
Regards,
Tom Ogilvy


"Tom Ogilvy" wrote in message
...
if lcase(cell.Value) like "*apple*" then


from the immediate window:

ActiveCell.Value = "Johnny Appleseed"
? lcase(activecell.Value) Like "*apple*"
True
--
Regards,
Tom Ogilvy



"todd" wrote in message
...
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?





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Case Statement Format for getting cell value from a worksheet George Excel Discussion (Misc queries) 1 May 11th 10 09:22 PM
Comparing cell contents via VB RajenRajput1 Excel Discussion (Misc queries) 5 June 29th 07 05:37 PM
Comparing Cell Contents in 2 or More Columns ConfusedNHouston Excel Discussion (Misc queries) 2 June 18th 07 03:08 PM
how do I format a cell to display its contents in Upper case? MarcM Excel Discussion (Misc queries) 1 March 8th 07 03:19 AM
Comparing cell contents with different reference cells Martin B Excel Worksheet Functions 3 November 22nd 06 07:10 PM


All times are GMT +1. The time now is 09:25 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"