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
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

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.
 

Watch MrExcel Video

Forum statistics

Threads
1,108,614
Messages
5,523,902
Members
409,542
Latest member
Shezz01

This Week's Hot Topics

Top