Subscript out of range runtime error '9'

bsquad

Board Regular
Joined
Mar 15, 2016
Messages
194
I am have some trouble with selecting ranges.

its a bit of a scenario-

I have one Master workbook with multiple sheets
-I am trying to import other workbooks' data into the master sheet in its respective sheet
--I have multiple sub workbooks or sub Master workbooks with identical sheets (identical in the sheets I need to import)
where I am having trouble is in the sheet selection when pasting in the Master workbook
-particularly with the line of code:

--'With Worksheets(ArraySelectCount2)' is where I am getting subscript error

some of this code I have found on this forum which has worked great for a single sheet


Code:
Sub SelectOpenCopy()
    Dim vaFiles As Variant
    Dim i As Long
    Dim i2 As Long
    Dim wb As Workbook
    Dim wb2 As Workbook
    Dim ws As Worksheet
    Dim File_Path As String
    File_Path = Range("File_Path").Value
'IMPORTED SHEETS
    Dim ArraySelect As Variant
        ArraySelect = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6")
    Dim ArraySelectCount As Variant
'MASTER SHEETS
    Dim ArraySelect2 As Variant
        ArraySelect2 = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6")
    Dim ArraySelectCount2 As Variant
    
        
MsgBox "Select the files to import"
vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", _
          Title:="Select files", MultiSelect:=True)
    If IsArray(vaFiles) Then
        For i = LBound(vaFiles) To UBound(vaFiles)
            Set wb = Workbooks.Open(Filename:=vaFiles(i))
            For Each ArraySelectCount In ArraySelect
                With wb.Worksheets(ArraySelectCount)
                    If Range("C3").Value <> "" Then
                            Range("C2").Select
                            Range(Selection, Selection.End(xlToRight)).Select
                            Range(Selection, Selection.End(xlDown)).Offset(1).Select
                            Selection.Copy
                        Windows("" & File_Path & "").Activate
                            For i2 = LBound(ArraySelect2) To UBound(ArraySelect2)
'                                ''Set wb2 = Worksheets(ArraySelect2)
                                For Each ArraySelectCount2 In ArraySelect2
                                    With Worksheets(ArraySelectCount2)
                                        If Range("B2").Value = "" Then
                                            Range("B2").Select
                                            Selection.PasteSpecial Paste:=xlPasteColumnWidths
                                            Selection.PasteSpecial Paste:=xlPasteValues
                                            Else
                                            Range("B1").Select
                                            Range("B1").End(xlDown).Offset(1).Select
                                            Selection.PasteSpecial Paste:=xlPasteColumnWidths
                                            Selection.PasteSpecial Paste:=xlPasteValues
                                        End If
                                    End With
                                Next ArraySelectCount2
                            Next i2
                        wb.Application.CutCopyMode = False
                    End If
                End With
            Next ArraySelectCount
            wb.Close savechanges:=False
        Next i
     End If
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
It seems that you may be doubling up on your iterations.

These two lines are effectively the same. I don't know how that works out with your code though.
Code:
For i2 = LBound(ArraySelect2) To UBound(ArraySelect2)
For Each ArraySelectCount2 In ArraySelect2
 
Upvote 0
Man you gotta lot of iterations there!

By my count, you will have pasted ranges in the ArraySelect2 set of sheets 36 times. Which means you are copying from:

Code:
ArraySelect Sheet1 to ArraySelect2 Sheet1
ArraySelect Sheet1 to ArraySelect2 Sheet2
ArraySelect Sheet1 to ArraySelect2 Sheet3
ArraySelect Sheet1 to ArraySelect2 Sheet4
ArraySelect Sheet1 to ArraySelect2 Sheet5
ArraySelect Sheet1 to ArraySelect2 Sheet6

ArraySelect Sheet2 to ArraySelect2 Sheet1
ArraySelect Sheet2 to ArraySelect2 Sheet2
ArraySelect Sheet2 to ArraySelect2 Sheet3
ArraySelect Sheet2 to ArraySelect2 Sheet4
ArraySelect Sheet2 to ArraySelect2 Sheet5
ArraySelect Sheet2 to ArraySelect2 Sheet6

ArraySelect Sheet3 to ArraySelect2 Sheet1
ArraySelect Sheet3 to ArraySelect2 Sheet2
ArraySelect Sheet3 to ArraySelect2 Sheet3
ArraySelect Sheet3 to ArraySelect2 Sheet4
ArraySelect Sheet3 to ArraySelect2 Sheet5
ArraySelect Sheet3 to ArraySelect2 Sheet6

ArraySelect Sheet4 to ArraySelect2 Sheet1
ArraySelect Sheet4 to ArraySelect2 Sheet2
ArraySelect Sheet4 to ArraySelect2 Sheet3
ArraySelect Sheet4 to ArraySelect2 Sheet4
ArraySelect Sheet4 to ArraySelect2 Sheet5
ArraySelect Sheet4 to ArraySelect2 Sheet6

ArraySelect Sheet5 to ArraySelect2 Sheet1
ArraySelect Sheet5 to ArraySelect2 Sheet2
ArraySelect Sheet5 to ArraySelect2 Sheet3
ArraySelect Sheet5 to ArraySelect2 Sheet4
ArraySelect Sheet5 to ArraySelect2 Sheet5
ArraySelect Sheet5 to ArraySelect2 Sheet6

ArraySelect Sheet6 to ArraySelect2 Sheet1
ArraySelect Sheet6 to ArraySelect2 Sheet2
ArraySelect Sheet6 to ArraySelect2 Sheet3
ArraySelect Sheet6 to ArraySelect2 Sheet4
ArraySelect Sheet6 to ArraySelect2 Sheet5
ArraySelect Sheet6 to ArraySelect2 Sheet6

Did you mean to do that?
 
Upvote 0
thank you for your 2nd post, that helped out a lot in understanding and would explain why I was seeing 360 lines (10 rows of data for each sheet). To answer you question; no I am essentially trying to - for each one of the workbooks imported (ArraySelect), paste them in their respective sheet in the ArraySelect2.

by imported I mean this part (which I am sure you already knew) - vaFiles = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", _ Title:="Select files", MultiSelect:=True)

Code:
ArraySelect Sheet1 to ArraySelect2 Sheet1
ArraySelect Sheet2 to ArraySelect2 Sheet2
ArraySelect Sheet3 to ArraySelect2 Sheet3
ArraySelect Sheet4 to ArraySelect2 Sheet4
ArraySelect Sheet5 to ArraySelect2 Sheet5
ArraySelect Sheet6 to ArraySelect2 Sheet6
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top