Close a known open workbook

jacobrcotton

Board Regular
Joined
Jan 28, 2017
Messages
51
Hi Everyone,

This is related to previous post, but i'm hoping I'm explaining this a bit better this time.

I have a code that will save a .xlsm as a new .xlsx with a timestamp, but its throwing an error when i'm trying to close the original spreadsheet. When i attempt to activate the original workbook, I'm receiving a "subscript out of range" error, and I don't know why. I know the workbook is open, as i'm looking at it and its named correctly with the ".xlsm" extension.

Any help would be much appreciated.

Code:
Sub SaveAsXLSX()

    Dim fPath As String, fName As String
    Dim ws As Worksheet
    Dim i As Long
    Dim arr() As String
    Dim CurrTimeStamp As Variant
    
    CurrTimeStamp = Format(Now(), "yyyymmddhhmmss")

    fPath = ActiveWorkbook.Path
    fName = Left(ActiveWorkbook.Name, (InStrRev(ActiveWorkbook.Name, ".", -1, vbTextCompare) - 1))

    ReDim arr(1 To (ActiveWorkbook.Sheets.Count))
    i = 1

    For Each ws In ActiveWorkbook.Worksheets
        arr(i) = ws.Name
        i = i + 1
    Next ws

    Worksheets(arr).Copy
    ActiveWorkbook.SaveAs FileName:=fPath & "\" & fName & " " & CurrTimeStamp & ".xlsx"

    Application.DisplayAlerts = False
 
'       I'm throwing an error at the line below, with a "subscript out of range" error
        Workbooks(fPath & "\" & fName & ".xlsm").Activate
        Workbooks(fPath & "\" & fName & ".xlsm").Close Savechanges:=False
    Application.DisplayAlerts = True
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hi

Replace code in Error

Code:
'       I'm throwing an error at the line below, with a "subscript out of range" error
        Workbooks(fPath & "\" & fName & ".xlsm").Activate
        Workbooks(fPath & "\" & fName & ".xlsm").Close Savechanges:=False

With Code below

Code:
ThisWorkbook.Close Savechanges:=False

Does it help?

Biz
 
Upvote 0
Hi Biz,

Unfortunately, it does not. The macro that is running is not stored in the original workbook, its stored in my PERSONAL workbook. so using

ThisWorkbook.Close

will close my PERSONAL workbook, not the workbook that contains the actual work file.

Thanks!
 
Upvote 0
Hi,

Trying code below in error
Code:
'       I'm throwing an error at the line below, with a "subscript out of range" error
        Workbooks(fPath & "\" & fName & ".xlsm").Activate
        Workbooks(fPath & "\" & fName & ".xlsm").Close Savechanges:=False

with code below
ActiveWorkbook.Close Savechanges:=False

Does it help?
Kind Regards

Biz
 
Upvote 0
How about
Code:
Sub SaveAsXLSX()

    Dim fPath As String, fName As String
    Dim ws As Worksheet
    Dim i As Long
    Dim arr() As String
    Dim CurrTimeStamp As Variant
    [COLOR=#0000ff]Dim Wbk As Workbook[/COLOR]
    
    CurrTimeStamp = format(Now(), "yyyymmddhhmmss")
    [COLOR=#0000ff]Set Wbk = ActiveWorkbook[/COLOR]
    fPath = ActiveWorkbook.Path
    fName = Left(ActiveWorkbook.name, (InStrRev(ActiveWorkbook.name, ".", -1, vbTextCompare) - 1))

    ReDim arr(1 To (ActiveWorkbook.Sheets.Count))
    i = 1

    For Each ws In ActiveWorkbook.Worksheets
        arr(i) = ws.name
        i = i + 1
    Next ws

    Worksheets(arr).Copy
    ActiveWorkbook.SaveAs FileName:=fPath & "\" & fName & " " & CurrTimeStamp & ".xlsx"

    Application.DisplayAlerts = False
 
'       I'm throwing an error at the line below, with a "subscript out of range" error
        [COLOR=#0000ff]Wbk.Close False[/COLOR]
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,022
Messages
6,128,330
Members
449,442
Latest member
CaptBrownShoes

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