PDA

View Full Version : Subscript out of range



Programs1234
08-18-2017, 07:36 AM
Having some issues saving down a file with VBA. The error box spits out 'Subscript Out of Range". Anybody know whats wrong?




Sub CATReport()

Dim filename3 As String
Dim LR3 As Integer
Dim column_in_review_2 As Variant
Dim rownumber As Integer
Dim mydate As Date
Dim currentdate As Variant
Dim fname 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(filename3).Activate

Workbooks(filename3).Sheets(1).Cells(16, 2) = currentdate

Workbooks(filename3).Sheets("Key").Select


' Bunch of irrelevant code




pth = "S:\Stock Loan\CPM\CATS\Reporting\Nightly File\2017\"


fname = pth & "CorporateActionsTrackerNightly" & "_" & currentdate & "_Internal Only" & ".xlsx"
Debug.Print fname

Workbooks(filename3).Activate

Workbooks(filename3).SaveAs FileName:=fname, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
'
End Sub

Aflatoon
08-18-2017, 07:38 AM
Code tags would be nice, as would a hint as to which line causes the error. ;)

YasserKhalil
08-18-2017, 07:41 AM
Hello
Please put the code between code tags ...
I have noticed that you used this variable at this line

Workbooks(filename3).Sheets(1).Cells(16, 2) = currentdate
And you didn't assign a value to it ...

And you have to click Debug and copy the yellow line that appears to you to know exactly what line causes the error ..

Programs1234
08-18-2017, 08:44 AM
Well, that is the date of which I would type into a cell of a different worksheet. The code breaks at the last line.

SamT
08-18-2017, 11:55 AM
Subscripts are Indices, and are always inside Parentheses. Out of Range means that Indice was not found, was not available, or is just plain wrong

Workbooks(filename3)
Sheets("Key")
Sheets(1)
Cells(15, 2)
MyArray(1)
Range("A1")

Subscripts in red

Subscripts out of range
Workbooks(filenmae3)
Sheets("Kay")
Sheets(0)
Cells(15, -2)
MyArray(Ubound(MyArrar +1))
Range(A1)
Range("ZZZZZ1")