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:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
My small brain figured out that it is not actually crashing. Both workbooks are being closed which results in a grey empty screen in excel because no files are open :LOL:.

On that note, does anybody have a fix in my code to keep my original workbook open?
 
Upvote 0
As soon as you performed a SaveAs. Newname, your original workbook was no longer open.
You'll need to reopen the file. Capture that name at the begging or your macro and then reopen it as the last step.
 
Upvote 0
Thanks for the reply. I was thinking the same thing but was still unable to execute it properly.

Here's what I tried.
Code:
Sub SaveWorkbook()

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


WorkbookName = ActiveWorkbook.Name
OriginalName = 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 Version.xlsx", xlOpenXMLWorkbook
wb2.Close
Application.Workbooks.Open (Path & OriginalName)
Application.DisplayAlerts = True


End Sub
 
Upvote 0
.
Code:
[COLOR=#000000][FONT=Consolas] Workbooks.Open Filename:=Path & [/FONT][/COLOR][COLOR=#333333]WorkbookName[/COLOR][COLOR=#000000][FONT=Consolas]
[/FONT][/COLOR]
 
Upvote 0
Try this:

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

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

    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
    wb2.Close
    Application.DisplayAlerts = True
End Sub
 
Upvote 0
Code:
[COLOR=#000000][FONT=Consolas] Workbooks.Open Filename:=Path & [/FONT][/COLOR][COLOR=#333333]WorkbookName[/COLOR][COLOR=#000000][FONT=Consolas]
[/FONT][/COLOR]

This didn't work for me for some reason. I believe it's because i'm editing the original name with my IF statement in my code.
 
Upvote 0
This looks good rlv. The only problem is that it's now saving my original file as a copy which won't work as I'm using this Macro button daily. Any work around to not saving it as a copy?
 
Last edited:
Upvote 0
The only problem is that it's now saving my original file as a copy which won't work as I'm using this Macro button daily. Any work around to not saving it as a copy?

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
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,287
Members
449,149
Latest member
mwdbActuary

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