Pagcaha
Pagcaha

Reputation: 1

VBA Excel: Using IF statement to run a sub routines (from two selections) based on a cell value (ex. "1")

Need an example on how to code a VBA sub routine using IF statement based on a cell value. For example, if value of cell = "1", run Code1 else run Code2

Currently, my workaround is on having two separate buttons to run the two codes. My colleague informed me though that this may be a bad idea moving forward because requests might increase the number of buttons I have to do.

Thanks in advance!

Upvotes: 0

Views: 155

Answers (1)

Dave
Dave

Reputation: 4356

You asked for an example:

Select Case ThisWorkbook.Worksheets.Range("A1")
    Case 1 : myFunction1
    Case 2 : myFunction2
    Case 3 : myFunction3
    Case Else : MsgBox "Sorry, the option provided is not valid"
End Select

Where the value of A1 in the code above is 1, myFunction1 will execute, where it's 2, myFunction2 will run and so on.

The Case Else is used as a final catchall circumstance to handle a scenario you haven't specified directly.

Upvotes: 1

Related Questions