VBA Variable

N

Newbie

Hi,

I am trying to run a routine based on the value in a cell, for example A1
might contain RED, BLUE or GREEN.
In the VBA I would have

x = Range("A1") .value
goto x
end

RED:
'red routine here
end
BLUE:
'blue routine here
end
GREEN
'green routine here
end

....but it doesn't work, any help much appreciated.
 
J

Jacob Skaria

Use SELECT/CASE as below..



Select Case Range("A1")

Case "RED"
'Code for Red

Case "BLUE"
'Code for Blue

Case "GREEN"
'Code for green

Case Else
'Code if else

End Select

If this post helps click Yes
 
M

Mike H

Hi,

Try this

Sub Sonic()
Select Case UCase(Sheets("Sheet1").Range("A1").Value)
Case "RED"
GoTo RED
Case "BLUE"
GoTo BLUE
Case "GREEN"
GoTo GREEN
Case Else
MsgBox "No Match"
Exit Sub
End Select
RED:
'Do RED things
Exit Sub

BLUE:
'Do BLUE Things
Exit Sub

GREEN:
'Do GREEN Things

End Sub

Mike
 
N

Newbie

Perfect, thanks Jacob

Jacob Skaria said:
Use SELECT/CASE as below..



Select Case Range("A1")

Case "RED"
'Code for Red

Case "BLUE"
'Code for Blue

Case "GREEN"
'Code for green

Case Else
'Code if else

End Select

If this post helps click Yes
 

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