VBA xLookup on multiple columns

darkybot

New Member
Joined
Feb 13, 2022
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hi all, I'm new in the world of xlookup and macros. I had to redo my recorded macros because .select is a bad habit to get used to, so I redid my entire macros, and now it works really well when I call it from a button.
Anyway, my macro is this:
VBA Code:
Sub SDS1()
'
' SDS Macro
'

Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Data")
       With ws.Range("U2:U" & ws.Range("D" & Rows.Count).End(xlUp).Row)
        .Formula = "=XLOOKUP(D2,'SDS003b'!I:I,'SDS003b'!U:U,"""")"
        .Value = .Value
        End With
End Sub
Basically I have 2 sheets, SDS003b is where the info will be search and Data is where the result will show up.
My question is, instead of doing another macros for column SDS003bV:V which is the one following the previous macro already mentioned, is there a way to add or spill the Xlookup to multiple columns?
I tried doing something like which it filed just column U with info but all the rest filled with zeros:
VBA Code:
Sub SDS1()
'
' SDS Macro
'

Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Data")
       With ws.Range("U2:Z" & ws.Range("D" & Rows.Count).End(xlUp).Row)
        .Formula = "=XLOOKUP(D2,'SDS003b'!I:I,'SDS003b'!U:Z,"""")"
        .Value = .Value
        End With
End Sub

Hope it make sense what Im asking.
 

Attachments

  • Screenshot 2023-09-22 084857.png
    Screenshot 2023-09-22 084857.png
    56.2 KB · Views: 31

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
You should only write the spilling formula into column U, so change this:
"U2:Z" &
back to
"U2:U" &
 
Upvote 0
You should only write the spilling formula into column U, so change this:
"U2:Z" &
back to
"U2:U" &
Thank Jan for the answer, tried but the xlookup only worked on column U without spilling to Z.
I tried this:
With ws.Range("U2:U" & ws.Range("D" & Rows.Count).End(xlUp).Row)
.Formula = "=XLOOKUP(D2,'SDS003b'!I:I,'SDS003b'!U:Z,"""")"
and this:
With ws.Range("U2:Z" & ws.Range("D" & Rows.Count).End(xlUp).Row)
.Formula = "=XLOOKUP(D2,'SDS003b'!I:I,'SDS003b'!U:Z,"""")"
 

Attachments

  • Screenshot dont spill.png
    Screenshot dont spill.png
    60.1 KB · Views: 13
  • Screenshot zeros.png
    Screenshot zeros.png
    61.5 KB · Views: 12
Upvote 0
There are 2 issues.
• to get it to spill you need to use Formula2 (not formula)
• .Value = . Value is only copying values for Column U which is where the spill formula is and in doing so you only end up with the column U values from the spill array

Give this a try:
VBA Code:
Sub SDS1_Mod()

Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Data")
    With ws.Range("U2:U" & ws.Range("D" & Rows.Count).End(xlUp).Row)
        .Formula2 = "=XLOOKUP(D2,'SDS003b'!I:I,'SDS003b'!U:Z,"""")"       
    End With
    
    With ws.Range("U2#:U" & ws.Range("D" & Rows.Count).End(xlUp).Row & "#")
        .Value = .Value
    End With
End Sub

Note: A filter alternative would have been:
VBA Code:
        .Formula2 = "=Filter('SDS003b'!U:Z,'SDS003b'!I:I = D2, """")"
 
Upvote 0
Solution
There are 2 issues.
• to get it to spill you need to use Formula2 (not formula)
• .Value = . Value is only copying values for Column U which is where the spill formula is and in doing so you only end up with the column U values from the spill array

Give this a try:
VBA Code:
Sub SDS1_Mod()

Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Data")
    With ws.Range("U2:U" & ws.Range("D" & Rows.Count).End(xlUp).Row)
        .Formula2 = "=XLOOKUP(D2,'SDS003b'!I:I,'SDS003b'!U:Z,"""")"      
    End With
   
    With ws.Range("U2#:U" & ws.Range("D" & Rows.Count).End(xlUp).Row & "#")
        .Value = .Value
    End With
End Sub

Note: A filter alternative would have been:
VBA Code:
        .Formula2 = "=Filter('SDS003b'!U:Z,'SDS003b'!I:I = D2, """")"

That works perfectly fine, now I know something else thank you Alex!
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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