Sequence each duplicate value in multi columns (Scripting Dictionary)

SamKhem

Board Regular
Joined
Mar 18, 2024
Messages
50
Office Version
  1. 2016
Platform
  1. Windows
Dear All Senior Members

I am new member in this thread. I would like to request you guide as vba with Sequence each duplicate value in multi columns (Scripting Dictionary).
Example:
Col. A Col.B Result: Col. C Col. D
aa cc 1 2
bb dd 1 2
cc aa 1 3
dd bb 1 2
aa dd 2 3

Thank in advance for your assist.
Best regards,
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Welcome to the MrExcel board!

Dear All Senior Members
Surely a junior member with a good solution would be just as good? ;)

Unless this is an assignment or test question, why specify Scripting Dictionary?
This seems to do what you want without the need for it.

VBA Code:
Sub test()
  Dim lr As Long
  
  lr = Columns("A:B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  With Range("C1:D" & lr)
    .Columns(1).FormulaR1C1 = "=IF(RC1="""","""",COUNTIF(R1C1:RC1,RC1))"
    .Columns(2).FormulaR1C1 = "=IF(RC2="""","""",COUNTIF(R1C1:R" & lr & "C1,RC2)+COUNTIF(R1C2:RC2,RC2))"
    .Value = .Value
  End With
End Sub

Here is my sheet after the code has run.
BTW, I suggest that you investigate XL2BB for providing sample data in the future.

SamKhem.xlsm
ABCD
1aacc12
2bbdd12
3ccaa13
4ddbb12
5aadd23
Sheet1
 
Upvote 0
Welcome to the MrExcel board!


Surely a junior member with a good solution would be just as good? ;)

Unless this is an assignment or test question, why specify Scripting Dictionary?
This seems to do what you want without the need for it.

VBA Code:
Sub test()
  Dim lr As Long
 
  lr = Columns("A:B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  With Range("C1:D" & lr)
    .Columns(1).FormulaR1C1 = "=IF(RC1="""","""",COUNTIF(R1C1:RC1,RC1))"
    .Columns(2).FormulaR1C1 = "=IF(RC2="""","""",COUNTIF(R1C1:R" & lr & "C1,RC2)+COUNTIF(R1C2:RC2,RC2))"
    .Value = .Value
  End With
End Sub

Here is my sheet after the code has run.
BTW, I suggest that you investigate XL2BB for providing sample data in the future.

SamKhem.xlsm
ABCD
1aacc12
2bbdd12
3ccaa13
4ddbb12
5aadd23
Sheet1
Thank so much Peter, Really appreciate your feedback, actually I need vba with scripting dictionary because I need to run vba million rows with multi columns.
 
Upvote 0
I need vba with scripting dictionary because I need to run vba million rows with multi columns.
OK, try something like this

VBA Code:
Sub test2()
  Dim d As Object
  Dim a As Variant
  Dim lr As Long, i As Long, j As Long, uba1 As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  lr = Columns("A:B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  With Range("A1:B" & lr)
   a = .Value
   uba1 = UBound(a, 1)
   For j = 1 To UBound(a, 2)
    For i = 1 To uba1
      If Len(a(i, j)) > 0 Then
        d(a(i, j)) = d(a(i, j)) + 1
        a(i, j) = d(a(i, j))
      End If
    Next i
   Next j
   .Offset(, .Columns.Count).Value = a
  End With
End Sub
 
Upvote 0
Solution
OK, try something like this

VBA Code:
Sub test2()
  Dim d As Object
  Dim a As Variant
  Dim lr As Long, i As Long, j As Long, uba1 As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  lr = Columns("A:B").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
  With Range("A1:B" & lr)
   a = .Value
   uba1 = UBound(a, 1)
   For j = 1 To UBound(a, 2)
    For i = 1 To uba1
      If Len(a(i, j)) > 0 Then
        d(a(i, j)) = d(a(i, j)) + 1
        a(i, j) = d(a(i, j))
      End If
    Next i
   Next j
   .Offset(, .Columns.Count).Value = a
  End With
End Sub
Great support.
Best wish
Have a nice weekend.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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