Dynamically renumber filtered rows

honkin

Active Member
Joined
Mar 20, 2012
Messages
374
Office Version
  1. 2016
Platform
  1. MacOS
I have a sheet which records football match results and there is a column which simply indicates the number of matches, starting from 1 onwards.

How is it possible to have that column be dynamic so that when the sheet is filtered, the first visible row counts as 1 and the the next visible 2 and so on?

I just want it to be able to be dynamic, so that all the numbering changes each time the filtering is changed

cheers
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
There may be more elegant ways of doing it, but this method does what you seem to be after. You haven't provided much information so the following is provided for demonstration purposes only. You can change the column and cell references to suit your sheet layout.
Basically, it uses a Worksheet_Calculate() private sub to trigger the event. There is a CountA() formula in the sample in cell C1 - you can put it anywhere on the sheet and hide the column it's in if need be.

The code which goes in the relevant sheet code area:

VBA Code:
Private Sub Worksheet_Calculate()
    Dim ws As Worksheet, lr As Long, i As Long, c As Range
    Set ws = Sheet1
    Application.ScreenUpdating = False
    lr = ws.Cells(Rows.Count, 2).End(3).Row
    i = 1
    For Each c In ws.Range("A2:A" & lr)
        c.ClearContents
        If c.EntireRow.Hidden = False Then
            c = i: i = i + 1
        End If
    Next c
    Application.ScreenUpdating = True
End Sub

Before applying a filter

test.xlsb
ABC
1filter16
21a
32b
43a
54b
65a
76b
87a
98b
109a
1110b
1211c
1312c
1413d
1514c
1615c
Sheet1
Cell Formulas
RangeFormula
C1C1=COUNTA(B1:B16)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$B$1:$B$16C1


after filtering for a & c

test.xlsb
ABC
1filter16
21a
42a
63a
84a
105a
126c
137c
158c
169c
17
Sheet1
Cell Formulas
RangeFormula
C1C1=COUNTA(B1:B16)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$B$1:$B$16C1


after filtering for b & d

test.xlsb
ABC
1filter16
31b
52b
73b
94b
115b
146d
17
Sheet1
Cell Formulas
RangeFormula
C1C1=COUNTA(B1:B16)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$B$1:$B$16C1
 
Upvote 0
There may be more elegant ways of doing it, but this method does what you seem to be after. You haven't provided much information so the following is provided for demonstration purposes only. You can change the column and cell references to suit your sheet layout.
Basically, it uses a Worksheet_Calculate() private sub to trigger the event. There is a CountA() formula in the sample in cell C1 - you can put it anywhere on the sheet and hide the column it's in if need be.

The code which goes in the relevant sheet code area:

VBA Code:
Private Sub Worksheet_Calculate()
    Dim ws As Worksheet, lr As Long, i As Long, c As Range
    Set ws = Sheet1
    Application.ScreenUpdating = False
    lr = ws.Cells(Rows.Count, 2).End(3).Row
    i = 1
    For Each c In ws.Range("A2:A" & lr)
        c.ClearContents
        If c.EntireRow.Hidden = False Then
            c = i: i = i + 1
        End If
    Next c
    Application.ScreenUpdating = True
End Sub

Before applying a filter

test.xlsb
ABC
1filter16
21a
32b
43a
54b
65a
76b
87a
98b
109a
1110b
1211c
1312c
1413d
1514c
1615c
Sheet1
Cell Formulas
RangeFormula
C1C1=COUNTA(B1:B16)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$B$1:$B$16C1


after filtering for a & c

test.xlsb
ABC
1filter16
21a
42a
63a
84a
105a
126c
137c
158c
169c
17
Sheet1
Cell Formulas
RangeFormula
C1C1=COUNTA(B1:B16)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$B$1:$B$16C1


after filtering for b & d

test.xlsb
ABC
1filter16
31b
52b
73b
94b
115b
146d
17
Sheet1
Cell Formulas
RangeFormula
C1C1=COUNTA(B1:B16)
Named Ranges
NameRefers ToCells
_FilterDatabase=Sheet1!$B$1:$B$16C1
Ta Kevin, but think I found exactly what I am after. I assumed something like SUBTOTAL or AGGREGATE may be the answer and finally found it.

=SUBTOTAL(3,$AG$21:AG21)

This is the baby which handles it.

And the below now takes care of the P&L dynamically as well

=SUBTOTAL(109,$I$21:I21)

Cheers
 
Upvote 0
Ta Kevin, but think I found exactly what I am after. I assumed something like SUBTOTAL or AGGREGATE may be the answer and finally found it.

=SUBTOTAL(3,$AG$21:AG21)

This is the baby which handles it.

And the below now takes care of the P&L dynamically as well

=SUBTOTAL(109,$I$21:I21)

Cheers
I knew there had to be a simpler method ? Thanks for letting me know ?
 
Upvote 0
.. and finally found it.

=SUBTOTAL(3,$AG$21:AG21)

This is the baby which handles it.

I hope that 'finally' didn't take you a long time after post #2 above as the solution was right there at the beginning of that link @footoo provided. :cool:


Thanks for letting me know ?
footoo had already let you know as per my comment above. ;)
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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