Subscript out of range error?

inkbird1

Board Regular
Joined
Apr 21, 2020
Messages
51
Hi all, getting the error " Runtime error 9 - Subscript out of range"


Private Sub Worksheet_Activate()
Dim arrIncidents As Variant
Dim arrActive As Variant
Dim lngRow As Long
Dim cnt As Long

arrIncidents = Sheets("Incidents").Range("A1").CurrentRegion.Value

ReDim arrActive(1 To 6, 1 To UBound(arrIncidents, 1))

For lngRow = 2 To UBound(arrIncidents, 1)

If arrIncidents(lngRow, 1) = Range("B3").Value Then
cnt = cnt + 1
arrActive(1, cnt) = arrIncidents(lngRow, 1)
arrActive(2, cnt) = arrIncidents(lngRow, 2)
arrActive(3, cnt) = arrIncidents(lngRow, 3)
arrActive(4, cnt) = arrIncidents(lngRow, 4)
arrActive(5, cnt) = arrIncidents(lngRow, 5)
arrActive(6, cnt) = arrIncidents(lngRow, 6)
End If

Next lngRow

ReDim Preserve arrActive(1 To 6, 1 To cnt)

With ListBox100
.ColumnCount = 6
.Column = arrActive
.ColumnWidths = "100,100,150,200,200,300"


End With

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Is it this line ?
VBA Code:
arrIncidents = Sheets("Incidents").Range("A1").CurrentRegion.Value
Try
VBA Code:
arrIncidents = Sheets("Incidents").Range("A1").Value
Also, make sure the sheet name is correct, both in syntax AND speeling....no leading or trailling spaces either
 
Upvote 0
I'd say the long cnt variable is zero as there are no records in Col. A of the Incidents sheet that match what's in cell B3 of the active sheet.
 
Upvote 0
I'd say the long cnt variable is zero as there are no records in Col. A of the Incidents sheet that match what's in cell B3 of the active sheet.

This is correct this error is occuring when there is no data in Column A -
How can i overcome this?
 
Upvote 0
As long as there's no other issue(s) try this:

VBA Code:
Private Sub Worksheet_Activate()
    
    Dim arrIncidents As Variant
    Dim arrActive As Variant
    Dim lngRow As Long
    Dim cnt As Long
    
    arrIncidents = Sheets("Incidents").Range("A1").CurrentRegion.Value
    
    ReDim arrActive(1 To 6, 1 To UBound(arrIncidents, 1))
    
    For lngRow = 2 To UBound(arrIncidents, 1)
        If arrIncidents(lngRow, 1) = Range("B3").Value Then
            cnt = cnt + 1
            arrActive(1, cnt) = arrIncidents(lngRow, 1)
            arrActive(2, cnt) = arrIncidents(lngRow, 2)
            arrActive(3, cnt) = arrIncidents(lngRow, 3)
            arrActive(4, cnt) = arrIncidents(lngRow, 4)
            arrActive(5, cnt) = arrIncidents(lngRow, 5)
            arrActive(6, cnt) = arrIncidents(lngRow, 6)
        End If
    Next lngRow
    
    If cnt > 0 Then
        ReDim Preserve arrActive(1 To 6, 1 To cnt)
        With ListBox100
            .ColumnCount = 6
            .Column = arrActive
            .ColumnWidths = "100,100,150,200,200,300"
        End With
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,701
Members
448,980
Latest member
CarlosWin

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