Converting countif to VBA dictionary

Guinaba

Board Regular
Joined
Sep 19, 2018
Messages
218
Office Version
  1. 2016
Platform
  1. Windows
Hi experts,

I have the formula below helping me to create an index col in my sheet, However, this is making my code too slow.

Does anyone know how to convert it to dictionary structure?

With Range("c2", Range("c" & Rows.Count).End(xlUp))

.Offset(0, -2).FormulaR1C1 = "=COUNTIF(R1C[2]:RC[2], RC[2])"

End With
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
How about:

VBA Code:
Sub test()
  Dim a As Variant, b As Variant
  Dim dic As Object, i As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("C2", Range("C" & Rows.Count).End(xlUp)).Value2
  ReDim b(1 To UBound(a), 1 To 1)
  For i = 1 To UBound(a)
    dic(a(i, 1)) = dic(a(i, 1)) + 1
    b(i, 1) = dic(a(i, 1))
  Next
  Range("A2").Resize(UBound(a)).Value = b
End Sub
 
Upvote 0
Hi @DanteAmor,
I am trying to understand your code do you mind telling why do you need two arrays? Is one to be used for the dic. index the the other for the dic.value?
 
Upvote 0
I am trying to understand your code do you mind telling why do you need two arrays? Is one to be used for the dic. index the the other for the dic.value?
Technically there are 3 matrices, a) to store the original data, b) to store the results, dic) to store the keys and the counter of each key.
 
Upvote 0
Thanks Dante! Really liked it, I am going to learn more dictionary.
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,669
Messages
6,126,120
Members
449,293
Latest member
yallaire64

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