vba - add collection to Dictionary - dictionnary to worksheet

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I am adding all rows to collection, and from collection adding to Dictionary.
and then from Dictionary to worksheet. I saw one video on youtube Make Your VBA Code Run 1000 Faster Part-2.

its not pasting as expected. Thanks

Input Data:-
Book1
ABCDEFGH
112701159191913741426103310431462
211621579194616441951187110541665
318511699177616101722122117021995
415521591104710081338119717451802
513371635183615801724140917491407
614261458199819681773119110061749
718381860144613401744198818621034
819021696164417891518152617721360
911381455181718381280120210581940
1017771127123118971430195616531324
1116181238158212471648115917091716
1211591752180610151510150811601299
1310741537177213471380143414111707
1418941200182014311593158010591048
1516121834124315601103186816461681
1616061146108818231221156713501932
1716601195178312641448109718701652
1813391772165310651825175712791731
1912191556122310471502137710531839
2019661878187716751826147213471493
2115451805172812441986179919191109
Sheet1



VBA Code:
[CODE=vba]Option Explicit

Sub ReadRows()

    Dim coll As New Collection, i As Long
    
    'Read Data to Collection - all rows
    Dim rg As Range
    Set rg = Range("A1").CurrentRegion
    
    For i = 1 To rg.Rows.Count
        coll.Add rg.Rows(i)
    Next i
    
    'Add Collection to Dictionary
    ReadCollectionToDictionary coll

End Sub
Private Function ReadCollectionToDictionary(ByVal coll As Collection) As Object

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    
    Dim i As Long
    For i = 1 To coll.Count
        dict.Add i, coll(i)
    Next i
    
    Set ReadCollectionToDictionary = dict
    WriteDictionaryToWorksheet dict

End Function


Private Sub WriteDictionaryToWorksheet(dict As Object)
    Range("A1").CurrentRegion.ClearContents

    Dim row As Long
    row = 1
        Cells(row, 1).Resize(dict.Count, 1) = WorksheetFunction.Transpose(dict.keys)
        Cells(row, 2).Resize(dict.Count, 1) = WorksheetFunction.Transpose(dict.items)
End Sub
[/CODE]
 
Hi Danteamor,

Thanks for providing another option ,
I liked Array approach . Can you add comment to understand it perfectly. Thanks

a = Sheets(1).Range("A1:E" & Sheets(1).Range("A" & Rows.Count).End(3).Row).Value2
ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
For i = 1 To UBound(a)
If dic1.Exists(a(i, 1)) And dic2.Exists(a(i, 2)) And a(i, 3) > 500 Then
k = k + 1
For j = 1 To UBound(a, 2)
b(k, j) = a(i, j)
Next j

End If
Next i

Sheets(1).Range("I2").Resize(k, UBound(b, 2)).Value = b


Thanks
mg
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You're very welcome, glad I could help.

And, it looks like Dante has given you a few options, so that's great.

Cheers!
 
Upvote 0
Can you add comment to understand it perfectly

How about:

VBA Code:
Sub FilterData_2()
  Dim arr_country As Variant, arr_region As Variant
  Dim dic1 As Object, dic2 As Object
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long, k As Long
  
  arr_country = Array("India", "America")
  arr_region = Array("North", "South")
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  
  'Put the countries in the dictionary
  For i = 0 To UBound(arr_country)
    dic1(arr_country(i)) = Empty
  Next
  'Put the regions in the dictionary
  For i = 0 To UBound(arr_region)
    dic2(arr_region(i)) = Empty
  Next
  
  'populate array 'a' with range of cells
  a = Sheets(1).Range("A1:E" & Sheets(1).Range("A" & Rows.Count).End(3).Row).Value2
  'resize the output
  ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
  'cycle from 0 to number of rows in array 'a'
  For i = 1 To UBound(a)
    'if country 'a(i, 1)' is in the country dictionary. And
    'the region 'a(i, 2)' is in the region dictionary. And
    'sales are greater than 500.
    If dic1.exists(a(i, 1)) And dic2.exists(a(i, 2)) And a(i, 3) > 500 Then
      'the variable 'k' counts the rows in the output.
      k = k + 1
      'cycle from 1 to the number of columns in array 'a'
      For j = 1 To UBound(a, 2)
        'fill the output in array 'b'(row, column) = with the value of array 'a'
        b(k, j) = a(i, j)
      Next j
    End If
  Next i
  'pass array 'b' to cells
  'starts in cell I2, resizes the output to the number of rows 'k' and the number of columns in array 'b'
  Sheets(1).Range("I2").Resize(k, UBound(b, 2)).Value = b
End Sub
 
Upvote 0
Hi Dante,

Thanks once again , for adding comment to make it still more simpler. thanks (y) ?


Thanks
mg
 
Upvote 0
If I am not mistaken, I believe this code will also do what the OP asked for...
VBA Code:
Sub FilterData()
  Dim LastRow As Long
  LastRow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
  Sheets(1).Range("C2:C" & LastRow) = Evaluate(Replace(Replace("IF(((@A2:A#=""India"")+(@A2:A#=""America""))*((@B2:B#=""North"")+(@B2:B#=""South""))*(@C2:C#>500),""=""&@C2:C#,@C2:C#)", "#", LastRow), "@", Sheets(1).Name & "!"))
  Intersect(Sheets(1).Columns("C").SpecialCells(xlFormulas).EntireRow, Sheets(1).Columns("A:E")).Copy
  Sheets(1).Range("I2").PasteSpecial xlValues
  Application.CutCopyMode = False
  Sheets(1).Columns("C").Replace "=", "", xlPart, , , , False, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,946
Members
449,198
Latest member
MhammadishaqKhan

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