Enter No of empty rows beside each cell with data

A Thayuman

New Member
Joined
Mar 6, 2019
Messages
28
Hi

I would like to know how many empty rows are below each cell with data. Coloum "B" has data - number of empty cells should appear in coloum "A"

I have found this code in one of the forum messages however the code works but I get -

The Microsoft Visual Basic Window pops up - "Rum-time error '6': Overflow

When I hit debug it shows be the error is in "NextName = Range("B" & Counter).End(xlDown).Row"

Sub CountEmpty()

Dim LastRow As Integer
Dim Counter As Integer
Dim NextName As Integer

LastRow = Range("B" & Rows.Count).End(xlUp).Row

For Counter = 2 To LastRow

If Range("B" & Counter).Value = "" Then GoTo NextTry

NextName = Range("B" & Counter).End(xlDown).Row

Range("A" & Counter).Value = NextName - Counter - 1

NextTry:

Next Counter

End Sub

I hope to hear from one of the experts

VBA to add rows.xlsm
ABCDEFG
1Empty CellsDateTransaction DetailsNotes 1Notes 2DebitCredit
2202-Jan-00Julia Linsyd stay R4446544180Transfer Debit-210.00
3
4
5508-Jan-00OPTUS BILLING SVC...INTERNET BPAYTransfer Debit-87.20
6
7
8
9
10
11507-Jan-00ST.GEORGE BANK-C...1234Automatic Drawing-418.70
12
13
14
15
16
17907-Jan-00HIRE PURCHASE...0000000237300845Automatic Drawing-430.17
18
19
20
21
22
23
24
25
26
27707-Jan-00LIMIT RATE IS 8.820...Miscellaneous Credit0.00
Sheet1
 
Last edited:

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Without checking anything else, IIRC that error would be raised if your integer variable exceeds 3,2676 (or whatever the real number is - I forget).
Try Debug.Print Range("B" & Counter).End(xlDown).Row
Looks to me like the value should be over a million because of xlDown.
 
Upvote 0
Similar to @Micron's comment, I assume there must be some way to detect the end of column B.

042523FunctionSyntax.xlsm
ABCDEFG
1DateTransaction DetailsNotes 1Notes 2DebitCredit
222-Jan-00Julia Linsyd stayR4446544180Transfer Debit-210
3
4
558-Jan-00OPTUS BILLING SVC...INTERNET BPAYTransfer Debit-87.2
6
7
8
9
10
1157-Jan-00ST.GEORGE BANK-C...1234Automatic Drawing-418.7
12
13
14
15
16
1797-Jan-00HIRE PURCHASE...0000000237300845-430.17
18
19
20
21
22
23
24
25
26
2777-Jan-00LIMIT RATE IS 8.820…Miscellaneous Credit0
28
29
30
31
32
33
34
350end
Sheet3


Try:

Code:
Sub countblankrows()
Dim lr As Long, n As Long, r As Long
    n = 0
    lr = Cells(Rows.Count, "B").End(xlUp).Row
  For r = lr To 2 Step -1
    If IsEmpty(Cells(r, "B")) Then
      n = n + 1
    Else: Cells(r, "A") = n
      n = 0
    End If
  Next r
End Sub
 
Upvote 0
Solution
Without checking anything else, IIRC that error would be raised if your integer variable exceeds 3,2676 (or whatever the real number is - I forget).
Try Debug.Print Range("B" & Counter).End(xlDown).Row
Looks to me like the value should be over a million because of xlDown.
Thanks for responding to my query, when I use debug.print in the immediate window I get 27

I also tired 'LastRow = Range("B" & 2000).End(xlUp).Row and got the same error and in debug.print in the immediate window I get 27

is there are better way to write this code
 
Upvote 0
Similar to @Micron's comment, I assume there must be some way to detect the end of column B.

042523FunctionSyntax.xlsm
ABCDEFG
1DateTransaction DetailsNotes 1Notes 2DebitCredit
222-Jan-00Julia Linsyd stayR4446544180Transfer Debit-210
3
4
558-Jan-00OPTUS BILLING SVC...INTERNET BPAYTransfer Debit-87.2
6
7
8
9
10
1157-Jan-00ST.GEORGE BANK-C...1234Automatic Drawing-418.7
12
13
14
15
16
1797-Jan-00HIRE PURCHASE...0000000237300845-430.17
18
19
20
21
22
23
24
25
26
2777-Jan-00LIMIT RATE IS 8.820…Miscellaneous Credit0
28
29
30
31
32
33
34
350end
Sheet3


Try:

Code:
Sub countblankrows()
Dim lr As Long, n As Long, r As Long
    n = 0
    lr = Cells(Rows.Count, "B").End(xlUp).Row
  For r = lr To 2 Step -1
    If IsEmpty(Cells(r, "B")) Then
      n = n + 1
    Else: Cells(r, "A") = n
      n = 0
    End If
  Next r
End Sub

Thanks kweaver, works like a dream your help is very much appreciated, really well done​

 
Upvote 0
I also tired 'LastRow = Range("B" & 2000).End(xlUp).Row and got the same error and in debug.print in the immediate window I get 27
Sure, on one particular iteration I guess. This is what I get
5
11
17
27
1048576
 
Upvote 0
Sure, on one particular iteration I guess. This is what I get
5
11
17
27
1048576
Thanks Micron for your response, I know this is a problem but dont know enough to take it any further, at least you have pointed me as to why there is an overflow error with end (xldown), thank you again
 
Upvote 0
NP. To fix the overflow problem you'd use a variable type that will accept the number you intend to put in it. In this case, a Long - good for over 2 billion. That's why you got the overflow error - last row number exceeds 1 million. Integer is good for +-32,767
What I meant in my first post is that you should use xlUp to find the last row, not xlDown, which is what you were given in post 3:

lr = Cells(Rows.Count, "B").End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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