I wrote a macro to loop through files in a folder, copy some data from 1 tab over to another workbook. I then want to close the looped workbook before opening the next, but I receive a Subscript out of range error when trying to close.
I am using a desktop version of Excel for Office 365.
In the code below, I get Run-time error 9: Subscript out of range error when trying to execute Workbooks(currentFile).Close
The currentFile
variable works further up the code to open a file.
I have tried setting it up like:
Workbooks(currentFile).Activate
ActiveWorkbook.Close
But I get the same error
'setup FSO
Dim folderName As String
Dim FSOLibrary As Object
Dim FSOFolder As Object
Dim FSOFile As Object
'Use For Each loop to loop through each file in the folder
For Each FSOFile In FSOFile
'actions to be performed on each file
currentFile = FSOFile
'go to Full Raw tab
Workbooks.Open (currentFile)
Worksheets("Full Raw").Range("A1").Select
'select everything & copy
Selection.End(xlDown).Select
ActiveCell.Resize(1, 15).Select
Range(Selection, Selection.End(xlUp)).Select
Selection.Copy
'go to combining workbook
Workbooks("" & txn_month & "_" & Year(Date) & "").Activate
'go to bottom of data
Range("A1").Select
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select
'paste copied data
ActiveCell.PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'close working file
Workbooks(currentFile).Close
Next
I expect each FSOFile to open, do the stuff, then close before the next FSOFile is selected
Where am I going wrong?