Unique increment ID for each distinct value in a range

user1783736

I have a function (UDF) that creates a unique id for each distinct String value in a range. My problem is that I am using the row number for the firs occurrence found. I need: a unique ID that increments by one for each distinct value. Please take a look at the image of my current values and the actual values that I want:

enter image description here

I am using the the following formula in column B row 2:

 =insert_id1(A2,$A$2:$A$8)

Below is my UDF:

Function insert_id1(Search_string As String, Search_in_col As Range)
Dim i As Long
Dim result As Integer
result = 0

For i = 1 To Search_in_col.count

  If Search_in_col.Cells(i, 1) = Search_string Then
        If result = 0 Or result > Search_in_col.Cells(i, 1).Row Then
        result = Search_in_col.Cells(i, 1).Row
        End If
  End If

Next
insert_id1 = result
End Function
tigeravatar

Does it have to be VBA? Use this formula in cell B2 and copy down:

=IF(COUNTIF(A$1:A1, A2)=0,MAX(B$1:B1)+1,VLOOKUP(A2,A$1:B1,2,FALSE))

EDIT

If it really needs to be VBA/UDF:

Public Function insert_id1(Search_string As String, Search_in_col As Range) As Long

    Dim i As Long
    Dim sUnq As String
    Dim cel As Range

    For Each cel In Search_in_col
        If Len(cel.Text) > 0 _
        And InStr(1, "|" & sUnq & "|", "|" & cel.Text & "|", vbTextCompare) = 0 Then
            i = i + 1
            sUnq = sUnq & "|" & cel.Text
            If cel.Text = Search_string Then
                insert_id1 = i
                Exit Function
            End If
        End If
    Next cel

End Function

And then the formula becomes:

=insert_id1(A2,$A$2:$A$8)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

minimum value for each unique id

From Dev

Set auto_increment value to each unique group

From Dev

SQL Server increment the row number for each new unique value

From Dev

jQuery Set unique ID value increment from 1 instead of 0

From Dev

Identify unique/distinct values in a range

From Dev

SQL Server stored procedure : create increment ID in each occurrence of value

From Dev

Excel Vba Filter Range By Each Unique Value and Copy Data

From Dev

SQL Subquery / Subselect getting value from next row without auto increment id or any distinct fields to join on

From Dev

how can each user get increment and unique id on same table in django

From Dev

auto increment value in each loop

From Dev

Get Count For Each Distinct Value

From Dev

Pandas group by unique ID and Distinct date per unique ID

From Dev

Dynamo DB Increment/Unique id generation

From Dev

Dynamo DB Increment/Unique id generation

From Dev

AngularJS increment value for id attributes

From Dev

Increment id value in jQuery selector

From Dev

Create multiple concurrent POST requests, with JSON body having distinct ID value in each request

From Dev

Mysql - Get count of unique distinct ip for each hour, each day

From Dev

MongoDB Aggregation: calculation for every unique/distinct value

From Dev

Unique value if where id =?

From Dev

Select distinct column value from date range

From Dev

Select distinct column value from date range

From Dev

Generate unique id for each device

From Dev

show divs with unique id each

From Dev

Unique id for each row in mysqli

From Dev

Unique id for each instance of component

From Dev

Count the occurences of each unique string in a range

From Dev

Find range of values in each unique day

From Dev

Eloquent select the first of each distinct 'id'?

Related Related

  1. 1

    minimum value for each unique id

  2. 2

    Set auto_increment value to each unique group

  3. 3

    SQL Server increment the row number for each new unique value

  4. 4

    jQuery Set unique ID value increment from 1 instead of 0

  5. 5

    Identify unique/distinct values in a range

  6. 6

    SQL Server stored procedure : create increment ID in each occurrence of value

  7. 7

    Excel Vba Filter Range By Each Unique Value and Copy Data

  8. 8

    SQL Subquery / Subselect getting value from next row without auto increment id or any distinct fields to join on

  9. 9

    how can each user get increment and unique id on same table in django

  10. 10

    auto increment value in each loop

  11. 11

    Get Count For Each Distinct Value

  12. 12

    Pandas group by unique ID and Distinct date per unique ID

  13. 13

    Dynamo DB Increment/Unique id generation

  14. 14

    Dynamo DB Increment/Unique id generation

  15. 15

    AngularJS increment value for id attributes

  16. 16

    Increment id value in jQuery selector

  17. 17

    Create multiple concurrent POST requests, with JSON body having distinct ID value in each request

  18. 18

    Mysql - Get count of unique distinct ip for each hour, each day

  19. 19

    MongoDB Aggregation: calculation for every unique/distinct value

  20. 20

    Unique value if where id =?

  21. 21

    Select distinct column value from date range

  22. 22

    Select distinct column value from date range

  23. 23

    Generate unique id for each device

  24. 24

    show divs with unique id each

  25. 25

    Unique id for each row in mysqli

  26. 26

    Unique id for each instance of component

  27. 27

    Count the occurences of each unique string in a range

  28. 28

    Find range of values in each unique day

  29. 29

    Eloquent select the first of each distinct 'id'?

HotTag

Archive