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.
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Is this what you mean ?

Book2
ABCDEFG
112/18/202216:31:51Host WWIDGuest Badge IDGuest NameContact NumberBadge No
218/12/20220.6699881212008813465789John1234567KK5-1
318/12/20220.670151212880013245646Doe1324658KK5-2
418/12/20220.670661216777813244587Lucy1456738KK5-3
518/12/20220.6707641216777856478974Aaron1897654KK5-4
618/12/20220.6708221277888813264894Joseph1348795KD2-1
718/12/20220.6708911277888814654987Isaac1354879KD2-2
Dec22
Cell Formulas
RangeFormula
G2:G7G2=XLOOKUP(D2, BadgeList!$B$2:$B$100, BadgeList!$C$2:$C$100 & "-" & BadgeList!$A$2:$A$100)
 
Upvote 0
Is this what you mean ?

Book2
ABCDEFG
112/18/202216:31:51Host WWIDGuest Badge IDGuest NameContact NumberBadge No
218/12/20220.6699881212008813465789John1234567KK5-1
318/12/20220.670151212880013245646Doe1324658KK5-2
418/12/20220.670661216777813244587Lucy1456738KK5-3
518/12/20220.6707641216777856478974Aaron1897654KK5-4
618/12/20220.6708221277888813264894Joseph1348795KD2-1
718/12/20220.6708911277888814654987Isaac1354879KD2-2
Dec22
Cell Formulas
RangeFormula
G2:G7G2=XLOOKUP(D2, BadgeList!$B$2:$B$100, BadgeList!$C$2:$C$100 & "-" & BadgeList!$A$2:$A$100)
it does the trick but I'll need to use VBA since my colleagues are mostly old people with low general knowledge of excel
 
Upvote 0
How does VBA help ? I would imagine that would make it even harder for your audience.
 
Upvote 0
How does VBA help ? I would imagine that would make it even harder for your audience.
It'll be helpul since the VBA code is not visible on front end. They can't tamper the cell formula that way.
 
Upvote 0
OK give this a try:

VBA Code:
Sub LookupBadgeID()

    Dim shtAct As Worksheet, shtBadge As Worksheet
    Dim rngFormula As Range, rngBadge As Range
    Dim LRowAct As Long, LRowBadge As Long
    Dim strFormula As String
    
    
    Set shtAct = ActiveSheet
    Set shtBadge = Worksheets("BadgeList")
    
    With shtAct
        LRowAct = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngFormula = .Range(.Cells(2, "G"), .Cells(LRowAct, "G"))
    End With
    
    With shtBadge
        LRowBadge = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngBadge = .Range(.Cells(2, "A"), .Cells(LRowBadge, "C"))
    End With

    strFormula = "=XLOOKUP(D2, BadgeList!$B$2:$B$~LR, BadgeList!$C$2:$C$~LR & "" - "" & BadgeList!$A$2:$A$~LR)"
    strFormula = Replace(strFormula, "~LR", LRowBadge)
    
    rngFormula.Formula2 = strFormula
    rngFormula.Value2 = rngFormula.Value2

End Sub
 
Upvote 0
the lookup code took quite a few seconds to be completed. Amending the data of the cells involved in lookup range will also loads a bit more after cell change.
 
Upvote 0
Amending the data of the cells involved in lookup range will also loads a bit more after cell change.
What does that mean ?

How many rows of data are we talking ? It would need to be rewritten using a dictionary to speed it up. I wouldn't be able to look at that until much later today.
(I am in Australia)
 
Upvote 0
What does that mean ?

How many rows of data are we talking ? It would need to be rewritten using a dictionary to speed it up. I wouldn't be able to look at that until much later today.
(I am in Australia)
this is the reply for earlier xlookup in front end. not vba. what i meant is. when i add a new badge in badge list sheet, it'll load around 2 3 secs.
 
Upvote 0
OK give this a try:

VBA Code:
Sub LookupBadgeID()

    Dim shtAct As Worksheet, shtBadge As Worksheet
    Dim rngFormula As Range, rngBadge As Range
    Dim LRowAct As Long, LRowBadge As Long
    Dim strFormula As String
   
   
    Set shtAct = ActiveSheet
    Set shtBadge = Worksheets("BadgeList")
   
    With shtAct
        LRowAct = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngFormula = .Range(.Cells(2, "G"), .Cells(LRowAct, "G"))
    End With
   
    With shtBadge
        LRowBadge = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngBadge = .Range(.Cells(2, "A"), .Cells(LRowBadge, "C"))
    End With

    strFormula = "=XLOOKUP(D2, BadgeList!$B$2:$B$~LR, BadgeList!$C$2:$C$~LR & "" - "" & BadgeList!$A$2:$A$~LR)"
    strFormula = Replace(strFormula, "~LR", LRowBadge)
   
    rngFormula.Formula2 = strFormula
    rngFormula.Value2 = rngFormula.Value2

End Sub
This will also take a while to complete since it checks all row in column G. I think maybe just check for guest badge number whenever I edit column D in Dec22 sheet.
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,566
Members
449,318
Latest member
Son Raphon

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