Trying to use vlookup between sheets

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
My VLookup code is not doing anything??

VBA Code:
Sub VLookup_Min()


Dim Wb        As Workbook
Dim Src       As Worksheet, Des As Worksheet
Dim SrcLRow   As Long, DesLRow
Dim x         As Variant
Dim SrcRng    As Range, DesRng As Range

Set Wb = ThisWorkbook
Set Src = Wb.Worksheets("Intact Detail")
Set Des = Wb.Worksheets("Alton")
SrcLRow = Src.Range("A" & Rows.Count).End(xlUp).Row
DesLRow = Des.Range("A" & Rows.Count).End(xlUp).Row
Set DesRng = Des.Range("A2:A" & DesLRow)
Set SrcRng = Src.Range("A2:A" & SrcLRow)

Src.Range("O2:O" & SrcLRow).AutoFilter Field:=1, Criteria1:="<>"

For Each x In SrcRng.SpecialCells(xlCellTypeVisible)
On Error Resume Next
    Src.Range("A" & x).Value = Application.WorksheetFunction.VLookup( _
    Src.Range("O" & x).Value, DesRng, 8, False)
Next x

If Src.AutoFilterMode Then Src.AutoFilter.ShowAllData

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Your DesRng is just column A (1 column) and you are asking for it to return column 8.
The resume next is masking the error.
 
