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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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.
 
Upvote 0

Forum statistics

Threads
1,213,501
Messages
6,114,010
Members
448,543
Latest member
MartinLarkin

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