Save workbook with Macros

judgejustin

Board Regular
Joined
Mar 3, 2014
Messages
142
I am trying to copy a workbook and save it as a new file. The problem, using the code below, is that it references the macros in the new workbook back to the original. I need the macros copied along with the workbook. Can someone tell me if I am missing something or just going about it completely wrong.
Code:
Sub Create_New_Workbook_Click()
 
    Set NewBook = Workbooks.Add
 
    ThisWorkbook.SaveAs
    ActiveWorkbook.Sheets("Sheet1").Visible = False
    ActiveWorkbook.Sheets("Void Control").Visible = False
    ActiveWorkbook.Sheets("Set-Up Voids").Visible = False
Dim myValue As Variant
myValue = InputBox("Enter Tax Roll Sequence # of the Parcel to be voided.", "Void Sheet Input", 0)
    ActiveSheet.Unprotect "password"
Range("F17").Value = myValue
ActiveSheet.Protect "password"
Dim sFile As String
Dim sPath As String
sFile = Range("F17").Value & ".xlsm"
sPath = "\" & sFile
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sFile, FileFormat:=52

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
that it references the macros in the new workbook back to the original
Could you give an example of what you mean by this statement. I have saved macro enabled files to different directories and the same directory without encountering a problem with the macros referring to anything other than what they were written to apply to. You can open the vb editor click on Tools; Macros to display the macros in your public modules and at the bottom there is a box that tells you which project (Workbook) they apply to. Does that box actually refer back to the original workbook if only the new workbook is open?

As for your code
Code:
sPath = "\" & sFile
is the only thing I noticed that doesn't do anything.
 
Upvote 0
I actually meant to take that line of Code out that you stated doesn't do any thing. It can be disregarded.
When I run this macro the new workbook that is created has other buttons in it that are assigned to other macros. If you click on any of these buttons to run their associated macro it actually opens the original workbook first. When I check to see what macro is assigned to the button it is listed as one stored in the original workbook, not the new workbook where the button is located.
The macros themselves are being copied into the new workbook but the buttons that are linked are still assigned to the original workbook.
 
Upvote 0
You said:
The macros themselves are being copied into the new workbook but the buttons that are linked are still assigned to the original workbook.

Show me the macro script.
 
Upvote 0
This is the Macro that creates a the new workbook.
Rich (BB code):
Sub Create_New_Workbook_Click()
 
    Set NewBook = Workbooks.Add
 
    ThisWorkbook.SaveAs
    ActiveWorkbook.Sheets("Sheet1").Visible = False
    ActiveWorkbook.Sheets("Void Control").Visible = False
    ActiveWorkbook.Sheets("Set-Up Voids").Visible = False
Dim myValue As Variant
myValue = InputBox("Enter Tax Roll Sequence # of the Parcel to be voided.", "Void Sheet Input", 0)
    ActiveSheet.Unprotect "password"
Range("F17").Value = myValue
ActiveSheet.Protect "password"
Dim sFile As String
Dim sPath As String
sFile = Range("F17").Value & ".xlsm"
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\" & sFile, FileFormat:=52

End Sub

There are several macros in the workbook that is created by the above listed one. Here is an example of one of them
Rich (BB code):
Sub Add_Picture_Click()

Dim myFiles, e
    myFiles = Application.GetOpenFilename(, , , , True)
    If Not IsArray(myFiles) Then Exit Sub
    For Each e In myFiles
        With ActiveSheet
        .Protect "password", DrawingObjects:=False, Contents:=True, Scenarios:=True
            ActiveSheet.Shapes.AddPicture (e), False, True, Range("B5:B11").Left, Range("B5:E5").Top, Range("B5:E5").Width, Range("B5:B11").Height
            End With
    Next
End Sub


The button that controls it stays assigned the the macro stored in the original worksheet. If I email the new worksheet to someone else they will have the macro (which is locked) but the button will not work because it is assigned to the macro in the original worksheet which is on my computer.
 
Upvote 0
This link might be what you need.

Break a link to an external reference in Excel - Excel

It is your buttons which are linked to the original worksheet, not the macros. You might need to re-assign the buttons once the links are broken. Haven't gone through the process myself, so don't know for sure.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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