create macros that uses if-then-else statments

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

Guest

I am new to using Macros and am looking for info on creating macros which will allow me to use if-then-else statements based on set variables and then print out the results of the conditional statement. For example: if content of cell A1=var1, print statement1, elseif content cell A1= var2 print statement2, elseif content cell A1= var3 print statement3 and so on up to var12. All content will be text If someone could point me in the right direction to learn about this it would be greatly appreciated, Thanks, Ross K.
 
Ross,

All one line for each (be careful of word wrap):

If Range("A1") = var1 Then Worksheets("print statement 1").printout
copies:=1

John

Ross K said:
I am new to using Macros and am looking for info on creating macros which
will allow me to use if-then-else statements based on set variables and then
print out the results of the conditional statement. For example: if
content of cell A1=var1, print statement1, elseif content cell A1= var2
print statement2, elseif content cell A1= var3 print statement3 and so on up
to var12. All content will be text If someone could point me in the right
direction to learn about this it would be greatly appreciated, Thanks, Ross
K.
 
If Then statements are pretty straight foreward.
If Worksheets(1).Cells(1, 1).Value = “Hello World!” Then
MsgBox “Hello World!”
End If

But for what you’re asking about, use a Select Case statement instead.

Select Case Worksheets(1).Cells(1, 1).Value
Case1 “Hello World!”
MsgBox “Hello World!”
Case2 “Goodnight sweet prince.”
MsgBox “Hamlet”
Case3 etc…etc…etc…

End Select

VBA Help should have enough info to take it from here, but let us know
if it doesn't.

- Pikus
 
Ross
I would use a Select Case construct with that many variables. HTH
Otto
Ross K said:
I am new to using Macros and am looking for info on creating macros which
will allow me to use if-then-else statements based on set variables and then
print out the results of the conditional statement. For example: if
content of cell A1=var1, print statement1, elseif content cell A1= var2
print statement2, elseif content cell A1= var3 print statement3 and so on up
to var12. All content will be text If someone could point me in the right
direction to learn about this it would be greatly appreciated, Thanks, Ross
K.
 
Otto,

I agree in principal but sometimes it seems easier (at least for me)
if you can get it into a number of one liners.

Even with 12 variables, if the OP is doing only one thing as a result
of the "IF", then 12 lines of

IF This Then Do That

might be even simpler than a Select Case.

Of course it's all up to the individual users preferences though.

John
 
Back
Top