VLookup rather than Loop

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
224
Office Version
  1. 365
Platform
  1. Windows
Hi,

<p>
Currently, I use For, Next loops to match and return values from two worksheets and populate col BC of WS1.
The values of Col AG in WS1 have 9 chars and on WS2 there are 2 cols, Col A has 9 chars and Col B has 12 chars.

For example, the value in Col AG of WS1,912828YG9 has no match on Col A of WS2 but has a match on Col B as US912828YG91

Currently, I use the below code which works perfectly

</code>
With Sheets("TSS Trans").Cells(1).CurrentRegion.Resize(, 55)
Data = .Value
With Sheets("HQLA_Pools").Cells(1).CurrentRegion
For i = 2 To UBound(Data)
Chk = Application.Match(Data(i, 33), .Columns(1), 0)
If IsError(Chk) Then
Chk = Application.Match("*" & Data(i, 33) & "*", .Columns(2), 0)
End If
If Not IsError(Chk) Then Data(i, 55) = .Cells(Chk, 3)
Next i
End With
.Value = Data
End With

ws.Range("BC:BC").NumberFormat = "@"
</code>

But since the data volume is large and I re-use this code for two other WS, the entire process consumes close to 8-10 mins.

Can this be done using VLookup using an IF condition?

I am not sure how to use the second column here, using the IF condition. The below VLookup matches one Col in WS2.

<code>
'ws.Range("BC2:BC" & ws.Cells(Rows.Count, "AG").End(xlUp).Row).FormulaR1C1 = "=VLOOKUP(""*""&RC[-22]&""*"",HQLA!C1:C3,3,0)"
</code>

</p>

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You are half way there in terms of speeding this up since you load the some of data into a variant array ( Data) , however doing a match on three columns of data for every row in data is very slow. There is much faster way which is to load the data in HQLA POOL into a three dictionaries and then you can use those directly in vba to do the looup. This saves thousandes of accesses to hte worksheet and so the macro should take seconds not minutes, try this:
VBA Code:
   Dim Dic1 As Object
   Set Dic1 = CreateObject("Scripting.dictionary")
   Dim Dic2 As Object
   Set Dic2 = CreateObject("Scripting.dictionary")

With Sheets("TSS Trans").Cells(1).CurrentRegion.Resize(, 55)
Data = .Value
With Sheets("HQLA_Pools").Cells(1).CurrentRegion
Ary = .Value
   For i = 1 To UBound(Ary)
      Dic1(Ary(i, 1)) = Ary(i, 3)
      Dic2(Ary(i, 2)) = Ary(i, 3)

   Next i
For i = 2 To UBound(Data)
  If Dic1.Exists(Data(i, 33)) Then
     Data(i, 55) = Dic1(Data(i, 33))
   End If
  If Dic2Exists(Data(i, 33)) Then
     Data(i, 55) = Dic2(Data(i, 33))
   End If

End With
.Value = Data
End With
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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