Macro Conditional execution

beczer

New Member
Joined
Nov 21, 2016
Messages
49
Hi everyone

Question. How to run a macro based on some conditions.

Example:

If A1 = X then (when I click run macro - button) use Macro 1
If A1 = Y then use Macro 2



Thanks

Tom
 
Re: Macro Conditional ececution

Button Name: 'button1'
Sheet1

Conditions that have to be met:
Code:
If Not Intersect(Target, Range("A1")) Is Nothing Then    Select Case Target.Value
    Case 2
        Call FirstMacro
    Case 4
        Call SecondMacro
    End Select
End If
End Sub

So as I said when I click on button then above code should be run, depending of value in A1

Thanks
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Re: Macro Conditional ececution

Conditions that have to be met:
Code:
If Not Intersect(Target, Range("A1")) Is Nothing Then    Select Case Target.Value
    Case 2
        Call FirstMacro
    Case 4
        Call SecondMacro
    End Select
End If
End Sub

So as I said when I click on button then above code should be run, depending of value in A1
That code, as it stands, is not appropriate for a macro run from a button. I can only guess what you actually want. Perhaps:
Code:
Private Sub Button1_Click()
If ActiveCell.Address = "$A$1" Then
    Select Case ActiveCell.Value
    Case 2
        Call FirstMacro
    Case 4
        Call SecondMacro
    Case Else
    End Select
End If
End Sub
The above code will only run the dependent macros from the button when A1 is the active cell on the sheet on which the button is located.
 
Upvote 0
Re: Macro Conditional ececution

okay, so you totally don't understand me. Anyway thanks for you help.

Above solution is unuseful for me. I still need to find a way how to conditionally execute macro assigned to button.

Button Click -> action
If A1 = text1 -> run macro1
If A1 = text2 -> run macro2
 
Upvote 0
Re: Macro Conditional ececution

Since you've never said what the conditions are, how can you expect anyone to know what you want. All anyone has had to go on is the precious little information you posted, which has an explicit Select Case test with two specific conditions that have been coded for. The problem is of your making and its now in your hands to resolve. I'm done with this.
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top