ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Shorter 'Or' Statement? (https://www.excelbanter.com/excel-programming/410104-shorter-statement.html)

Bigfoot17

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.

Dave Peterson

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

JaimeVasquez

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.

Peter T

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.




JE McGimpsey

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


Peter T

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




Jeff Johnson[_2_]

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.



Peter T

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



JE McGimpsey

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.



All times are GMT +1. The time now is 10:35 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com