how to save a macro workbook but as xlsx file

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
626
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Is there a way to save a Macro Workbook as .xlsx file? I found the following code online but when I open the converted .xlsx file, it tries to run the macros but then gives an error message "Sub function not defined" when sheet 1 is activated. This sheet does have code assigned to it. I don't want to set a password; all I want is for the user to ONLY view the file. Thanks.

VBA Code:
Sub BackupAsXLSX()

Dim swb As Workbook: Set swb = ThisWorkbook

Dim sFilePath As String: sFilePath = swb.FullName
Dim DotPosition As Long: DotPosition = InStrRev(sFilePath, ".")
Dim dFilePath As String: dFilePath = Left(sFilePath, DotPosition) & "xlsm"
    
Application.ScreenUpdating = False

swb.Sheets.Copy
Dim dwb As Workbook: Set dwb = ActiveWorkbook
Application.DisplayAlerts = False
dwb.SaveAs Filename:=dFilePath, FileFormat:=xlOpenXMLWorkbook
Application.DisplayAlerts = True
dwb.Close savechanges:=False

Application.ScreenUpdating = True

MsgBox "Excel Backup File Created with ( .xlsx) Extension.", vbInformation

End Sub
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
By definition, "xlsx" files are macro/vba free files and CANNOT contain any VBA.
If you want to keep the VBA code in there, you will need to save as an "xlsm" or "xlsb" file.
If you want to save as an "xlsx", you will need to get rid of the VBA code and any General VBA modules you created in the file first.
 
Upvote 0
When sheet1 is activated, it tries to run the ActiveX code and the Worksheet_Activate Code.
VBA Code:
Private Sub CommandButton1_Click() 'ActiveX button
    Call ImportMostRecentFile 'ActiveX Code
End Sub

Private Sub Worksheet_Activate()
  
    Call Sheet1_Activate

End Sub
 
Upvote 0
That can't be happening in an xlsx file since that code won't be in it.
 
Upvote 0

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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