Subscript out of range error when trying to save workbook

Bagharmin

Board Regular
Joined
Nov 22, 2010
Messages
168
I'm getting a subscript out of range error when trying to save a renamed workbook to my Desktop. Here's the code:

Code:
'   Open the previous month's Report

    Filt = "Excel Files (*.xls*), *.xls*," & "Comma Separated Files (*.csv), *.csv," & "Text Files (*.txt), *.txt"
    FilterIndex = 2
    Title = "Please open the previous month's Report."

    FileName = Application.GetOpenFilename(FileFilter:=Filt, FilterIndex:=FilterIndex, Title:=Title)
    
    If FileName = False Then
        MsgBox ("No file was selected.")
        Exit Sub
    End If
    
    Workbooks.Open (FileName)
    
'   Now give it a new name and save it.

    Filt = "Excel Workbook (*.xlsx), *.xlsx"
    FilterIndex = 1
    Title = "Now give the Report and new name and save it."
    TempName = FileName
    
    FileName = Application.GetSaveAsFilename(TempName, Filt, FilterIndex, Title)
    MsgBox (FileName)
    
    ComboRpt = Right(FileName, Len(FileName) - InStrRev(FileName, "\"))
    
    Workbooks(ComboRpt).SaveAs FileName

I get the error at the last line where I actually try to save the file. What am I doing wrong here? Everything's been properly DIM'd; I just haven't shown all that part of the code.

Many thanks.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
It is far easier to use a workbook variable:

Code:
Dim wb as Workbook
Set wb = Workbooks.Open(FileName)
' other code here
wb.SaveAs FileName
 
Upvote 0
Thanks. I'll probably go ahead and do that, but, for my own education, can you give me an idea why I was getting the subscript out of range error when trying to save?
 
Upvote 0
Because you're parsing the name you intend to use to save the workbook, not the one it currently has.
 
Upvote 0
Oh. So, based on my original code, it would have needed to be "Workbooks(FileName).SaveAs FileName"?
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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