Home » excel » excel – How to call a procedure depending on a cell value in VBA

excel – How to call a procedure depending on a cell value in VBA

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have written different procedures on different worksheet in a Workbook. I want to call a particular procedure depending on the specific cell value in a worksheet.

I tried with defining a variable

Example:

Suppose, Worksheet.Range(“C6”)=RC

So i defined a variable ‘ i ‘

i= thisworkbook.sheets("Sheet1").range("C6").value

Call i

but I’m getting errors in this.

value Range C6 is changing as per contents it has array of total 32 contents, so I have written 32 procedures different. i just need to call each procedure depending on the value of ‘C6’.

Its total 32 Procedures, i can write with the IF condition but it will be too bulky so i’m trying for some easy method.

How to&Answers:

Use Application.Run rather than Call:

Say we put the name of a macro in A1, then:

Sub MAIN()
    Dim WhichMac As String
    WhichMac = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
    Application.Run WhichMac
End Sub

Sub MyMac()
    MsgBox "Hello World"
End Sub

enter image description here

MAIN() will call the macro whose name you have placed in A1.