VBA: playing with arrays and dictionaries

Nelson78

Well-known Member
Joined
Sep 11, 2017
Messages
526
Office Version
  1. 2007
Hello everybody.

I've a situation as follows:

https://imgur.com/6FS0Fqw

I need to calculate how many different cities (column H) have code 233 (column D) and list aaa (column K)

The desired result is in cell N16 = 2, meaning the 2 records in green (in red the not considered duplicates).

I think I need a dictionary.

Thank's in advance.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
you can use a formula in n16 such as :
=COUNTIFS(D1:D14,233,K1:K14,"aaa")

Or vba

Code:
Sub test()
    Sheet1.Range("N16") = Evaluate("=COUNTIFS(Sheet1!D1:D14,233,Sheet1!K1:K14,""aaa"")")
End Sub
 
Last edited by a moderator:
Upvote 0
No, using the solutions you've proposed, the result is 4 (it considers duplicates).
I need 2 (no duplicates).
 
Last edited by a moderator:
Upvote 0
you can use a formula in n16 such as :
=COUNTIFS(D1:D14,233,K1:K14,"aaa")

Or vba

Code:
Sub test()
    Sheet1.Range("N16") = Evaluate("=COUNTIFS(Sheet1!D1:D14,233,Sheet1!K1:K14,""aaa"")")
End Sub

Maybe something like this:

Code:
Sub count()
  
  Dim X, vRws
  Dim objDict As Object
  Dim lngRow As Long, lngLastRow

  Set objDict = CreateObject("Scripting.Dictionary")
  objDict.CompareMode = 1
  With Sheets(1)
    lngLastRow = .Range("K" & .Rows.Count).End(xlUp).Row
    vRws = Evaluate("row(2:" & lngLastRow & ")")
    X = Application.Index(.Cells, vRws, Array(4, 8, 11))  
  End With
  For lngRow = 1 To UBound(X, 1)
    objDict(X(lngRow, 1) & "|" & X(lngRow, 2) & "|" & X(lngRow, 3)) = 1
  Next


'??????????

end sub

Now, in
Code:
objDict
I have all the different combinations of columns D/H/K: how can I count the relevant ones?
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Jul53
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("H2", Range("H" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Dn.Offset(, 3).Value = "aaa" And _
    Dn.Offset(, -4).Value = 233 [COLOR="Navy"]Then[/COLOR] .Item(Dn.Value) = Empty
[COLOR="Navy"]Next[/COLOR]
Range("N16") = .Count
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Jul53
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range
[COLOR="Navy"]Set[/COLOR] Rng = Range("H2", Range("H" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Dn.Offset(, 3).Value = "aaa" And _
    Dn.Offset(, -4).Value = 233 [COLOR="Navy"]Then[/COLOR] .Item(Dn.Value) = Empty
[COLOR="Navy"]Next[/COLOR]
Range("N16") = .Count
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick

I'm adapting it to the real situation, and it seems to work fine.

But I'm bumping into a secondary problem: I've a button to trigger the macro in a different sheet that causes the following error:

Code:
run-time error '1004' Application-defined or Object-defined error

How can I handle it?
 
Upvote 0
Try Modifying the "Set Rng" line to the below
Code:
With Sheets("Sheet1") ' Change to your sheet name instead of "sheet1"
   Set Rng = [COLOR="#FF0000"][SIZE=4].[/SIZE][/COLOR]Range("H2", [COLOR="#FF0000"][SIZE=4].[/SIZE][/COLOR]Range("H" & Rows.Count).End(xlUp))

End With

NB:- Don't forget the Dots in Red !!!!
 
Last edited:
Upvote 0
I would love to see this done with an array formula alone ... I have tried but no success.
 
Upvote 0
You could use something like:

=COUNT(1/FREQUENCY(IF(K2:K14="aaa",IF(D2:D14=233,MATCH(H2:H14,H2:H14,0))),MATCH(H2:H14,H2:H14,0)))

entered normally.
 
Upvote 0
You could use something like:

=COUNT(1/FREQUENCY(IF(K2:K14="aaa",IF(D2:D14=233,MATCH(H2:H14,H2:H14,0))),MATCH(H2:H14,H2:H14,0)))

entered normally.

I haven't dissected the formula to study it but a quick test gave me the wrong result - It always returns 1

Thank you.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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