Run time error '9' Subscript out of range, array

101dell

New Member
Joined
Jul 4, 2017
Messages
5
Hi all, so I'm new to excel programming and I've run into an issue, this forum has helped me with other projects in the past but now i'm stuck.

For context; i'm creating a macro that will take the rows from one sheet that contain key words (that can be changed by the user and are written in the cells of another sheet) and pasting them to a new sheet. However the following code throws the Run time error '9' Subscript out of range error. I'm sure it's something silly, but I don't understand it well enough to identify the issue. The debugger highlights "Found = Found Or Not (rngCells.Find(strArray(J)) Is Nothing)" as the issue. I'm fully aware this isn't an elegant solution to the problem either, but any help would be appreciated. :)

Code:
Private Sub CommandButton1_Click()


Dim strArray As Variant
Dim wsSource As Worksheet
Dim wsDest As Worksheet
Dim NoRows As Long
Dim DestNoRows As Long
Dim I As Long
Dim J As Integer
Dim rngCells As Range
Dim rngFind As Range
Dim Found As Boolean
    
    strArray = Sheets("Sheet2").Range("A1:A10").Value
    
    
    Set wsSource = Sheet1
    
    NoRows = wsSource.Range("A1000").End(xlUp).Row
    DestNoRows = 1
    Set wsDest = ActiveWorkbook.Worksheets.Add
        
    For I = 1 To NoRows
    
        Set rngCells = wsSource.Range("H" & I & ":V" & I)
        Found = False
        For J = 0 To UBound(strArray)
            Found = Found Or Not (rngCells.Find(strArray(J)) Is Nothing)
        Next J
        
        If Found Then
            rngCells.EntireRow.Copy wsDest.Range("A" & DestNoRows)
            
            DestNoRows = DestNoRows + 1
        End If
    Next I
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
strArray is 2-dimensional.
 
Upvote 0
No, what would fix it would be to refer to both dimensions when trying to access the items in the array.

For example.
Code:
        For J = LBound(strArray) To UBound(strArray)
            Found = Found Or Not (rngCells.Find(strArray(J, 1)) Is Nothing)
        Next J
 
Upvote 0
Hi,
not fully tested & bit of a guess but see if this comes close to what you are trying to do

Code:
Private Sub CommandButton1_Click()
    Dim strArray As Variant
    Dim wsSource As Worksheet, wsDest As Worksheet
    Dim NoRows As Long, DestNoRows As Long
    Dim J As Integer
    Dim rngCells As Range, Found As Range
    Dim FirstAddress As String
    
'2D array
    strArray = Sheets("Sheet2").Range("A1:A10").Value
    
'set source sheet
    Set wsSource = Sheet1
'no of used rows
    NoRows = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
'row counter
    DestNoRows = 1
'search range
    Set rngCells = wsSource.Range("H1").CurrentRegion
    
    For J = 1 To UBound(strArray, 1)
        If Len(strArray(J, 1)) > 0 Then
'start search
            Set Found = rngCells.Find(strArray(J, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not Found Is Nothing Then
'add new worksheet
                If wsDest Is Nothing Then Set wsDest = ActiveWorkbook.Worksheets.Add
'mark start address
                FirstAddress = Found.Address
                Do
'copy found record
                    Found.EntireRow.Copy wsDest.Range("A" & DestNoRows)
'increment counter
                    DestNoRows = DestNoRows + 1
'find next record match
                    Set Found = rngCells.FindNext(Found)
'bail out if no further match
                    If Found Is Nothing Then Exit Do
'or get back to start address
                Loop While Found.Address <> FirstAddress
            End If
        End If
    Set Found = Nothing
    Next J
End Sub

I have used Range.FindNext Method to search for additional matches.

As already pointed out, when you read a range into an array it's 2D with lower bound of 1.

Hope helpful but adjust code as required.

Dave
 
Last edited:
Upvote 0
Hi,
not fully tested & bit of a guess but see if this comes close to what you are trying to do

Code:
Private Sub CommandButton1_Click()
    Dim strArray As Variant
    Dim wsSource As Worksheet, wsDest As Worksheet
    Dim NoRows As Long, DestNoRows As Long
    Dim J As Integer
    Dim rngCells As Range, Found As Range
    Dim FirstAddress As String
    
'2D array
    strArray = Sheets("Sheet2").Range("A1:A10").Value
    
'set source sheet
    Set wsSource = Sheet1
'no of used rows
    NoRows = wsSource.Cells(wsSource.Rows.Count, "A").End(xlUp).Row
'row counter
    DestNoRows = 1
'search range
    Set rngCells = wsSource.Range("H1").CurrentRegion
    
    For J = 1 To UBound(strArray, 1)
        If Len(strArray(J, 1)) > 0 Then
'start search
            Set Found = rngCells.Find(strArray(J, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not Found Is Nothing Then
'add new worksheet
                If wsDest Is Nothing Then Set wsDest = ActiveWorkbook.Worksheets.Add
'mark start address
                FirstAddress = Found.Address
                Do
'copy found record
                    Found.EntireRow.Copy wsDest.Range("A" & DestNoRows)
'increment counter
                    DestNoRows = DestNoRows + 1
'find next record match
                    Set Found = rngCells.FindNext(Found)
'bail out if no further match
                    If Found Is Nothing Then Exit Do
'or get back to start address
                Loop While Found.Address <> FirstAddress
            End If
        End If
    Set Found = Nothing
    Next J
End Sub

I have used Range.FindNext Method to search for additional matches.

As already pointed out, when you read a range into an array it's 2D with lower bound of 1.

Hope helpful but adjust code as required.

Dave


Thanks for the help Dave, it kind of works, for some reason it only shows 1 match, in some cases it doesn't show anything even though I know the keywords exist. What have I missed?
 
Upvote 0
Thanks for the help Dave, it kind of works, for some reason it only shows 1 match, in some cases it doesn't show anything even though I know the keywords exist. What have I missed?

as always, provided solutions are, without seeing copy of workbook, a bit of a guess

Try adding MatchCase parameter to the Find function & see if that helps.

Rich (BB code):
Set Found = rngCells.Find(strArray(J, 1), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)

Dave
 
Upvote 0
as always, provided solutions are, without seeing copy of workbook, a bit of a guess

Try adding MatchCase parameter to the Find function & see if that helps.

Rich (BB code):
Set Found = rngCells.Find(strArray(J, 1), LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)

Dave

I understand that, and I appreciate the help. :)

I tried your suggestion, to no avail. I think what is happening is that it stops looking after it finds the first result. If you don't mind me asking, could you explain where this code searches. It's changed a fair bit from my original and i'm not entirely confident I understand how and what it's doing. I initially defined which columns to search for the key words in, but now i don't get what it's looking at.

Many thanks.
 
Upvote 0

Forum statistics

Threads
1,215,425
Messages
6,124,824
Members
449,190
Latest member
rscraig11

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