beartooth91
Board Regular
- Joined
- Dec 15, 2024
- Messages
- 51
- Office Version
- 365
- 2019
- 2016
- Platform
- Windows
I'm getting the 'subscript out of range' error on the line below and I'm not real sure why. Tried a few different things and still no success. Any ideas?
The entire sub is below.
VBA Code:
sheetExist = (wbMaster.Sheets(wsname).Index > 0) '<------- Getting 'subscript out of range' error
VBA Code:
Sub Import_IO_Lists()
' Opens each workbook in the Standard-Format IO Lists subfolder and copies each worksheet into the NIC Master IO List workbook
'
Application.ScreenUpdating = False
'Declare Variables------------------------------------------------------------------------------
Dim sheetExist As Boolean
Dim StartRow As Long, LastRow As Long, sRow As Long
Dim SfFolder As String, SfList As String, wbname As String, wsname As String
Dim nme() As String
Dim wbMaster As Workbook
SfFolder = Dir(ThisWorkbook.Path & "/Standard-Format IO Lists")
SfList = Dir(ThisWorkbook.Path & "/Standard-Format IO Lists" & "\*.xlsx")
Set wbMaster = Workbooks("NIC Master IO List.xlsm")
'-----------------------------------------------------------------------------------------------
'Loop through the Standard-Format IO Lists subfolder, copy each workbook to a IO sheet in the Master IO List workbook
Do While SfList <> ""
With Workbooks("NIC Master IO List.xlsm").Sheets(1)
Sheets(1).Copy After:=Sheets(Worksheets.Count)
ActiveSheet.Name = "NewSheet"
StartRow = .Cells(Rows.Count, "B").End(xlUp).Row + 1
End With
Workbooks.Open FileName:=ThisWorkbook.Path & "/Standard-Format IO Lists" & "\" & SfList 'Open each workbook, one at a time
wbname = ActiveWorkbook.Name ' Capture newly-opened workbook name
With Workbooks(wbname).Sheets(1)
'LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
nme = Split(.Cells(11, 2).Value, "-", -1)
wsname = nme(1)
End With
sheetExist = (wbMaster.Sheets(wsname).Index > 0) '<------- Getting 'subscript out of range' error
With Workbooks(wbname).Sheets(1)
LastRow = .Cells(Rows.Count, "B").End(xlUp).Row
If Not sheetExist Then
Sheets(1).Range("B11:BO" & LastRow).Copy Destination:=wbMaster.Worksheets("NewSheet").Range("B" & StartRow)
Else
Sheets(1).Range("B11:BO" & LastRow).Copy Destination:=wbMaster.Worksheets(wsname).Range("B" & sRow)
End If
Workbooks(wbname).Close
End With
With Workbooks("NIC Master IO List.xlsm").Worksheets("NewSheet")
If Not sheetExist Then
Worksheets("NewSheet").Name = wsname
Else
Worksheets("NewSheet").Delete
End If
End With
'wsname = nme(1)
SfList = Dir
Loop
Application.ScreenUpdating = True
End Sub