If Condition

H

hlopes

Hi,

I need to run a macro based on the contents of a cell, if cell A1
Apples, then
run macro1, if cell A1 = Oranges, then run macro2, etc.
Can anyone help.

Thanks
(e-mail address removed)
 
K

Kevin

There are two ways you can do this depending on your
preference

The first useses If...then elseif structure

dim fruit as text

fruit=worksheets("sheetname").Range("A1").value

If(fruit="Apples")
Macro1
elseif(fruit="Oranges")
Macro2
elseif(fruit="plums")
Macro3
etc...
end if

the second method would use the if structure only:

If(fruit="Apples")
Macro1
end if

if(fruit="Oranges")
Macro2
endi if

if(fruit="plums")
Macro3
end if

etc...

Both methods will accomplish what you want. These macros
would need to be in the project your working in. If not,
you would need a different method for calling the macro
that involves the Application.Run method. The syntax for
this method is more complicated, but if you need to do
this someone here can help. Just let us know.

Hope that helps!

Kevin
 
G

Guest

hi,
a littl vague but...
you may need a third macro
sub whatif()
if range("A1").value = "Apples" then
call macro1
else
if Range("A1").value = "oranges" then
call macro 2
end if
end if
end sub
 
T

Tom Ogilvy

select Case lcase(Range("A1"))
case "apples" : macro1
case "oranges" : macro2
case "pears" : macro3
end Select
 

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