Upvote 0
Your DesRng is just column A (1 column) and you are asking for it to return column 8.
The resume next is masking the error.
Thanks that`s sorted now A to I but next issue is the code below says error "method object of range worksheet failed"?

VBA Code:
Src.Range("A" & x).Value = Application.WorksheetFunction.VLookup( _
    Src.Range("O" & y).Value, DesRng, 8, False)
 
Last edited:
Upvote 0
Sorry back to x but still not working

VBA Code:
Src.Range("A" & x).Value = Application.WorksheetFunction.VLookup( _
    Src.Range("O" & x).Value, DesRng, 8, False)
 
Upvote 0
You are going to need to show us what the sheet Source looks like.
I think you are intending to filter on Column O but if you click on the heading of Column O and hit Ctrl+A, if that selectes multiple column then the filter is being applied to whatever is the first column of what is now highlighted.

Secondly you are using x as if it is a row number it is actually a cell in column A, so on the left side of the "=" it would not be
Src.Range("A" & x).Value =
it would just be
x.Value =
and
Src.Range("O" & x).Value
would be
Src.Range("O" & x.Row).Value,
 
Upvote 0
Thanks for the help above but still not working here is the source range

ProductDescriptionBranch Available STKCategory CodeTypeCompany Effective STKTotal Company STKLast Cost ActualV Line SpecialDefault SupplierManufacturerAlternative CodeMaximum Stock LevelMinimum Stock LevelSales Quantity
SPECIALSpecial-3SPECIALSpecial292961.07NoNo Default Supplier00464
12777Frost Body B19 Medium Sump MJ 100mm Outlet211Standard37932911.29NoHEBEI METALS & MINERALS CORP LTD001
12798Frost Body B21 side outlet. 111Standard123837NoHEBEI METALS & MINERALS CORP LTD00
13034Frost Body B15 (cast iron) BSP 4" Internal (Female) Thread 111Standard35425411.86NoHEBEI METALS & MINERALS CORP LTD00
13300Frost Body B19 external 4 inch NPS(Male) External Thread 111Standard119190.9383NoHEBEI METALS & MINERALS CORP LTD00
13333Frost Body B22 BSP 4" Side Internal (Female) Thread111Standard201119.65NoHEBEI METALS & MINERALS CORP LTD00
13610Frost Body B2 MJ 150mm Outlet (M6 version) 111Standard1434313.3291NoHEBEI METALS & MINERALS CORP LTD00
13613Frost Body B2 BSP 4" Internal (Female) Thread (M6 version) 111Standard47632624NoHEBEI METALS & MINERALS CORP LTD00
15049Frost Perforated Filter pipe for 33.0454.000 planter drain assembly 150mm long 111Standard10810820.43NoFine Line Sheet Metal Engineering Ltd00
150RTWP150mm Twinwall Duct x6m; Purple 11TWDUStandard111118.49YesNaylor Drainage Ltd00
20VGGRID20/20 Biaxial Geogrid 3.95x50m Black 24GEOMStandard18267147.44NoTaian Road Engineering Materials co. Ltd161626
225TWEC225mm Twinwall Pipe End Cap 2TWDUStandard222210.45YesNo Default Supplier007
300TH300mm Dia Top Hat 1TWDStandard1123.15YesNo Default Supplier52
30VGGRID30/30 Biaxial Geogrid 3.95x50m Black 17GEOMStandard12550202.94NoTaian Road Engineering Materials co. Ltd181010
 
Upvote 0
And here is the destination range

Product CodeProduct Description Line 1Category.CodePurchasing.PackSizePurchasing.DefaultSupplierStockingStatusP_CURRENTB.MinimumStockLevelSupplier Lead TimeMin Stock This Month
025KGLLubricant Tube 250g ADITB10DAV001Stocked85
9001Plasson Concentric Water Meter WATERF1CIR001Stocked14
20VGGRID20/20 Biaxial Geogrid 3.95x50m Black GEOM9TAI001Stocked1612
300TH300mm Dia Top Hat TWD1NONEStocked24
30VGGRID30/30 Biaxial Geogrid 3.95x50m Black GEOM9TAI001Stocked1012
315VC4315mm Underground Drainage Inspection Chamber Base 5no.110mm Inlet (inc. 2 Socket Plugs)UDR48BRE002Stocked15
4VF78110mm Flexible Spigot & Socket fit Rain/Waste Adaptor UDR1VIP001Stocked2002
4VFWS110mm Rubber Wall Seal (Requires 140mm dia core drill) FLEXSEAL1VIP001Stocked152
6VC160600mm Vision 600 Chamber Base Swept Cross 160mm pipework US8INT001Stocked10030
6VC160S600mm Vision 600 Chamber Base 160mm Straight Channel US5INT001Stocked830
6VC250600mm Vision 600 Chamber Base Swept Cross 250mm pipework US8INT001Stocked20030
6VC250S600mm Vision 600 Chamber Base 250mm Straight Channel US5INT001Stocked830
6VC315600mm Vision 600 Chamber Base Swept Cross 315mm pipework US1INT001Stocked5030
6VCA10150250mm Vision 600 chamber, outlet adaptor to 150mm TW – Socket US1NAY001Stocked2010
 
Upvote 0
Did you accidentally copy the same data twice ?
At the moment you are Filtering the sheet "Intact Detail".
I believe you intend to filter on column O but you are really filtering on Column A

You are then looking up sheet Alton using the value in column O in Intact Detail which is Sales Quantity.
(That seems an odd thing to use for a lookup value)
You are then looking up sheet Alton. Column O of Intact Detail>Sales Quantity is never going to find a match in what you are showing as the Alton Sheet which has product as the first column.

How about getting your Vlookup to work in the spreadsheet itself and then post it here and we can convert it to a VBA worksheet function ?

Also I would have considered what you have as being Src to be the Destination since it is where the Vlookup and it's return value go.
 
Upvote 0
I got it working in excel but can`t get it to work in VBA. See Excel formula '=VLOOKUP(A2,'Intact Detail'!A2:O2,15,FALSE)

VBA Code:
Sub VLookup_Min()

Dim Wb        As Workbook
Dim Src       As Worksheet, Des As Worksheet
Dim SrcLRow   As Long, DesLRow
Dim x         As Variant
Dim SrcRng    As Range, DesRng As Range

Set Wb = ThisWorkbook
Set Src = Wb.Worksheets("Intact Detail")
Set Des = Wb.Worksheets("Alton")
SrcLRow = Src.Range("A" & Rows.Count).End(xlUp).Row
DesLRow = Des.Range("A" & Rows.Count).End(xlUp).Row


Src.Range("O2:O" & SrcLRow).AutoFilter Field:=1, Criteria1:="<>"

Set DesRng = Des.Range("A2:I" & DesLRow).SpecialCells(xlCellTypeVisible)
Set SrcRng = Src.Range("A2:A" & SrcLRow).SpecialCells(xlCellTypeVisible)

For Each x In SrcRng
On Error Resume Next
    x.Value = Application.WorksheetFunction.VLookup( _
    Src.Range("O2" & x.Row).Value, DesRng, 15, False)
Next x

If AutoFilter = True Then
AutoFilter = False
End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,103
Members
449,096
Latest member
provoking

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