Macro for Vlookup based on a Criteria

user2021

New Member
Joined
Mar 10, 2021
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Need help to get my code corrected. I need the Column C values onto my "working sheet Column B" based on the string value that is "column B in entity sheet". vlookup value would be the entity ID in column A.
sheet.png



VBA Code:
Set sht = ActiveWorkbook.Worksheets("working sheet")
Set sht1 = ActiveWorkbook.Worksheets("entity sheet")

LR = sht.UsedRange.Rows.Count

With sht
For i = 2 To LR
If InStr(sht1.Range("B" & i).Value, "WS") Then
sht.Range("B" & i).Value = (Application.VLookup(.Range("A" & i).Value, Worksheets("entity sheet").Range("A2:C5000"), 3, False))
End If
Next i
End With


EDIT: Fixed VBA code
 
Last edited by a moderator:

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
You have referred to wrong sheet sht instead of sht1

VBA Code:
Set sht = ActiveWorkbook.Worksheets("working sheet")
Set sht1 = ActiveWorkbook.Worksheets("entity sheet")

LR = sht1.UsedRange.Rows.Count

n = 0
With sht1
    For i = 2 To LR
        If InStr(sht1.Range("B" & i).Value, "WS") Then
            n = n + 1
            sht.Range("B" & n).Value = Application.VLookup(.Range("A" & i).Value, sht1.Range("A2:C5000"), 3, False)
        End If
    Next i
End With

However, your column A after HR00015, you have repeated value HR0006, HR0007, HR0008, HR0009 that makes VLookUp capture the previous values.
 
Upvote 0
You have referred to wrong sheet sht instead of sht1

VBA Code:
Set sht = ActiveWorkbook.Worksheets("working sheet")
Set sht1 = ActiveWorkbook.Worksheets("entity sheet")

LR = sht1.UsedRange.Rows.Count

n = 0
With sht1
    For i = 2 To LR
        If InStr(sht1.Range("B" & i).Value, "WS") Then
            n = n + 1
            sht.Range("B" & n).Value = Application.VLookup(.Range("A" & i).Value, sht1.Range("A2:C5000"), 3, False)
        End If
    Next i
End With

However, your column A after HR00015, you have repeated value HR0006, HR0007, HR0008, HR0009 that makes VLookUp capture the previous values.
Yes that's the issue i have. The vlookup is picking the first value. But i want the values based on source id (column b). if the string that contains "WS" in source id, i want that value alone to be picked up. this code is not working.
 
Upvote 0
Yes that's the issue i have. The vlookup is picking the first value. But i want the values based on source id (column b). if the string that contains "WS" in source id, i want that value alone to be picked up. this code is not working.
When you asked to fix the code, obviously it is not working at all because you are looking up on wrong sheet. That was fixed but I have no idea what you were trying to achieve since it wasn't mentioned. Let us know what is your expected result.

It sounds like you do not need to lookup the column A at all since as long as column B starts with WS, then just transfer column C value to another sheet. Isn't that so?
 
Upvote 0
When you asked to fix the code, obviously it is not working at all because you are looking up on wrong sheet. That was fixed but I have no idea what you were trying to achieve since it wasn't mentioned. Let us know what is your expected result.

It sounds like you do not need to lookup the column A at all since as long as column B starts with WS, then just transfer column C value to another sheet. Isn't that so?
result sheet.png
 
Upvote 0
When you asked to fix the code, obviously it is not working at all because you are looking up on wrong sheet. That was fixed but I have no idea what you were trying to achieve since it wasn't mentioned. Let us know what is your expected result.

It sounds like you do not need to lookup the column A at all since as long as column B starts with WS, then just transfer column C value to another sheet. Isn't thatI have

When you asked to fix the code, obviously it is not working at all because you are looking up on wrong sheet. That was fixed but I have no idea what you were trying to achieve since it wasn't mentioned. Let us know what is your expected result.

It sounds like you do not need to lookup the column A at all since as long as column B starts with WS, then just transfer column C value to another sheet. Isn't that so?
The expected result is put as a table above
 
Upvote 0
This should work

VBA Code:
Sub LookUpCriteria()

Dim eRow As Long
Dim cell As Range, rngData As Range
Dim Element As Variant, dData  As Object
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = ActiveWorkbook.Worksheets("working sheet")
Set ws2 = ActiveWorkbook.Worksheets("entity sheet")

eRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
Set rngData = ws1.Range("A2", "A" & eRow)
Set dData = CreateObject("Scripting.Dictionary")

For Each cell In rngData
    If Left(cell.Offset(0, 1), 2) = "WS" Then
        dData.Add cell.Value2, cell.Offset(0, 2).Value2
    End If
Next

For Each cell In rngData
    For Each Element In dData
        ws2.Range("A" & cell.Row) = cell.Value2
        If cell.Value2 = Element Then
            ws2.Range("B" & cell.Row) = dData(Element)
        End If
    Next
Next

End Sub
 
Upvote 0
This should work

VBA Code:
Sub LookUpCriteria()

Dim eRow As Long
Dim cell As Range, rngData As Range
Dim Element As Variant, dData  As Object
Dim ws1 As Worksheet, ws2 As Worksheet

Set ws1 = ActiveWorkbook.Worksheets("working sheet")
Set ws2 = ActiveWorkbook.Worksheets("entity sheet")

eRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row
Set rngData = ws1.Range("A2", "A" & eRow)
Set dData = CreateObject("Scripting.Dictionary")

For Each cell In rngData
    If Left(cell.Offset(0, 1), 2) = "WS" Then
        dData.Add cell.Value2, cell.Offset(0, 2).Value2
    End If
Next

For Each cell In rngData
    For Each Element In dData
        ws2.Range("A" & cell.Row) = cell.Value2
        If cell.Value2 = Element Then
            ws2.Range("B" & cell.Row) = dData(Element)
        End If
    Next
Next

End Sub
This isn't working. The column B in working sheet is blank
 
Upvote 0
What column B? I was just following the expected result you were showing. :unsure:
am not getting the expected result. on running the code, the expected result column - "source entity id - wss" is blank.
 
Upvote 0

Forum statistics

Threads
1,215,268
Messages
6,123,966
Members
449,137
Latest member
yeti1016

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