Run time error9, subscript out of range

rinijg

New Member
Joined
May 13, 2011
Messages
47
I have a macro in which I have to count the number of non empty cells in a row.

I have given the code as follows
Code:
Dim re As Long
re = Worksheets("Sheet1").Range("B2", Range("B2").End(xlDown)).Count
But when i run this code, its showing the error "subscript out of range"

Can you tell me how to solve this problem?
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
a small mistake in the above post. I have to count the number of nonempty cells in a column, not row
 
Upvote 0
If Sheet1 is not active sheet, then you'll get incorrect range:
Code:
Dim re As Long
re = Worksheets("Sheet1").Range("B2", [B][COLOR="Red"]Range("B2").End(xlDown)[/COLOR][/B]).Count

The range in red is referring to active sheet. If active sheet is, say, Sheet2 then red code belongs to Sheet2.
Here's correction:
Code:
Dim re As Long
With Worksheets("Sheet1")
    re = .Range("B2", .Range("B2").End(xlDown)).Count
End With
 
Upvote 0
I changed the code as follows.
Code:
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim l As Integer
Dim re As Long
Worksheets("Sheet1").Activate
k = 12
m = 12
a = 1
n = Worksheets("Sheet1").Range("A2", Range("A2").End(xlDown)).Count
Worksheets("New KK routings").Activate
With Worksheets("New KK routings")
    re = .Range("B2", .Range("B2").End(xlDown)).Count
End With
Still it is showing the same error. The value of re should be 7308
 
Upvote 0
There's no need to activate sheet. Read carefully my previous post.
Code:
With Worksheets("Sheet1")
    n = .Range("A2", .Range("A2").End(xlDown)).Count
End With
With Worksheets("New KK routings")
    re = .Range("B2", .Range("B2").End(xlDown)).Count
End With
 
Upvote 0
This is my code now
Code:
Dim n As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim m As Integer
Dim l As Integer
Dim re As Long
k = 12
m = 12
a = 1
With Worksheets("Sheet1")
    n = .Range("A2", .Range("A2").End(xlDown)).Count
End With
With Worksheets("New KK routings")
    re = .Range("B2", .Range("B2").End(xlDown)).Count
End With
Still it is showing the same error. It is not showing error for
Code:
With Worksheets("Sheet1")
    n = .Range("A2", .Range("A2").End(xlDown)).Count
End With
It is showing error for only
Code:
With Worksheets("Sheet1")
    n = .Range("A2", .Range("A2").End(xlDown)).Count
End With
 
Upvote 0
There's no need to activate sheet. Read carefully my previous post.
Code:
With Worksheets("Sheet1")
    n = .Range("A2", .Range("A2").End(xlDown)).Count
End With
With Worksheets("New KK routings")
    re = .Range("B2", .Range("B2").End(xlDown)).Count
End With

Where you are overlooking simple fact, the sheet is being activated, so your theory that the code was looking at ranges on different sheets is incorrect, meaning your previous post is still wrong.

Maybe

Code:
End(xlUp)

Alternatively try

Code:
re = WorksheetFunction.CountA(Columns("A"))-1

Note:- only use "-1" at the end of the code line if A1 is non-blank, likewise for the column B equivilant.
 
Last edited:
Upvote 0
Try this:
Code:
Dim n As [B][COLOR="Red"]Long[/COLOR][/B]
 
Upvote 0
jasonb75, Prove it.
Code:
Worksheets("Sheet1").Range("A2", [B][COLOR="Red"]Range("A2").End(xlDown)[/COLOR][/B])

If Sheet1 IS NOT ACTIVE, red refers to active sheet, which, of cource, is NOT Sheet1.
 
Last edited:
Upvote 0
I am really sorry.. Actually that was a mistake in typing. Its not showing error for calculating the value of n. Its showing error in calculating the value of re. That is first With.. end with is executed properly. Second with..end with is showing this error
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,740
Members
452,940
Latest member
rootytrip

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