VBA to say IF cell = any one of 3 values,then do something

P

PCLIVE

I'm drawing a blank on this one.

I need an IF statement in VBA that basically says, IF range("A1").value =
"name1" or "name2" or "Name3" then DoSomething.

The DoSomething code will be the same as long as A1 matches one of the three
names.
Any help?

Thanks,
Paul

--
 
J

JE McGimpsey

Simplest, perhaps:

With .Range("A1")
If (.Value) = "name1" Or (.Value = "name2") Or _
(.Value = "name3") Then
DoSomething
End If
End With
 
G

Guest

If Range("A1").Value = "name1" Or Range("A1").Value = "name2" OR
Range("A1").Value = "name3" Then

Else

End If




If this posting was helpful, please click on the Yes button.
Regards,

Michael Arch.
 
G

Guest

A couple of possibles...

With Range("A1")
if .Value = "name1" or .Value = "name2" or .Value = "name2" then
'do something
end if

OR

Select Case Range("A1").Value
Case "name1", "name2", "name3"
'do Something
End Select
 
B

Bill Renaud

If "Name1", "Name2", and "Name3" refer to named ranges ("Name1" refers
to cell $D$5, for example), then you might need something like the
following:

If Range("A1") = Range("Name1") Or _
Range("A1") = Range("Name2") Or _
Range("A1") = Range("Name3") _
Then
MsgBox "A1 is equal."
Else
MsgBox "A1 is not equal."
End If
 
P

PCLIVE

Thanks JE,

I had to remove the period before "Range"...but other than that, it does the
trick.

Thanks again,
Paul


--
 
G

Guest

So you can't write an unqualified range even when testing... I thought it was
just me! :)
 
D

Dana DeLouis

Just different if the names do in fact follow a numbering pattern.

Sub Demo()
Select Case UCase([A1].Value)
Case "NAME1" To "NAME3"
MsgBox "Doing Something"
End Select
End Sub
 

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

Top