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
 
Which sheet did you put the lookup in, I assume Alton, is that correct ? and also what cell ?
When you did it manually which sheet did you filter on Column O ?

Can you describe what you are trying to do. You seem to be filtering Sheet Intact Detail for not blank in O but wanting to update Sheet Alton.
Are you saying that if there is a value in both sheets you want Intact Detail to overwrite what is in Alton ?
 
Last edited:
Upvote 0

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Which sheet did you put the lookup in, I assume Alton, is that correct ? and also what cell ? I2
When you did it manually which sheet did you filter on Column On Intact Detail

Can you describe what you are trying to do. You seem to be filtering Sheet Intact Detail for not blank in O but wanting to update Sheet Alton. That`s right
Are you saying that if there is a value in both sheets you want Intact Detail to overwrite what is in Alton ? In Alton column I there is no data so that`s the column to fill into
 
Upvote 0
See if this work on your machine. I suspect that it will only work in MS365.

VBA Code:
Sub VLookup_Min_AlternateFinal()
    Dim Wb        As Workbook
    Dim Src       As Worksheet, Des As Worksheet
    Dim SrcLRow   As Long, DesLRow
    Dim rCell         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:O" & SrcLRow)

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

        
    With Application
        Des.Range("I2").Resize(DesRng.Rows.Count).Value = _
            .IfError(.Trim(.VLookup(Des.Range("A2").Resize(DesRng.Rows.Count).Value, SrcRng, 15, False)), "")
    End With
        
End Sub
 
Upvote 0
Solution
See if this work on your machine. I suspect that it will only work in MS365.

VBA Code:
Sub VLookup_Min_AlternateFinal()
    Dim Wb        As Workbook
    Dim Src       As Worksheet, Des As Worksheet
    Dim SrcLRow   As Long, DesLRow
    Dim rCell         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:O" & SrcLRow)

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

       
    With Application
        Des.Range("I2").Resize(DesRng.Rows.Count).Value = _
            .IfError(.Trim(.VLookup(Des.Range("A2").Resize(DesRng.Rows.Count).Value, SrcRng, 15, False)), "")
    End With
       
End Sub
Seems to only fill A2 needs to be A2 to lastrow which is why I had a loop. Also not sure why you have a repeat of below

VBA Code:
    Set DesRng = Des.Range("A2:A" & DesLRow)
    Set SrcRng = Src.Range("A2:O" & SrcLRow)
 
Upvote 0
Sorry your code does work could you please explain for me how it works very much still learning.
As in why my code was failing to work thanks in advance.
 
Last edited:
Upvote 0
Primary sheet: Alton
Get value from sheet: Intact Detail

To apply a filter on the sheet you are looking up doesn't add any value.
Use VLookup on Column A-Product on the Primary sheet to lookup sheet to return Column O.

The only issue you might get is that if the Product is found on the sheet Intact Detail but column O is empty, it will return "0" zero.
Also if not found it will error out.

The iferror takes care of not found and returns ""
The Trim changes the Vlookup result to TEXT and as such if not found it returns "".
Writing to the spreadsheet has conveniently translated the now text numbers back to a numeric value.

Rich (BB code):
    With Application
        Des.Range("I2").Resize(DesRng.Rows.Count).Value = _
            .IfError(.Trim(.VLookup(Des.Range("A2").Resize(DesRng.Rows.Count).Value, SrcRng, 15, False)), "")
    End With
 
Upvote 0
Primary sheet: Alton
Get value from sheet: Intact Detail

To apply a filter on the sheet you are looking up doesn't add any value.
Use VLookup on Column A-Product on the Primary sheet to lookup sheet to return Column O.

The only issue you might get is that if the Product is found on the sheet Intact Detail but column O is empty, it will return "0" zero.
Also if not found it will error out.

The iferror takes care of not found and returns ""
The Trim changes the Vlookup result to TEXT and as such if not found it returns "".
Writing to the spreadsheet has conveniently translated the now text numbers back to a numeric value.

Rich (BB code):
    With Application
        Des.Range("I2").Resize(DesRng.Rows.Count).Value = _
            IfError(.Trim(.VLookup(Des.Range("A2").Resize(DesRng.Rows.Count).Value, SrcRng, 15, False)), "")
    End With
Thanks for that
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,107
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