vba using the OR operator

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Here is my code that does not work:

If Rows(iRow, 1) = ("11/13/2005" Or "11/23/2005") Then
blah blah

I can get the code to work on a single date but not with multiple dates
using "or". I have about 20 dates that I want to check and I could get by
with just repeating the code 20 times but I wanted to do it using a single
statement.

Thank you!!
 
You need to do:

If Rows(iRow, 1) = "11/13/2005" Or Rows(iRow, 1) = "11/23/2005" Then


RBS
 
Monda,

Try it this way:

If Rows(iRow,1).value = #11/13/2005# or Rows(iRow,1).value = #11/23/2005" Then
'* YOUR CODE HERE *
End if
 
Thank you Vergel for reply but I am getting error on this code:

If Rows(iRow, 1).Value = "11/13/2005" Or Rows(iRow, 1).Value = "11/23/2005"
Then

**code**
end if
 
Firstly, Rows(iRow,1) won't work. Do you mean Cells(iRow,1)?

You could put all the dates in an array and then loop the array, e.g.

Dim bFlag As Boolean
myArray = Array(#11/15/2005#, #11/23/2005#)
For iCt = 0 To UBound(myArray)
If Cells(iRow, 1) = myArray(iCt) Then
bFlag = True
Exit For
End If
Next iCt
If bFlag = True Then
'do something
End If

Hth,
Merjet
 
Should it not be:

If Cells(iRow, 1).Value

RBS


Mona said:
Thank you Vergel for reply but I am getting error on this code:

If Rows(iRow, 1).Value = "11/13/2005" Or Rows(iRow, 1).Value =
"11/23/2005"
Then

**code**
end if
 
Try using the Cells collection instead of Rows. So, use

Cells(iRow, 1)

instead of

Rows(iRow, 1)
 
sub ifcells()
If cells(iRow, 1) ="11/13/2005" Or cells(irow.1)="11/23/2005") Then msgbox
"ok"
end sub
 
The Rows property does not take multiple arguments. If you want to search
all cells in each entire row, try the following:

If Not Range(Rows(1), Rows(iRow)).Find(DateValue("11/13/2005")) Is Nothing
Or _
Not Range(Rows(1), Rows(iRow)).Find(DateValue("11/23/2005")) Is Nothing Then

....Your code Here....

End If

Note also that the example above assumes you are searching for actual dates.
Your original example shows a search for a strings "11/13/2005" and
"11/23/2005." If you really want to search for the string, delete the
DateValue characters from the example.
 

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

Back
Top