Vlookup Macro fine-tuning

aaleem

Board Regular
Joined
Sep 26, 2014
Messages
56
Office Version
  1. 2016
Hi,

i have the below macro in one of the file which has more than 100,000+ records.

this macro is taking approx. 10 minutes to run.

is there any way we can speed up the process?
Any suggestion will be much apprciated.

VBA Code:
Sub VlookupLocation()

Dim authorWs As Worksheet, detailsWs As Worksheet
Dim authorsLastRow As Long, detailsLastRow As Long, x As Long
Dim dataRng As Range

Set authorWs = ThisWorkbook.Worksheets("Unmatched GRN Report")
Set detailsWs = ThisWorkbook.Worksheets("Loc_Status")

authorsLastRow = authorWs.Range("A" & Rows.Count).End(xlUp).Row
detailsLastRow = detailsWs.Range("A" & Rows.Count).End(xlUp).Row

Set dataRng = detailsWs.Range("A2:L" & detailsLastRow)



For x = 2 To authorsLastRow
    On Error Resume Next
    
    If authorWs.Range("AD" & x).Value = "" Then
    
    authorWs.Range("AD" & x).Value = Application.WorksheetFunction.VLookup( _
    authorWs.Range("G" & x).Value, dataRng, 2, False)
    
    authorWs.Range("AG" & x).Value = Application.WorksheetFunction.VLookup( _
    authorWs.Range("G" & x).Value, dataRng, 5, False)
    
    authorWs.Range("AI" & x).Value = Application.WorksheetFunction.VLookup( _
    authorWs.Range("G" & x).Value, dataRng, 7, False)
    
    authorWs.Range("AL" & x).Value = Application.WorksheetFunction.VLookup( _
    authorWs.Range("G" & x).Value, dataRng, 10, False)
    
    
    authorWs.Range("AM" & x).Value = Application.WorksheetFunction.VLookup( _
    authorWs.Range("G" & x).Value, dataRng, 11, False)
    
    authorWs.Range("AN" & x).Value = Application.WorksheetFunction.VLookup( _
    authorWs.Range("G" & x).Value, dataRng, 12, False)
    
    Else
    End If
    
Next x

End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
May be:
VBA Code:
Option Explicit
Sub VlookupLocation()
Dim authorWs As Worksheet, detailsWs As Worksheet
Dim authorsLastRow As Long, detailsLastRow As Long, x As Long, col
Dim dataRng As Range
Set authorWs = ThisWorkbook.Worksheets("Unmatched GRN Report")
Set detailsWs = ThisWorkbook.Worksheets("Loc_Status")
authorsLastRow = authorWs.Range("A" & Rows.Count).End(xlUp).Row
detailsLastRow = detailsWs.Range("A" & Rows.Count).End(xlUp).Row
Set dataRng = detailsWs.Range("A2:L" & detailsLastRow)
authorWs.Cells.AutoFilter field:=30, Criteria1:="" ' Create an autofilter for column AD =""
    On Error Resume Next
    For Each col In Array(2, 5, 7, 10, 11, 12) ' loop for each column_index of VLOOKUP formula
        With authorWs.Range(Cells(2, 28 + col), Cells(authorsLastRow, 28 + col))
            .Formula = "=VLOOKUP(G2,Loc_Status!$A$2:$L$" & detailsLastRow & "," & col & ",0)"
            ' upper paste the formula. If you want value only, just add 1 more line as below:
            '.Value = .Value
        End With
    Next
authorWs.AutoFilterMode = False ' remove Autofilter
End Sub
 
Upvote 0
Dear bebo021999,

thank you so much for your help.

i tried running the above code im getting Run-time error '1004. Auto filter method of Range class failed on the below line of code
VBA Code:
authorWs.Cells.AutoFilter field:=30, Criteria1:="" ' Create an autofilter for column AD =""

the Sheet "Unmatched GRN Report" is having a Pivot Table by the name "Unmatched_GRN", is this error due to the Pivot table?

Also, I comment on this line of code and run the VBA, the Vlookup formula is getting pasted, is it possible instead of the Vlookup formula, that the value from the Loc_Status sheet should update.

Thank you again for your suggestions.
Aleem
 
