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
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Re: Macro Conditional ececution

Code:
Sub J
    Select Case [A1]
        Case 1: Call Macro1
        Case 5: Call Macro2
    End Select
End Sub
 
Upvote 0
Re: Macro Conditional ececution

I'm not sure whether it is what I need. Any alternatives ?

Thx
 
Last edited:
Upvote 0
Re: Macro Conditional ececution

Did you actually try to implement it? If so, show us your implementation and tell us what problems you're having.
 
Upvote 0
Re: Macro Conditional ececution

Hi everyone

I would like to use (run) below code only when I click on button. How can I do it ?

Thanks

Code:
Private Sub Worksheet_Change(ByVal Target As Range)If Not Intersect(Target, Range("A1")) Is Nothing Then
    Select Case Target.Value
    Case 2
        Call FirstMacro
    Case 4
        Call SecondMacro
    'add in all the cases you require
    End Select
End If
End Sub
 
Upvote 0
Re: Macro Conditional ececution

I can't see how your code is related to the subject of this thread. The code you've posted is for a Worksheet_Change event. No buttons are involved. The use of buttons implies to use of a userform, but you give no indication that one exists. Of course, you could add a MsgBox to the existing code, so it only executes if a 'Yes' answer is given. For example:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
    If MsgBox("Execute?", vbYesNo) = vbNo Then Exit Sub
    Select Case Target.Value
    Case 2
        Call FirstMacro
    Case 4
        Call SecondMacro
    'add in all the cases you require
    End Select
End If
End Sub
 
Upvote 0
Re: Macro Conditional ececution

yes, sorry. It is because I don't know how to change it/do it....

I would like to assign a above (similar) code to the button and after click on button above (similar) code should be run.

I hope it is clear

Thanks
 
Upvote 0
Re: Macro Conditional ececution

Did you at least try the code I posted??? There is little point people answering your questions then you coming back (as you've done twice now in this thread) asking for alternatives or saying you don't know how to make the changes that have been provided in full.
 
Upvote 0
Re: Macro Conditional ececution

I never answer without check sth before. Thank you for your reply but as I said this is not exactly what I need.

I would like to assign conditionally executing macro to the button.

Right now as you mentioned it is Worksheet_Change event but I need to change it into button click event.

Thanks
 
Upvote 0
Re: Macro Conditional ececution

And where is that button??? What kind of button is it??? Does it have a name??? What are the conditions that have to be met for it to call the dependent macros? You really haven't given any context one could possibly make meaningful use of for answering this...
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,254
Members
448,556
Latest member
peterhess2002

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