Index/Match formula to VBA

Drawleeh

New Member
Joined
Sep 2, 2021
Messages
34
Office Version
  1. 2016
  2. 2010
Platform
  1. Windows
I have a spreadsheet that I have set up to work with formulas currently but I would like some help creating it within VBA.

I have column A and B which are entry columns, one contains a number the other contains a country ISO code. Then in a separate column I concatenate these 2 columns into one column via =A2&" "&B2

Then I use a INDEX/MATCH formula =IFERROR(INDEX($U$2:$U$249, MATCH(0, INDEX(COUNTIF($I$1:I1, $U$2:$U$249), 0, 0), 0)), "") to get a list of unique results in a third Column indexed from the concatenated list.

How do I go about changing this to VBA? Any help would be appreciated.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
1635177040745.png

Inserting an imagine to clarify the above.
 
Upvote 0
Either of these should work.

Book1 (version 2).xlsb
ABCD
1CodeOriginConcatenateUnique Code
232131000GB32131000 GB32131000 GB
396082000GB96082000 GB96082000 GB
486082000GB86082000 GB86082000 GB
596081010JP96081010 JP96081010 JP
648025890JP48025890 JP48025890 JP
796082000JP96082000 JP96082000 JP
832131000JP32131000 JP32131000 JP
996082000GB96082000 GB96081010 GB
1096082000GB96082000 GB
1196081010GB96081010 GB
1248025890JP48025890 JP
1396082000JP96082000 JP
Sheet4
Cell Formulas
RangeFormula
C2:C13C2=A2&" "&B2


VBA Code:
Sub UNIK()
Dim r As Range:         Set r = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If Not .exists(AR(i, 1)) Then .Add AR(i, 1), 1
    Next i
    Debug.Print .Count
    r.Offset(, 1).Resize(.Count).Value2 = Application.Transpose(.keys)
End With
End Sub

Sub IM()
Dim r As Range:     Set r = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)

With r.Offset(, 1)
    .Formula = "=IFERROR(INDEX(" & r.Address & ", MATCH(0, INDEX(COUNTIF($D$1:D1, " & r.Address & "), 0, 0), 0)), """")"
    .Value2 = .Value2
End With
End Sub
 
Upvote 0
Either of these should work.

Book1 (version 2).xlsb
ABCD
1CodeOriginConcatenateUnique Code
232131000GB32131000 GB32131000 GB
396082000GB96082000 GB96082000 GB
486082000GB86082000 GB86082000 GB
596081010JP96081010 JP96081010 JP
648025890JP48025890 JP48025890 JP
796082000JP96082000 JP96082000 JP
832131000JP32131000 JP32131000 JP
996082000GB96082000 GB96081010 GB
1096082000GB96082000 GB
1196081010GB96081010 GB
1248025890JP48025890 JP
1396082000JP96082000 JP
Sheet4
Cell Formulas
RangeFormula
C2:C13C2=A2&" "&B2


VBA Code:
Sub UNIK()
Dim r As Range:         Set r = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If Not .exists(AR(i, 1)) Then .Add AR(i, 1), 1
    Next i
    Debug.Print .Count
    r.Offset(, 1).Resize(.Count).Value2 = Application.Transpose(.keys)
End With
End Sub

Sub IM()
Dim r As Range:     Set r = Range("C2:C" & Range("C" & Rows.Count).End(xlUp).Row)

With r.Offset(, 1)
    .Formula = "=IFERROR(INDEX(" & r.Address & ", MATCH(0, INDEX(COUNTIF($D$1:D1, " & r.Address & "), 0, 0), 0)), """")"
    .Value2 = .Value2
End With
End Sub

They both do provide a index/match outcome however would it be possible to reduce the number of columns and remove column C, so the concatenation of column A and B occurs only within the code and that data is then index/matched?
 
Upvote 0
How about
VBA Code:
Sub Drawleeh()
   Dim Ary As Variant
   Dim r As Long
   
   Ary = Range("A2:B" & Range("A" & Rows.Count).End(xlUp).Row).Value2
   
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         .Item(Ary(r, 1) & " " & Ary(r, 2)) = Empty
      Next r
      Range("C2").Resize(.Count).Value = Application.Transpose(.Keys)
   End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,014
Messages
6,122,697
Members
449,092
Latest member
snoom82

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