Upvote 0
is it possible instead of the Vlookup formula, that the value from the Loc_Status sheet should update.
As my comment, jus restore:
.Value = .Value

About pivot table, I am not sure. Could you show how the sheet "Unmatched GRN Report" look like?
 
Upvote 0
Dear bebo021999,

I'm attaching a sample file, the original file has more than 100K records.
Sample sheet.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMAN
1SupplierSupplier NameOrder NoInvc Match StatusCurrency Code Exchange Rate To LocGRN NumberReceive DateUser IDCompanyVirtual LocationAppointment NumberHandle NumberQuantity Total Unit Cost GroupingConverted AEDUnique.Supplier CodeG/L ClassPmt TrmDescriptionNet Days to PayUnique Invoice DatesCalc.Due DateAgeFromDateRemarksSupplier ClassMerged Invoice No.Comb.StatusCom.Handle RefCurrent ApproverCompany.1Entity.NameEntity.Entity_GroupingUsernameUser job descDeptDept.1Responsible
2123297701Liverpool LLC8420040USAR1.021154443351089520118-May-2022454880.43DSD4,983.671232977OSO090Net 90 Days904468944779-67Not Yet DueOthers
3123297701Liverpool LLC8422178USAR1.0211544432310895895012-May-20220.948535962.164DSD36,722.921232977OSO090Net 90 Days904469344783-71Not Yet DueOthers
4123297701Liverpool LLC8416645USAR1.0211544432410895873912-May-2022242135.376DSD2,180.551232977OSO090Net 90 Days904469344783-71Not Yet DueOthers
5123297701Liverpool LLC8420058USAR1.0211544432410895873712-May-202247439700.206DSD40,540.041232977OSO090Net 90 Days904469344783-71Not Yet DueOthers
6123297701Liverpool LLC8420294USAR1.0211544432410895874212-May-20228913099.833DSD13,376.951232977OSO090Net 90 Days904469344783-71Not Yet DueOthers
7123297701Liverpool LLC8422171USAR1.0211544433510895863212-May-202242437383.777DSD38,174.611232977OSO090Net 90 Days904469344783-71Not Yet DueOthers
811534003EM Trading LLC8428500UAED1121810896095114-May-2022121018.332DSD1,018.33115340LST075Credit 75 Days754469544770-58Not Yet DueOthers
9123297701Liverpool LLC8422179USAR1.0211544433210896169214-May-202215313336.407DSD13,618.531232977OSO090Net 90 Days904469544785-73Not Yet DueOthers
10123297701Liverpool LLC8422167USAR1.0211544433010896317015-May-202243841721.615DSD42,604.211232977OSO090Net 90 Days904469644786-74Not Yet DueOthers
1115250009Beidoun Contracting8426459UKWD0.082494638880210896303415-May-20228431.46DSD35.59152500OST60Credit 60 Days604469644756-44Not Yet DueOthers
1210007503Intl. Est.8428326UAED1121410896431116-May-2022181195.173DSD1,195.17100075OSTP08Payment 90 Days (P)904469744787-75Not Yet DueOthers
1310010407GT IHE8428443UAED1122910896532717-May-20229017002.44DSD17,002.44100104GRPP04Payment 90 Days904469844788-76Not Yet DueOthers
14127075103ORIENTAL DE LA PARFUMERIE8429093UAED1850210896626117-May-202282237.544DSD2,237.541270751LST060Credit 60 Days604469844758-46Not Yet DueOthers
15127526704Aramex stocks8422812UAED1850210896538617-May-202271154.979DSD1,154.981275267LST090Net 90 Days904469844788-76Not Yet DueOthers
16128054506Jindal General Trading Co LLC8424444UAED1850210896575217-May-2022297088.17041DSD7,088.171280545LST090Net 90 Days904469844788-76Not Yet DueOthers
17123297701Liverpool LLC8416644USAR1.0211544432510896636117-May-2022242135.376DSD2,180.551232977OSO090Net 90 Days904469844788-76Not Yet DueOthers
18123297701Liverpool LLC8420295USAR1.0211544432510896635817-May-20227110275.687DSD10,493.061232977OSO090Net 90 Days904469844788-76Not Yet DueOthers
1946026505Liverpool Middle East FZE8420057UKWD0.082494638874110896795718-May-20221182.674DSD6.82460265LSOP03Payment 60 Days604469944759-47Not Yet DueOthers
Unmatched GRN Report
Cell Formulas
RangeFormula
R2:R19R2=+[@[Total Unit Cost]]*[@[Exchange Rate]]


