VBA Code works but crashes excel

turbo805

New Member
Joined
Oct 24, 2016
Messages
22
Hello,

Briefly I created a macro that saves my workbook as two versions--a macro enabled version and a non-macro enabled version.

The macro works correctly.

HOWEVER, it crashes my excel every time I run it.
Also as a background, my code saves the second workbook as the same name but with "DASHBOARD" added to the title. Hence the IF statement in my code.

Really what i'm looking for is a fix to the crashing and why my small brain cannot figure it out. I believe it has something to do with this line--wb.SaveAs (Path & WorkbookName).
Cheers!

Code:
Sub SaveWorkbook()

Dim wb As Workbook, wb2 As Workbook
Dim Path As String
Dim WorkbookName As String

WorkbookName = ActiveWorkbook.Name
Application.DisplayAlerts = False
Path = "C:\Users\" & Environ("Username") & "\Documents\"
Set wb = ThisWorkbook
wb.SaveAs (Path & WorkbookName)

If InStr(WorkbookName, ".") > 0 Then
   WorkbookName = Left(WorkbookName, InStr(WorkbookName, ".") - 1)
End If

Set wb2 = Workbooks.Open(Path & WorkbookName)
wb2.SaveAs Path & WorkbookName & " " & "DASHBOARD.xlsx", xlOpenXMLWorkbook
wb2.Close
Application.DisplayAlerts = True


End Sub
 
Last edited:
One way
Code:
Sub SaveWorkbook()
    Dim wb As Workbook, wb2 As Workbook
    Dim Path As String, WorkbookName As String, FName1 As String, FName2 As String

    WorkbookName = ActiveWorkbook.Name
    Path = "C:\Users\" & Environ("Username") & "\Documents\"
    Set wb = ThisWorkbook
    wb.Save                                           'save original

    FName1 = Path & "Copy of " & WorkbookName
    wb.SaveCopyAs (FName1)                            'save copy

    If InStr(WorkbookName, ".") > 0 Then
        WorkbookName = Left(WorkbookName, InStr(WorkbookName, ".") - 1)
    End If

    FName2 = Path & WorkbookName & " " & "DASHBOARD.xlsx"

    Set wb2 = Workbooks.Open(FName1)
    Application.DisplayAlerts = False
    wb2.SaveAs FName2, xlOpenXMLWorkbook              'use copy to save macro-free workbook
    wb2.Close
    VBA.Kill FName1                                   'delete copy
    Application.DisplayAlerts = True
End Sub

Thank you!
 
Upvote 0

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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