Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 67
Default Shorter 'Or' Statement?

I have the following line working, but it seems that perhaps the line could
be shortened:
If Range("B" & CurRow) = "X" Or Range("B" & CurRow) = "x" Or Range("B" &
CurRow) = "O" Or Range("B" & CurRow) = "o" Then
If I needed to add a few more acceptable characters would I continue adding
more 'or' statements or can I try another method?

Thanks.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Shorter 'Or' Statement?

select case lcase(activesheet.range("B" & currow).value)
case is = "x","o"
'do something
case else
'do something else
end select



Bigfoot17 wrote:

I have the following line working, but it seems that perhaps the line could
be shortened:
If Range("B" & CurRow) = "X" Or Range("B" & CurRow) = "x" Or Range("B" &
CurRow) = "O" Or Range("B" & CurRow) = "o" Then
If I needed to add a few more acceptable characters would I continue adding
more 'or' statements or can I try another method?

Thanks.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Shorter 'Or' Statement?

On Apr 28, 9:34 am, Bigfoot17
wrote:
I have the following line working, but it seems that perhaps the line could
be shortened:
If Range("B" & CurRow) = "X" Or Range("B" & CurRow) = "x" Or Range("B" &
CurRow) = "O" Or Range("B" & CurRow) = "o" Then
If I needed to add a few more acceptable characters would I continue adding
more 'or' statements or can I try another method?

Thanks.


Hi, try with something like this:

If UCase$(Range("B" & CurRow)) Like "[ABCDEFG]" Then MsgBox "ABCDEFG"

HTH

Saludos,
Jaime Vasquez.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Shorter 'Or' Statement?

With your example you could reduce your Or's if you use UCase.
s = UCase(Range("B" & CurRow))

Also faster to assign the value to a variable rather than reading the cell
multiple times.

Maybe 'Like' might work for you

If Range("B" & CurRow) Like "[OXox]" then
or
If Ucase("B" & CurRow) Like "[OX]" then

Regards,
Peter T

"Bigfoot17" wrote in message
...
I have the following line working, but it seems that perhaps the line

could
be shortened:
If Range("B" & CurRow) = "X" Or Range("B" & CurRow) = "x" Or Range("B" &
CurRow) = "O" Or Range("B" & CurRow) = "o" Then
If I needed to add a few more acceptable characters would I continue

adding
more 'or' statements or can I try another method?

Thanks.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Shorter 'Or' Statement?

Small correction:

If UCase(Range("B" & CurRow).Text)) Like "[OX]" Then

or, equivalently:

If UCase(Cells(CurRow, 2).Text)) Like "[OX]" Then

I wonder if the added function overhead makes that form less efficient
than the explicit "[OXox]" version...



In article ,
"Peter T" <peter_t@discussions wrote:

Maybe 'Like' might work for you

If Range("B" & CurRow) Like "[OXox]" then
or
If Ucase("B" & CurRow) Like "[OX]" then



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Shorter 'Or' Statement?

Not sure if better to return the .Text vs the .Value property, might depend
on the scenario. Off the top of my head, with a single alpha character in a
cell I can't think what might make them different.

I wonder if the added function overhead makes that form less efficient
than the explicit "[OXox]" version...


Would need to test, as a guess, with a small set of characters probably not
worth using UCase, perhaps a payback comes with a longer set.

Regards,
Peter T

"JE McGimpsey" wrote in message
...
Small correction:

If UCase(Range("B" & CurRow).Text)) Like "[OX]" Then

or, equivalently:

If UCase(Cells(CurRow, 2).Text)) Like "[OX]" Then

I wonder if the added function overhead makes that form less efficient
than the explicit "[OXox]" version...



In article ,
"Peter T" <peter_t@discussions wrote:

Maybe 'Like' might work for you

If Range("B" & CurRow) Like "[OXox]" then
or
If Ucase("B" & CurRow) Like "[OX]" then



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default Shorter 'Or' Statement?

Only reason I use .Text vs .Value is that the .Text property returns a
String, which is the proper input argument type for the Like operator,
whereas .Value returns a Variant which needs to be (either explicitly or
implicitly) converted.

OTOH, I don't know what I'd do with all those saved nanoseconds...


In article ,
"Peter T" <peter_t@discussions wrote:

Not sure if better to return the .Text vs the .Value property, might depend
on the scenario. Off the top of my head, with a single alpha character in a
cell I can't think what might make them different.

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default Shorter 'Or' Statement?

"Bigfoot17" wrote in message
...

I have the following line working, but it seems that perhaps the line could
be shortened:
If Range("B" & CurRow) = "X" Or Range("B" & CurRow) = "x" Or Range("B" &
CurRow) = "O" Or Range("B" & CurRow) = "o" Then
If I needed to add a few more acceptable characters would I continue
adding
more 'or' statements or can I try another method?


To touch on a different issue, you should note that every single time you
have Range("B" & CurRow) written, it will be evaluated. Therefore it's
evaluated FOUR times in your statement above. You can save a little time by
doing something like this:

Dim CellValue As String

CellValue = Range("B" & CurRow).Value

If CellValue = "blah" Or CellValue = "Yada" Or ...<etc.


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,600
Default Shorter 'Or' Statement?

"Jeff Johnson" wrote in message
news:qZadncgnSLv3h4vVnZ2dnUVZ_qGknZ2d@datapex...
"Bigfoot17" wrote in message
...

I have the following line working, but it seems that perhaps the line

could
be shortened:
If Range("B" & CurRow) = "X" Or Range("B" & CurRow) = "x" Or Range("B" &
CurRow) = "O" Or Range("B" & CurRow) = "o" Then
If I needed to add a few more acceptable characters would I continue
adding
more 'or' statements or can I try another method?


To touch on a different issue, you should note that every single time you
have Range("B" & CurRow) written, it will be evaluated. Therefore it's
evaluated FOUR times in your statement above. You can save a little time

by
doing something like this:

Dim CellValue As String

CellValue = Range("B" & CurRow).Value

If CellValue = "blah" Or CellValue = "Yada" Or ...<etc.


Indeed reading cells is relatively slow and better to assign it's value to a
variable for repeated use (I also suggested same in previous post).

However it's worth noting that reading a cell's value (or text property)
does not "evaluate" the cell. If you need to do that, and you might if
calculation is set to manual, need either to force a calculation (range,
sheet wb or full) or do -

cellValue = application.evaluate(Range("B" & CurRow).Value)

Regards,
Peter T


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
Shorter way Oldjay Excel Discussion (Misc queries) 1 May 5th 08 05:12 PM
Shorter Code Sandy Excel Programming 2 July 7th 07 08:47 PM
Shorter Sub Pete Excel Programming 7 April 9th 05 01:22 AM
Shorter Formula Pete Excel Discussion (Misc queries) 1 February 18th 05 03:37 PM
shorter way? Me ![_3_] Excel Programming 0 September 15th 03 06:12 PM


All times are GMT +1. The time now is 06:26 PM.

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"