Excel crashing when closing workbook in VBA

jbenfleming

New Member
Joined
Mar 30, 2017
Messages
34
When I run this code, Excel will crash when it reaches the last line, i.e where I close "document2". Any thoughts? I've used similar code before and it has always worked.

Code:
Option Explicit


Sub DAILYSALES_END()
Dim rownum As Double


'save document1 at temporary location
    Application.DisplayAlerts = False
    document1.SaveAs "tempdoc4salesmacro.xlms"
    Application.DisplayAlerts = True
    Workbooks.Open ("file path for document2")
    Windows("tempdoc4salesmacro.xlms").Activate
    
'move sheet to new document, change sheet name
    Sheets("BLANK").Select
    Sheets("BLANK").Move Before:=Workbooks("document2").Sheets(1)
    Range("B2").Select
    ActiveSheet.Name = Format(Date, "ddmmm")
    ActiveWorkbook.Save
    
'clear out data from first document
    DoEvents
    Workbooks.Open ("document1")
    Range("B2").Select
    If Range("B2").Value <> "" And Range("B3").Value = "" Then
        Range("B2:J2").Clear
    ElseIf Range("B2").Value <> "" And Range("B3").Value <> "" Then
        Selection.End(xlDown).Select: rownum = ActiveCell.Row
        Range(Cells(2, 2), Cells(rownum, 10)).Clear
    End If
    
       Range("D2:D30").Select
    Selection.Style = "Currency"
    Range("H2:H30").Select
    Selection.Style = "Percent"
    Selection.NumberFormat = "0.00%"
    Range("B2:J47").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    
    
    Range("B2").Select
'save and close document1
    ActiveWorkbook.Save
    ActiveWorkbook.Close


'save and close document2
    ActiveWorkbook.Save
    ActiveWorkbook.Close



End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
I think you're closing the workbook where the macro resides before the macro is finished. It then tries to finish the macro and gives you an error.

It would be clearer if you set your Workbook references as in below. I don't understand the line marked in red

Code:
Sub DAILYSALES_END()
Dim rownum As Double
Dim WB1 As Workbook
Dim WB2 As Workbook
Dim WB3 As Workbook


'save document1 at temporary location
    Application.DisplayAlerts = False
    document1.SaveAs "tempdoc4salesmacro.xlms"
    Set WB1 = ActiveWorkbook
    Application.DisplayAlerts = True
    Workbooks.Open ("file path for document2")
    Set WB2 = ActiveWorkbook
    Windows("tempdoc4salesmacro.xlms").Activate
    
'move sheet to new document, change sheet name
    Sheets("BLANK").Select
    Sheets("BLANK").Move Before:=Workbooks("document2").Sheets(1)
    Range("B2").Select
    ActiveSheet.Name = Format(Date, "ddmmm")
    WB2.Save
    
    
'clear out data from first document
    DoEvents
    [COLOR=#ff0000]Workbooks.Open ("document1")[/COLOR]
    Range("B2").Select
    If Range("B2").Value <> "" And Range("B3").Value = "" Then
        Range("B2:J2").Clear
    ElseIf Range("B2").Value <> "" And Range("B3").Value <> "" Then
        Selection.End(xlDown).Select: rownum = ActiveCell.Row
        Range(Cells(2, 2), Cells(rownum, 10)).Clear
    End If
    
       Range("D2:D30").Select
    Selection.Style = "Currency"
    Range("H2:H30").Select
    Selection.Style = "Percent"
    Selection.NumberFormat = "0.00%"
    Range("B2:J47").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    
    
    Range("B2").Select
'save and close document2
    WB2.Close savechanges:=True




'save and close document1


    WB1.Close savechanges:=True






End Sub
 
Upvote 0

Forum statistics

Threads
1,215,492
Messages
6,125,116
Members
449,206
Latest member
burgsrus

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