Macro - close workbook (Phantom blank excel workbook stays open)

dpnab

New Member
Joined
Apr 12, 2022
Messages
35
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'[m trying to create a macro that will automatically close the workbook but what happens is it leaves behind a blank/greyed out excel file, so not even a new workbook.

I can use application.quit but then if there is another excel file open, it will force close that. Any suggestions?

Sub Export_Sheets()
' Note: Leading zeroes in Column C & D to be kept

Dim wbk1 As Workbook, wbk2 As Workbook
Dim sh As Worksheet, rng As Range
Dim LastRow As Long
Dim fldrName As String
' Don't show confirmation window
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set wbk1 = ThisWorkbook
fldrName = "\\SERVER ADDRESS"

For Each sh In wbk1.Sheets
If sh.Name = "Upload" Then
' Find last row non blank row (where the term blank includes formulas returning "")
LastRow = sh.Cells.Find("*", LookIn:=xlValues, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set rng = sh.Range("A1").CurrentRegion.Resize(LastRow)
Set wbk2 = Workbooks.Add
wbk2.Sheets(1).Range(rng.Address).Value = rng.Value
rng.Columns("C:D").Copy
wbk2.Sheets(1).Range("C1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
wbk2.Sheets(1).Columns.AutoFit
wbk2.SaveAs Filename:=fldrName & "/" & sh.Name & ".csv", FileFormat:=xlCSV, local:=True
wbk2.Close
End If
Next sh


ActiveWorkbook.Saved = True
ActiveWorkbook.Close
' Allow confirmation windows to appear as normal
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hope this helps.
VBA Code:
Sub Export_Sheets()
' Note: Leading zeroes in Column C & D to be kept

Dim wbk1 As Workbook, wbk2 As Workbook
Dim sh As Worksheet, rng As Range
Dim LastRow As Long
Dim fldrName As String
' Don't show confirmation window
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set wbk1 = ThisWorkbook
fldrName = "\\SERVER ADDRESS"
With wbk1.Sheets("Upload")
    ' Find last row non blank row (where the term blank includes formulas returning "")
    LastRow = .Cells.Find("*", LookIn:=xlValues, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Set rng = .Range("A1").CurrentRegion.Resize(LastRow) '.Resize(LastRow,4)?
    rng.Select
    Set wbk2 = Workbooks.Add
    wbk2.Sheets(1).Range(rng.Address).Value = rng.Value
    rng.Columns("C:D").Copy ' If column B of Sheets ("Upload") has some values, I don't think this row and the row below are needed.
    wbk2.Sheets(1).Range("C1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    wbk2.Sheets(1).Columns.AutoFit
    wbk2.SaveAs Filename:=fldrName & "/" & .Name & ".csv", FileFormat:=xlCSV, local:=True
    wbk2.Close
End With

wbk1.Saved = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
If Workbooks.Count = 1 Then
    Application.Quit
Else
    wbk1.Close
End If

' Allow confirmation windows to appear as normal
End Sub
 
Upvote 0
Solution
Hope this helps.
VBA Code:
Sub Export_Sheets()
' Note: Leading zeroes in Column C & D to be kept

Dim wbk1 As Workbook, wbk2 As Workbook
Dim sh As Worksheet, rng As Range
Dim LastRow As Long
Dim fldrName As String
' Don't show confirmation window
Application.DisplayAlerts = False
Application.ScreenUpdating = False

Set wbk1 = ThisWorkbook
fldrName = "\\SERVER ADDRESS"
With wbk1.Sheets("Upload")
    ' Find last row non blank row (where the term blank includes formulas returning "")
    LastRow = .Cells.Find("*", LookIn:=xlValues, searchorder:=xlByRows, searchdirection:=xlPrevious).Row
    Set rng = .Range("A1").CurrentRegion.Resize(LastRow) '.Resize(LastRow,4)?
    rng.Select
    Set wbk2 = Workbooks.Add
    wbk2.Sheets(1).Range(rng.Address).Value = rng.Value
    rng.Columns("C:D").Copy ' If column B of Sheets ("Upload") has some values, I don't think this row and the row below are needed.
    wbk2.Sheets(1).Range("C1").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
    wbk2.Sheets(1).Columns.AutoFit
    wbk2.SaveAs Filename:=fldrName & "/" & .Name & ".csv", FileFormat:=xlCSV, local:=True
    wbk2.Close
End With

wbk1.Saved = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
If Workbooks.Count = 1 Then
    Application.Quit
Else
    wbk1.Close
End If

' Allow confirmation windows to appear as normal
End Sub
Says "Compile error: End with Without With"
 
Upvote 0
Which line? And please delete this line "rng.Select".
Sorry, I copied it incorrectly originally.

I changed "If Workbooks.Count = 1 Then" to 2 and it worked! I guess it technically kept the workbook open during that part. not sure but it works, and I tested it while having another excel workbook open to make sure.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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