Call a macro from a cell

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
do you mean upon selecting the cell? Or if Cell Value = X?

Either way is possible, but clarify please.
 
Upvote 0
Thanks for the reply, I need to link a product code to a product. I would normally would use nested if functons in excel, however excel only allows for seven. If the user selects 1/2 Reg from cell A2, then I need GB4080 to come up in cell B2. I can do this with macros but I'm trying to make it automatic instead of having the user call the macro through a button, ect....Thanks again, Leo...
 
Upvote 0
Ok, so the question is, do you want the macro to run upon a specific cell's VALUE changing to a specific value? If so, which cell and what value?


Or would you like the macro to run simply after selecting a specific cell. If so, which cell?
 
Upvote 0
Ok, I see 3 different ways to do it.

1. If User simply selects specific cell, run macro
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Range("a1"), Target) Is Nothing Then Exit Sub
Application.Run "mymacro"
End Sub

2. If specific cell value changes to specific value VIA User Entry
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("a1"), Target) Is Nothing Then Exit Sub
If Range("a1").Value = "X" Then Application.Run "mymacro"
End Sub

3. If specific cell value changes to specific value VIA Formula
Code:
Private Sub Worksheet_Calculate()
If Range("a1").Value = "X" Then Application.Run "mymacro"
End Sub


All 3 examples use Cell A1
All 3 examples use "mymacro" as the name of the macro
Examples 2 and 3 use "X" as the value to trigger macro

adjust as needed

All 3 codes should be pasted into the Worksheet Code
 
Upvote 0
Hi

While I'm sure jonmo's solution is far more elegant and efficent it is possible to have more than 7 nested IF statements. After your first 7 have been completed with all the )) closed off just add another IF statement joined by the & symbol. This may be easier in some cases where you want to add another 2 or 3 conditions to your original statement.

ie: =IF(B1=1,"A",IF(B1=2,"B",IF(B1=3,"C",IF(B1=4,"D",IF(B1=5,"E",IF(B1=6,"F",IF(B1=7,"G","")))))))&IF(B1=8,"H",IF(B1=9,"I",""))
 
Upvote 0
Sweeeeeet!!!!

I've looked and looked and looked for a way to do that.

Nice.

The only drawback is that makes the result text. Which can complicate things if you need numbers....That can be resolved like this
Code:
=--(IF(B1=1,"1",IF(B1=2,"2",IF(B1=3,"3",IF(B1=4,"4",IF(B1=5,"5",IF(B1=6,"6",IF(B1=7,"7","")))))))&IF(B1=8,"8",IF(B1=9,"9","")))
 
Upvote 0
Thanks for the reply, I need to link a product code to a product. I would normally would use nested if functons in excel, however excel only allows for seven. If the user selects 1/2 Reg from cell A2, then I need GB4080 to come up in cell B2. I can do this with macros but I'm trying to make it automatic instead of having the user call the macro through a button, ect....Thanks again, Leo...

Why not use VLOOKUP?
 
Upvote 0

Forum statistics

Threads
1,216,129
Messages
6,129,055
Members
449,484
Latest member
khairianr

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