1

I'm getting an error on the Filename line saying that subscript is out of range. I've the file name critea on a sheet called Fname

Set xl = ActiveWorkbook
Application.DisplayAlerts = True
Path = "S:\Common\Central\"
Filename = xl.Sheets("Fname").Range("A7").Value
xl.SaveAs Path & Filename & ".xlsx"
xl.Close
5
  • Sheet name probably has leading or trailing whitespace. Or the active workbook is not the workbook with the sheet in it. Commented Mar 12, 2021 at 20:06
  • try change activeworkbook to thisworkbook (if target sheet is on workbook with macro) Commented Mar 12, 2021 at 20:08
  • I get the same error. What should I do to set it as the ActiveWorkbook? Commented Mar 12, 2021 at 20:13
  • So I remove the Set xl = ActiveWorkbook statement and I no longer get an error on Filename but I get a new on the next line to SaveAs. The new error is Object doesn't support this property or method Commented Mar 12, 2021 at 20:24
  • Are there maybe two workbooks involved in this code? If it is only one, is it the one containing this code? If it is two, please clarify which is which. I mean, you may be reading the file name from one workbook, and then saving another workbook with this file name. Commented Mar 12, 2021 at 21:31

2 Answers 2

0

My advice to you is as follows:

declarate global variables in standard modules like

    Public wb As Workbook
    Public ws As Worksheet
    
    Sub startupSettings()
        Set wb = ThisWorkbook
        Set ws = wb.Worksheets("Sheetname")
    End Sub

you can call this procedure by worbook open event in thisworkbook code like

Private Sub Workbook_Open()
    startupSettings
End Sub

or every time as first when you lunch procedures

Sub someSub()
    startupSettings
    '.... code
End Sub

then your save file procedure looks like

option explicit

Sub saveMyFile()
    Dim path As String, fileName As String
    startupSettings '   if variables not set before
    Application.DisplayAlerts = True
    path = "S:\Common\Central\"
    fileName = ws.Range("A7").Value
    wb.SaveAs path & fileName & ".xlsx" ' if you save workbook with macro that line return error should be .xlsm or another procedure with workbook.add
    wb.Close
End Sub
Sign up to request clarification or add additional context in comments.

1 Comment

to make the code readable always use main sub when u lunch subrutines and functions
0

Backup Workbook

  • If you turn off Application.DisplayAlerts before saving, you won't see any dialog (overwrite, file format,...) and the file will be automatically overwritten. Don't forget to turn it on again.
  • When saving files, you should always use a parameter for the second argument FileFormat. In your case, it is xlOpenXMLWorkbook for .xlsx files.

The Code

Option Explicit

Sub backupWorkbook()
    
    Const FolderPath As String = "S:\Common\Central\"

    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    ' Set wb = ActiveWorkbook ' any workbook that is selected
    
    Dim BaseName As String: BaseName = wb.Worksheets("Fname").Range("A7").Value

    Application.DisplayAlerts = False
    wb.SaveAs FolderPath & BaseName & ".xlsx", xlOpenXMLWorkbook
    Application.DisplayAlerts = True

    wb.Close

End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.