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?
 
Try this, although reading what Sektor and Jason are saying I think there's a problem you've not identified or are unable to explain which is why it's causing a problem:
Code:
Sub Rinijg()

    Dim n As Long, a As Long, i As Long, j As Long, k As Long, m As Long, l As Long, re As Long
    
    k = 12: m = 12: a = 1
    
    With Worksheets("Sheet1")
        n = .Range("A2).End(xlDown).Row - 1
    End With
    
    With Worksheets("New KK routings")
        re = .Range("B2").End(xlDown).Row - 1
    End With
    
    MsgBox n & " " & re

End Sub
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
By the way, check worksheets names. Are they spelled correctly?
 
Upvote 0
jasonb75, Anyway there must be no error even if there's empty row. Then Count will just return 1.

My additional comment was thinking ahead to possible wrong results after the error is eliminated.

Not sure why, you would think it would count 1 if it was empty but for some reason it counts 2.

Just noticed that Worksheets("New KK routings") was changed to Worksheets("NewKKroutings") looks like a good candidate for an error.
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,753
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