Adding Macros from a different excel sheet

haleem100

Board Regular
Joined
Jul 29, 2014
Messages
53
Hi
I need some help with adding macros from a different worksheet to my worksheet
I have few macros that are saved in a different worksheet but I need them to be added to a new worksheet that I have created. Instead of manually adding the macros, is there a easy way to add those macros

Any advise would be appreciated

Thank you
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

scott_n_phnx

Active Member
Joined
Sep 28, 2006
Messages
445
The quick answer is no. But, with that said, I did find a difficult way to do just that. I had a project where I had to add a new worksheet and then add a Worksheet_Change event. Below is the code that I used, in conjunction with the ThisWorkbook module.

Code:
Sub ChangeMacroCode()
Dim sCodeName As String
Dim LineNum As Long
' Get the codename of the newly created sheet
sCodeName = Worksheets("Working Data").CodeName 'new worksheet name

' Write some text in the module
With ActiveWorkbook.VBProject.VBComponents(sCodeName).CodeModule
LineNum = .CountOfLines + 1
    If .CountOfLines > 0 Then .DeleteLines (1)
            .InsertLines LineNum, "Private Sub Worksheet_Change(ByVal Target As Range)"
            LineNum = LineNum + 1
            .InsertLines LineNum, "Application.ScreenUpdating = False"
            LineNum = LineNum + 1
            .InsertLines LineNum, "If Target.Address = ""$C$2"" Then"
            LineNum = LineNum + 1
            .InsertLines LineNum, "        Dim LastRow As Long"
            LineNum = LineNum + 1
            .InsertLines LineNum, "        LastRow = Cells(Rows.Count, 4).End(xlUp).Row"
            LineNum = LineNum + 1
            .InsertLines LineNum, "        Range(""D2"", ""D"" & LastRow).ClearContents"
            LineNum = LineNum + 1
            .InsertLines LineNum, "    Call ListServers"
            LineNum = LineNum + 1
            .InsertLines LineNum, "End If"
            LineNum = LineNum + 1
            .InsertLines LineNum, "If Target.Address = ""$F$2"" Then"
            LineNum = LineNum + 1
            .InsertLines LineNum, "        Dim LastRow As Long"
            LineNum = LineNum + 1
            .InsertLines LineNum, "        LastRow = Cells(Rows.Count, 4).End(xlUp).Row"
            LineNum = LineNum + 1
            .InsertLines LineNum, "        Range(""G2"", ""G"" & LastRow).ClearContents"
            LineNum = LineNum + 1
            .InsertLines LineNum, "    Call ListApps"
            LineNum = LineNum + 1
            .InsertLines LineNum, "End If"
            LineNum = LineNum + 1
            .InsertLines LineNum, "Application.ScreenUpdating = True"
            LineNum = LineNum + 1
            .InsertLines LineNum, "End Sub"
End With
End Sub

Additionally, instead of adding each of the codes, you might consider having the other codes in modules and then modify the above code to just call the other macros as needed.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,598
Messages
5,832,645
Members
430,150
Latest member
amitk1

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
Top