VBA array help-

Mallesh23

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

Need your help in below situation , trying to learn array and dictionary.

I want to store below lines of code into Array and print into range.

For Each Cl In Range("k2", Range("k" & Rows.Count).End(xlUp))
If .Exists(Cl.Value) Then
Cl.Offset(, 1) = .Item(Cl.Value)(0)
Cl.Offset(, 4) = .Item(Cl.Value)(1)
End If
Next Cl

Option Explicit

Rich (BB code):
Sub Workig_Code()
   
   Dim Cl As Range
   Dim i As Long     
   Dim rg As Range
  Dim arr As Variant
   Dim arr_out As Variant

   Set rg = Range("A1").CurrentRegion
   Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)
   arr = rg.Value
   Dim Dict As New Scripting.dictionary
   
   arr_out = Range("K2", Range("K" & Rows.Count).End(xlUp)).Value
   
   
   'Store Required Columns 4 and 7 as items in dictionary   
   Dict.RemoveAll   
   With Dict
        For i = LBound(arr, 1) To UBound(arr, 1)
                If Not .Exists(arr(i, 2)) Then
                    .Add arr(i, 2), Array(arr(i, 4), arr(i, 7))
                End If
        Next i
       
      
    For Each Cl In Range("k2", Range("k" & Rows.Count).End(xlUp))
         If .Exists(Cl.Value) Then
            Cl.Offset(, 1) = .Item(Cl.Value)(0)
            Cl.Offset(, 4) = .Item(Cl.Value)(1)
         End If
      Next Cl
   End With      
   
End Sub


Rich (BB code):
Sub Dict_Array()

    Dim i As Long
   
   
   Dim rg As Range
   Set rg = Range("A1").CurrentRegion
   Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)
   
   Dim arr As Variant
   arr = rg.Value
   
   
   Dim arr_out As Variant
   arr_out = Range("K2", Range("K" & Rows.Count).End(xlUp)).Value
   
   
   'Store Required Columns 4 and 7 as items in dictionary
   Dim Dict As New Scripting.dictionary
   
   Dict.RemoveAll
   
   With Dict
        For i = LBound(arr, 1) To UBound(arr, 1)
                If Not .Exists(arr(i, 2)) Then
                    .Add arr(i, 2), Array(arr(i, 4), arr(i, 7))
                End If
        Next i
                 
              
       ' Store Both Items Column into Array and Print    ' need your help below .
              
        For i = LBound(arr_out, 1) To UBound(arr_out, 1)
              arr_out(i, 1) = .Item(arr_out(i, 1)(0))
              arr_out(i, 1) = .Item(arr_out(i, 1)(1))
        Next i
       
       
      Range("L2:L17").Value = arr_out(0)
      Range("O2:O17").Value = arr_out(1)
      
End Sub

Below is the data, expected output is in Column L and O.


