VBA Search Value from other sheet and return data

tbxor

New Member
Joined
Dec 14, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
  3. Web
Test.xlsm
ABCDEFG
112/18/202216:31:51Host WWIDGuest Badge IDGuest NameContact NumberBadge No
212/18/202216:04:471212008813465789John1234567KK5-1
312/18/202216:05:011212880013245646Doe1324658KK5-2
412/18/202216:05:451216777813244587Lucy1456738KK5-3
512/18/202216:05:541216777856478974Aaron1897654
612/18/202216:05:591277888813264894Joseph1348795
712/18/202216:06:051277888814654987Isaac1354879
Dec22


Test.xlsm
ABC
1NoGuest Badge IDOrigin
2113465789KK5
3213245646KK5
4313244587KK5
5456478974KK5
6113264894KD2
7214654987KD2
BadgeList


There's multiple guest badge origin. KK5 and KD2. All of them listed in badge list sheet. I need to lookup the value of Guest Badge ID, 5th column in Dec22 sheet. Get the data from Badge List sheet and set the value of Badge No column in Dec22 sheet to be a combination of Origin and Badge No as shown in G2 until G4.
 
See if this works for you.

VBA Code:
Sub LookupBadgeID_dic()

    Dim shtAct As Worksheet, shtBadge As Worksheet
    Dim rngActResult As Range, rngActID As Range, rngBadge As Range
    Dim arrBadge As Variant, arrActID As Variant
    Dim LRowAct As Long, LRowBadge As Long
    Dim dictBadge As Object, dictKey As String
    Dim i As Long
    
    Set shtAct = ActiveSheet
    Set shtBadge = Worksheets("BadgeList")
    
    With shtAct
        LRowAct = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngActID = .Range(.Cells(2, "D"), .Cells(LRowAct, "D"))
        Set rngActResult = .Range(.Cells(2, "G"), .Cells(LRowAct, "G"))
        arrActID = rngActID.Value2
        ReDim Preserve arrActID(1 To UBound(arrActID), 1 To 2)
    End With
    
    With shtBadge
        LRowBadge = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngBadge = .Range(.Cells(2, "A"), .Cells(LRowBadge, "C"))
        arrBadge = rngBadge.Value2
    End With
    
    Set dictBadge = CreateObject("Scripting.dictionary")
    
    ' Load badge lookup range into Dictionary
    For i = 1 To UBound(arrBadge)
        dictKey = arrBadge(i, 2)
        If Not dictBadge.exists(dictKey) Then
            dictBadge(dictKey) = i
        End If
    Next i
    
    ' For Lookup return value get values from Dictionary
    For i = 1 To UBound(arrActID)
        dictKey = arrActID(i, 1)
        If dictBadge.exists(dictKey) Then
            arrActID(i, 2) = arrBadge(dictBadge(dictKey), 3) & "-" & arrBadge(dictBadge(dictKey), 1)
        End If
    Next i
    
    rngActResult.Value2 = Application.Index(arrActID, 0, 2)

End Sub
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
See if this works for you.

VBA Code:
Sub LookupBadgeID_dic()

    Dim shtAct As Worksheet, shtBadge As Worksheet
    Dim rngActResult As Range, rngActID As Range, rngBadge As Range
    Dim arrBadge As Variant, arrActID As Variant
    Dim LRowAct As Long, LRowBadge As Long
    Dim dictBadge As Object, dictKey As String
    Dim i As Long
  
    Set shtAct = ActiveSheet
    Set shtBadge = Worksheets("BadgeList")
  
    With shtAct
        LRowAct = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngActID = .Range(.Cells(2, "D"), .Cells(LRowAct, "D"))
        Set rngActResult = .Range(.Cells(2, "G"), .Cells(LRowAct, "G"))
        arrActID = rngActID.Value2
        ReDim Preserve arrActID(1 To UBound(arrActID), 1 To 2)
    End With
  
    With shtBadge
        LRowBadge = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngBadge = .Range(.Cells(2, "A"), .Cells(LRowBadge, "C"))
        arrBadge = rngBadge.Value2
    End With
  
    Set dictBadge = CreateObject("Scripting.dictionary")
  
    ' Load badge lookup range into Dictionary
    For i = 1 To UBound(arrBadge)
        dictKey = arrBadge(i, 2)
        If Not dictBadge.exists(dictKey) Then
            dictBadge(dictKey) = i
        End If
    Next i
  
    ' For Lookup return value get values from Dictionary
    For i = 1 To UBound(arrActID)
        dictKey = arrActID(i, 1)
        If dictBadge.exists(dictKey) Then
            arrActID(i, 2) = arrBadge(dictBadge(dictKey), 3) & "-" & arrBadge(dictBadge(dictKey), 1)
        End If
    Next i
  
    rngActResult.Value2 = Application.Index(arrActID, 0, 2)

End Sub
Can you fiddle a code that checks for a targeted cell change using Worksheet_Change target.column? for example, when I edit the value of D2, it will trigger an xlookup
 
Upvote 0
Try this:-
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim rngChg As Range, rCell As Range
    Dim shtBadge As Worksheet
    Dim rngBadge As Range
    Dim LRowBadge As Long
    
    Set rngChg = Intersect(Target, Columns("D"))

    If Not rngChg Is Nothing Then
        Application.EnableEvents = False
        Set shtBadge = Worksheets("BadgeList")
        With shtBadge
            LRowBadge = .Range("A" & Rows.Count).End(xlUp).Row
            Set rngBadge = .Range(.Cells(2, "A"), .Cells(LRowBadge, "C"))
        End With
    
        For Each rCell In rngChg
             rCell.Offset(, 3).Value2 = Application.XLookup(rCell, rngBadge.Columns(2), rngBadge.Columns(3)) & "-" & _
                                                Application.XLookup(rCell, rngBadge.Columns(2), rngBadge.Columns(1))
        Next rCell
        Application.EnableEvents = True
    End If

End Sub
 
Upvote 0
Solution
Try this:-
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   
    Dim rngChg As Range, rCell As Range
    Dim shtBadge As Worksheet
    Dim rngBadge As Range
    Dim LRowBadge As Long
   
    Set rngChg = Intersect(Target, Columns("D"))

    If Not rngChg Is Nothing Then
        Application.EnableEvents = False
        Set shtBadge = Worksheets("BadgeList")
        With shtBadge
            LRowBadge = .Range("A" & Rows.Count).End(xlUp).Row
            Set rngBadge = .Range(.Cells(2, "A"), .Cells(LRowBadge, "C"))
        End With
   
        For Each rCell In rngChg
             rCell.Offset(, 3).Value2 = Application.XLookup(rCell, rngBadge.Columns(2), rngBadge.Columns(3)) & "-" & _
                                                Application.XLookup(rCell, rngBadge.Columns(2), rngBadge.Columns(1))
        Next rCell
        Application.EnableEvents = True
    End If

End Sub
niiiiiicceeeee. thaannnk youuuu!
 
Upvote 0

Forum statistics

Threads
1,214,957
Messages
6,122,472
Members
449,087
Latest member
RExcelSearch

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