Counting rows of empty cells between two cells with value

Sweedler

Board Regular
Joined
Nov 13, 2020
Messages
114
Office Version
  1. 2016
Platform
  1. Windows
  2. MacOS
Hello

I have a rather large spreadsheet, ca 150000 rows. In one column I have values in about 15000 cells, but they are not spread out randomly. I need a method of counting the amount of rows, in that column, that are between two cells with a value. I am not sure that this makes sense:

This is the result I want - or some version of itand then this step would be next
3030
130-1
230-2
2626
126-1
44
14-1
24-2
34-3
6001260012

I hope this helps basically explain it
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi @Sweedler, thanks for posting on the forum.

I present you a macro, considering the number of rows, I recommend it, since the formula could cause slowness in your sheet.

I assume the data is in column A and you want the results in column B.
If this is not the case, the change in the macro is minimal, you only have to change the "A" by the letter of the column where you have the data. And in the output, change "B2" to the cell where you want the output to start.

That said, put the following macro in a module and execute it.
VBA Code:
Sub CountingRows()
  Dim a As Variant, b As Variant, num As Variant
  Dim i As Long, j As Long
 
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 2)
 
  For i = 1 To UBound(a, 1)
    If a(i, 1) <> "" Then
      num = a(i, 1)
      b(i, 2) = num
      j = 0
    Else
      j = j + 1
      b(i, 1) = j
      b(i, 2) = num & "-" & j
    End If
  Next
  Range("B2").Resize(UBound(b, 1), 2).Value = b
End Sub

Recommendation: Put columns B and C in text format, since for some results that have hyphen excel it can assume that they are dates.

-------------------------------------------------------------------
But if you prefer the formulas, I propose the following:
Dante Amor
ABC
1This is the result I want - or some version of itand then this step would be next
230 30
3130-1
4230-2
526 26
6126-1
74 4
814-1
924-2
1034-3
1160012 60012
Hoja2
Cell Formulas
RangeFormula
B2:B11B2=IF(A2="",IF(A1<>"",1,B1+1),"")
C2:C11C2=IF(B2="",A2,LEFT(C1,FIND("-",C1&"-")-1) &"-"&B2)



--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
 
Last edited:
Upvote 0
Solution
Hi @Sweedler, thanks for posting on the forum.

I present you a macro, considering the number of rows, I recommend it, since the formula could cause slowness in your sheet.

I assume the data is in column A and you want the results in column B.
If this is not the case, the change in the macro is minimal, you only have to change the "A" by the letter of the column where you have the data. And in the output, change "B2" to the cell where you want the output to start.

That said, put the following macro in a module and execute it.
VBA Code:
Sub CountingRows()
  Dim a As Variant, b As Variant, num As Variant
  Dim i As Long, j As Long
 
  a = Range("A2", Range("A" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 2)
 
  For i = 1 To UBound(a, 1)
    If a(i, 1) <> "" Then
      num = a(i, 1)
      b(i, 2) = num
      j = 0
    Else
      j = j + 1
      b(i, 1) = j
      b(i, 2) = num & "-" & j
    End If
  Next
  Range("B2").Resize(UBound(b, 1), 2).Value = b
End Sub

Recommendation: Put columns B and C in text format, since for some results that have hyphen excel it can assume that they are dates.

-------------------------------------------------------------------
But if you prefer the formulas, I propose the following:
Dante Amor
ABC
1This is the result I want - or some version of itand then this step would be next
230 30
3130-1
4230-2
526 26
6126-1
74 4
814-1
924-2
1034-3
1160012 60012
Hoja2
Cell Formulas
RangeFormula
B2:B11B2=IF(A2="",IF(A1<>"",1,B1+1),"")
C2:C11C2=IF(B2="",A2,LEFT(C1,FIND("-",C1&"-")-1) &"-"&B2)



--------------
Let me know the result and I'll get back to you as soon as I can.
Sincerely
Dante Amor
--------------
Thank you sir. I tried both, but settled for the formulas as, for this type of work, I usually prefer them
 
Upvote 0

Forum statistics

Threads
1,215,130
Messages
6,123,220
Members
449,091
Latest member
jeremy_bp001

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