Consulting

Results 1 to 15 of 15

Thread: Subscript out of range and SaveAs Method error

  1. #1

    Subscript out of range and SaveAs Method error

    Alright, i got some code here that does not work. Can anyone help me fix this. The error box keeps shooting out the message either "subscript outta range" or "Save as" error. Error happens in the LAST LINE

    Please see the code below:

    Sub CATReport()
    
    Dim filename3 As Variant
    Dim LR3 As Integer
    Dim column_in_review_2 As Variant
    Dim rownumber As Integer
    Dim mydate As Date
    Dim currentdate As Variant
    Dim fname3 As Variant
    Dim filename4 As Variant
    
    
    
    'Attach Buttons from Macro sheet
    
    LR3 = Range("AC" & Rows.Count).End(xlUp).Row
    
    Application.DisplayAlerts = False
    
    Workbooks("Macros").Activate
    
    
    filename3 = Workbooks("Macros").Sheets(1).Cells(15, 2)
    
    Workbooks("Macros").Activate
    
    currentdate = Workbooks("Macros").Sheets(1).Cells(16, 2)
    
    Workbooks(filename3).Activate
    
    
    ''''''''''Irrelevant code'''''''''''''''
    
    
    
    
    
    pth = "S:\Stock Loan\CPM\CATS\Reporting\Nightly File\2017\"
    
    fname3 = pth & "CorporateActionsTrackerNightly" & "_" & currentdate & "_Internal Only" & ".xlsx"
    Debug.Print fname3
    
    
    Workbooks(filename3).SaveAs FileName:=fname3, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
    
    End Sub

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Does CurrentDate contain any illegal filename characters?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    No sir, currentdate belongs to whatever i plug into the cell of another workbook.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    If your date format is 01/02/17 then "/" is an illegal filename character. Copy and post your fname3 value
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    I did not use this "/" character. I mean the current date value is string so i do not believe that is the problem. The error happens at the last line.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Copy and post your fname3 value

    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    "CAT Practice Sheet" is what it is called. It is located on another spreadsheet tho.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I see in your code
    fname3 = pth & "CorporateActionsTrackerNightly" & "_" & currentdate & "_Internal Only" & ".xlsx" 
        Debug.Print fname3
    I ask
    Copy and post your fname3 value
    Can I be any clearer?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    you will get a "subscript out of range " error if the value in filename3 is invalid or the workbook is not open.

    So your macro depends totally on what is in

    Workbooks("Macros").Sheets(1).Cells(15, 2) and
    Workbooks("Macros").Sheets(1).Cells(16, 2)

    Since you haven't told us what is in these cells and you haven't posted any workbooks, you are making it very difficult to help you.

  10. #10
    currentdate is pretty much the date that i would type into another spreadsheet. So if I am running the report today, i would just plug in something like 8.25.2017 so that the report would save with today's date in the name.


    "S:\Stock Loan\CPM\CATS\Reporting\Nightly File\2017\" & "CorporateActionsTrackerNightly" & "_" & currentdate & "_Internal Only" & ".xlsx"

    Is this what you are asking for?

  11. #11
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    i would just plug in something like 8.25.2017 so that the report would save with today's date in the name.
    That could be your problem 8.25.2017 is not a standard excel format for a date
    try 8/25/2017

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Is this what you are asking for?
    No. I would expect to see values for all variables and no ampersands if you post the result of "Debug.Print fname3"
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    I mean the currentdate is just a string so i am not too sure if that will fix the problem.

  14. #14
    "S:\Stock Loan\CPM\CATS\Reporting\Nightly File\2017\CorporateActionsTrackerNightly_ 8.25.2017_Internal Only.xlsx" <-- This is what it should look like.

  15. #15
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    This is what it should look like
    I give up.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •