Calling macro (with/without parameters) from other workbook
Following code runs the macro from other opened workbook. Pass the required parameter in the below code function as required.
Sub RunMacroFromWbk(wbkTarget As Workbook, strProcOrFunc As String, _ Optional arg1, _ Optional arg2, _ Optional arg3, _ Optional arg4, _ Optional arg5) 'This code works for a maximum of 5 parameters If TypeName(arg5) <> "Error" Then Application.Run "'" & wbkTarget.Name & "'!" & strProcOrFunc, arg1, arg2, arg3, arg4, arg5 Exit Sub End If If TypeName(arg4) <> "Error" Then Application.Run "'" & wbkTarget.Name & "'!" & strProcOrFunc, arg1, arg2, arg3, arg4 Exit Sub End If If TypeName(arg3) <> "Error" Then Application.Run "'" & wbkTarget.Name & "'!" & strProcOrFunc, arg1, arg2, arg3 Exit Sub End If If TypeName(arg2) <> "Error" Then Application.Run "'" & wbkTarget.Name & "'!" & strProcOrFunc, arg1, arg2 Exit Sub End If If TypeName(arg1) <> "Error" Then Application.Run "'" & wbkTarget.Name & "'!" & strProcOrFunc, arg1 Exit Sub End If Application.Run "'" & wbkTarget.Name & "'!" & strProcOrFunc End Sub
»
- Vishesh's blog
- Login or register to post comments
- 39069 reads
Recent comments
5 years 36 weeks ago
6 years 22 weeks ago
6 years 34 weeks ago
6 years 37 weeks ago
6 years 38 weeks ago
6 years 43 weeks ago
6 years 52 weeks ago
7 years 2 days ago
7 years 3 days ago
7 years 3 days ago