Can i call a macro name from a cell value?

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
2,592
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
HI Everyone,

I have a standard macro i use in lots of places but i find myself making lots of copy's because in it i have the comand "Call" (Another macro) and the macro i want to call can be different .

so i was wondering if i could instead of say "Call" (Another macro) i could list that macros name in sheets "Macros" say "D7" and have it call it instead?

can this be done? if so how?

thanks

Tony
 

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

rehanemis

New Member
Joined
Aug 15, 2016
Messages
47
Why not you make a button to call it every time or just place in the standard module and call it from anywhere in your sheets.
 

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
2,592
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Ok so heres an exaple of the code,

now the thing is i would nornally have to


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AE1:AH500")) Is Nothing Then
If Range("Z" & Target.Row) = "Dropdown" Then
Call TonyDoesGreen1
Exit Sub
End If
End If

'reapeat code

If Not Intersect(Target, Range("AE1:AH500")) Is Nothing Then
If Range("Z" & Target.Row) = "TextBox" Then
Call TonyDoesRed1
Exit Sub
End If
End If

'and there ight be 25 different calls so i make 25 copies of the code
whereas i could put:

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AE1:AH500")) Is Nothing Then
If Range("Z" & Target.Row) = "Use" Then
Call  Range("Y" & Target.Row) .value  @but this is the row i dont know if it will work?
Exit Sub
End If
End If
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
10,981
Office Version
  1. 2019
Platform
  1. Windows
Are all of the macros in the same module?

To do what you want, you need to call the macro by workbook name, module and macro name with something similar to
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("AE1:AH500")) Is Nothing Then
    If Range("Z" & Target.Row) = "Use" Then
        Application.Run "'" & ThisWorkbook.Name & "'!VBAProject." & "Module1." & Range("Y" & Target.Row).Value
        Exit Sub
    End If
End If
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,113,824
Messages
5,544,541
Members
410,619
Latest member
gregor222
Top