Multiple Column Look up.xlsx
BCDEFGHIJKLMNOP
1Player NamePeriodTeamTest CenturyODI CenturyTotalPlayer NameTeamTest CenturyODI CenturyTotal Century
2Sachin Tendulkar1989–2013 India5149100Sachin Tendulkar India100=VLOOKUP(K2,$B$1:$G$43,6,0)
3Ricky Ponting1995–2012 Australia413071Ricky Ponting Australia71
4Virat Kohli2008–2020 India274370Virat Kohli India70=VLOOKUP(K2,$B$1:$G$43,3,0)
5Kumar Sangakkara2000–2015 Sri Lanka382563Brian Lara West Indies53
6Jacques Kallis1995–2014 South Africa451762Rahul Dravid India48
7Hashim Amla2004–2019 South Africa282755AB de Villiers South Africa47
8Mahela Jayawardene1997–2015 Sri Lanka341953David Warner Australia42
9Brian Lara1990–2007 West Indies341953Sanath Jayasuriya Sri Lanka42
10Rahul Dravid1996–2012 India361248Chris Gayle West Indies27
11AB de Villiers2004–2018 South Africa222547Gary Kirsten South Africa47
12David Warner2009–2020 Australia241842Adam Gilchrist Australia43
13Sanath Jayasuriya1989–2011 Sri Lanka142842Joe Root England41
14Chris Gayle1989–2011 West Indies25227Kevin Pietersen England39
15Shivnarine Chanderpaul1989–2011 West Indies301141Saeed Anwar Pakistan39
16Ross Taylor1989–2011New Zealand707Allan Border Australia26
17Matthew Hayden1993–2009 Australia301040Don Bradman Australia56
18Gary Kirsten1993–2004 South Africa212647
19Kane Williamson1993–2004 New Zealand133043
20Adam Gilchrist1996–2008 Australia172643
21Joe Root2004–2014 England162541
22Kevin Pietersen2004–2014 England231639
23Javed Miandad1975–1996 Pakistan231538
24Aravinda de Silva1975–1996 Sri Lanka112435
25Saeed Anwar1975–1996 Pakistan201939
26Gordon Greenidge1974–1991 West Indies193049
27Allan Border1974–1991 Australia32326
28Don Bradman1928–1948 Australia292756
29Mohammad Azharuddin1928–1948 India71522
30Graham Gooch1975–1995 England201838
31Greg Chappell1970–1984 Australia242852
32Marvan Atapattu1970–1984 Sri Lanka112738
33Nathan Astle1970–1984 New Zealand162036
34Andrew Strauss1970–1984 England62531
35Garfield Sobers1970–1984 West Indies262955
36David Boon1970–1984 Australia53035
37Marcus Trescothick1970–1984 England121729
38Ian Bell1970–1984 England42529
39David Gower1970–1984 England182240
40Shikhar Dhawan2010–2020India72229
41Geoffrey Boycott1964–1982 England222244
42Justin Langer1964–1982 Australia232144
43V. V. S. Laxman2000-2014 India62329
Sheet1
Cell Formulas
RangeFormula
L2:L17L2=VLOOKUP(K2,$B$1:$G$43,3,0)
O2:O17O2=VLOOKUP(K2,$B$1:$G$43,6,0)
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Team,

I reattmpted I am getting Result as expected, But Can we shorten the Code,

I am getting result , but I using multiple loops here.

Rich (BB code):
Sub Dict_Array()
    Dim i As Long  
   Dim rg As Range
   Set rg = Range("A1").CurrentRegion
   Set rg = rg.Resize(rg.Rows.Count - 1).Offset(1)
  
   Dim arr As Variant
   arr = rg.Value
  
  
   Dim arr_out As Variant
   arr_out = Range("K2", Range("K" & Rows.Count).End(xlUp)).Value
  
  
   'Store Required Columns 4 and 7 as items in dictionary
   Dim dict As New Scripting.Dictionary
  
   dict.RemoveAll
  
   With dict
  
'======================================================================'======================================================================
         'Store Column 4 in Items and Print
         For i = LBound(arr, 1) To UBound(arr, 1)
                 If Not .Exists(arr(i, 2)) Then
                     .Add arr(i, 2), arr(i, 4) ', arr(i, 7))
                 End If
         Next i
                 
              
        ' Store Both Single Column into Array and Print
         For i = LBound(arr_out, 1) To UBound(arr_out, 1)
               arr_out(i, 1) = .Item(arr_out(i, 1))
         Next i
        
         End With
      
       Range("L2").Resize(UBound(arr_out, 1)).Value = arr_out
     
'======================================================================
            dict.RemoveAll
            'Store Column 7 in Items and Print
            arr_out = Range("K2", Range("K" & Rows.Count).End(xlUp)).Value
      
       With dict
         For i = LBound(arr, 1) To UBound(arr, 1)
                 If Not .Exists(arr(i, 2)) Then
                     .Add arr(i, 2), arr(i, 7) ', arr(i, 7))
                 End If
         Next i
                 
              
        ' Store Both Single Column into Array and Print
         For i = LBound(arr_out, 1) To UBound(arr_out, 1)
               arr_out(i, 1) = .Item(arr_out(i, 1))
         Next i   
                
          Range("O2").Resize(UBound(arr_out, 1)).Value = arr_out
     
      End With
                
End Sub


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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