kind regards
aleem
 
Upvote 0
Dear bebo021999

please find attached the other sample sheet.

thanks
aleem


Sample sheet.xlsx
ABCDEFGHIJKL
1To LocComb.StatusCom.Handle RefCurrent ApproverCompany.1Entity.NameEntity.Entity_GroupingUsernameUser job descDeptDept.1Responsible
2101Pending with Warehouse-GRN showing in Microstrategy/No HandlesOunassLogisticsLogisticsDee
31214Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
41218Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
51229Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
6201Pending with Warehouse-GRN showing in Microstrategy/No HandlesB&MLogisticsLogisticsDee
72416Pending with Stores- Drag and drop not done/Handle not createdB&MStoresStoresStores
82419Pending with Stores- Drag and drop not done/Handle not createdB&MStoresStoresStores
9301Pending with Warehouse-GRN showing in Microstrategy/No HandlesB&MLogisticsLogisticsDee
103764Pending with Stores- Drag and drop not done/Handle not createdB&MStoresStoresStores
114020Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
124323Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
134324Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
144325Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
154330Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
164332Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
174335Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
18501Pending with Warehouse-GACBeautyLogisticsLogisticsDee
196824Pending with Stores- Drag and drop not done/Handle not createdB&MStoresStoresStores
206911Pending with Stores- Drag and drop not done/Handle not createdB&MStoresStoresStores
21701Pending with Warehouse-GRN showing in Microstrategy/No HandlesBeautyLogisticsLogisticsDee
22801Pending with Warehouse-GRN showing in Microstrategy/No HandlesBeautyLogisticsLogisticsDee
238502Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
248741Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
258742Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
268802Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
278302Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
288315Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
294341Pending with Stores- Drag and drop not done/Handle not createdBeautyStoresStoresStores
306823Pending with Stores- Drag and drop not done/Handle not createdB&MStoresStoresStores
Loc_Status
 
Upvote 0
Try to test with plain sheets that you gave (without Pivot)
VBA Code:
Option Explicit
Sub VlookupLocation()
Dim authorWs As Worksheet, detailsWs As Worksheet
Dim authorsLastRow As Long, detailsLastRow As Long, x As Long, col
Dim dataRng As Range
Set authorWs = ThisWorkbook.Worksheets("Unmatched GRN Report")
Set detailsWs = ThisWorkbook.Worksheets("Loc_Status")
authorsLastRow = authorWs.Range("A" & Rows.Count).End(xlUp).Row
detailsLastRow = detailsWs.Range("A" & Rows.Count).End(xlUp).Row
Set dataRng = detailsWs.Range("A2:L" & detailsLastRow)
authorWs.Cells.AutoFilter field:=30, Criteria1:="" ' Create an autofilter for column AD =""
    On Error Resume Next
    For Each col In Array(2, 5, 7, 10, 11, 12) ' loop for each column_index of VLOOKUP formula
        With authorWs.Range(Cells(2, 28 + col), Cells(authorsLastRow, 28 + col))
            .Formula = "=VLOOKUP(G2,Loc_Status!$A$2:$L$" & detailsLastRow & "," & col & ",0)"
            .Value = .Value
        End With
    Next
authorWs.AutoFilterMode = False ' remove Autofilter
End Sub
It seems OK
 

Attachments

  • Capture2.JPG
    Capture2.JPG
    190.9 KB · Views: 12
Upvote 0
Dear bebo021999,

thank you so much, yes this is working fine, I have converted the table to a range then it is working.
Also, the time has improved a lot, it is now running in a few seconds only.

Thank you so much for your help.

kind regards
Aleem
 
Upvote 0
when faced by this sort of problem you might want to look at dictionaries which can do exactly the same thing and are reliably very fast, see this thread, which reduced a macro that took 1 hour 20 min utes to 10 seconds:
How to reduce number of loops in VBA
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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