How to add a index number to a helper column

JeffGrant

Well-known Member
Joined
Apr 7, 2021
Messages
516
Office Version
  1. 365
Platform
  1. Windows
Hi All, I am having a real brain fade today.

My aim is to keep visible the first occurrence of a record, then hide the rest.

Bu I need to create a helper column such as this.

Book1
AB
1NameIndex
2Jeff1
3Jeff
4Jeff
5Tom1
6Tom
7Tom
8Tim1
9Tim
10Noel1
11Fred1
12Fred
13Joe1
14Joe
15Joe
Sheet1


Ordinarily I would research until I found a For Next Loop somewhere on the big www, but I have over 10,000 records, so I am hoping there is a better and more efficient way.

I am not familiar enough with PQ.

If a loop is the preferred way, can someone be kind enough to write one. I am sure what you folk can do in a matter of minutes will take me considerable time to find.

Much appreciated.

J.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Why not just use a formula
Excel Formula:
=IF(COUNTIF(A$2:A2,A2)=1,1,"")
 
Upvote 0
Solution
@JeffGrant Are you needing vba and the helper index?

If you apply advanced filter to the Name column and select Unique rcords only then it will hide all bar the first instance.
 
Upvote 0
VBA Code:
Option Explicit

Sub CountNames()
    Dim i As Long, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Range("B1") = "Index"
    Range("B2") = 1
    For i = 3 To lr
        If Range("A" & i) = Range("A" & i - 1) Then
            Range("B" & i) = ""
        Else: Range("B" & i) = 1
        End If
    Next i
    MsgBox "completed"
End Sub
Option Explicit
 
Upvote 0
If you do need a macro, another option is
VBA Code:
Sub JeffGrant()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long
   
   Ary = Range("A1").CurrentRegion.Value2
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   Nary(1, 1) = "Index"
   With CreateObject("scripting.dictionary")
      For r = 2 To UBound(Ary)
         If Not .exists(Ary(r, 1)) Then
            .Add Ary(r, 1), Nothing
            Nary(r, 1) = 1
         End If
      Next r
   End With
   Range("B1").Resize(UBound(Nary)).Value = Nary
End Sub
 
Upvote 0
@JeffGrant Are you needing vba and the helper index?

If you apply advanced filter to the Name column and select Unique rcords only then it will hide all bar the first instance.
Hi Snakehips,

Didn't realise I could that.

So I have selected the entire range
added the filter
clicked on advanced to select Unique records
entered the criteria range

and all it does is remove the filters and leave the block high lighted.

Any ideas why it would be doing that? What did I do wrong?
 
Upvote 0
Why not just use a formula
Excel Formula:
=IF(COUNTIF(A$2:A2,A2)=1,1,"")
Thanks Fluff, this was an easy solution.

A combination of this plus Snakehips did the trick.

I appreciated every body rapid assistance.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
VBA Code:
Option Explicit

Sub CountNames()
    Dim i As Long, lr As Long
    lr = Range("A" & Rows.Count).End(xlUp).Row
    Range("B1") = "Index"
    Range("B2") = 1
    For i = 3 To lr
        If Range("A" & i) = Range("A" & i - 1) Then
            Range("B" & i) = ""
        Else: Range("B" & i) = 1
        End If
    Next i
    MsgBox "completed"
End Sub
Option Explicit
Thanks Alan. I have another spot where I need to actually add lines where the number of records is less than 3, so that all names have three records. I need this because I need to fill data for a linear regression that needs to get done. I will adapt this code for that. Thanks again.
 
Upvote 0
Hi Snakehips,

Didn't realise I could that.

So I have selected the entire range
added the filter
clicked on advanced to select Unique records
entered the criteria range

and all it does is remove the filters and leave the block high lighted.

Any ideas why it would be doing that? What did I do wrong?
Just edit the range to be the name column only.
Screenshot 2021-07-11 at 13.20.26.png

Screenshot 2021-07-11 at 13.21.03.png
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,693
Members
449,048
Latest member
81jamesacct

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