Attempting to use dynamic variables within XLOOKUP VBA

sax2play

Board Regular
Joined
Mar 13, 2021
Messages
63
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am trying to use XLOOKUP, using all dynamic variables. The columns on the sheet may change position but the header is always the same. When I define all of the ranges and then attempt add the XLOOKUP function, I get a #NAME? error. Any help or suggestions would be greatly appreciated.

VBA Code:
Dim lastrow, lastcol, lastrow2, lastcol2 As Long
    Dim myRng2 As Range
    Dim mycell2 As Range
    Dim MyColl2 As Collection
    Dim myIterator2 As Variant
    Dim MyColl3, MyColl4, MyColl5 As Collection
    Dim myIterator3, myIterator4, myIterator5 As Variant
    Dim myRng3, myRng4, myRng5 As Range
    Dim mycell3 As Integer
    Dim wsReview As Worksheet
    Dim wsReview2 As Worksheet
    Dim colFound As Integer
    
    lastrow = WB.Sheets("Review").Cells(Rows.Count, "A").End(xlUp).Row
    lastcol = WB.Sheets("Review").Cells(1, Columns.Count).End(xlToLeft).Column
    lastrow2 = WB.Sheets("Review (2)").Cells(Rows.Count, "A").End(xlUp).Row
    lastcol2 = WB.Sheets("Review (2)").Cells(1, Columns.Count).End(xlToLeft).Column
    
    Set wsReview = WB.Worksheets("Review")
    Set wsReview2 = WB.Worksheets("Review (2)")
    Set MyColl2 = New Collection
    'Set MyColl3 = New Collection
    Set MyColl4 = New Collection
    Set MyColl5 = New Collection
    
    mycell3 = 2
    
    MyColl2.Add "Review Notes"
    'MyColl2.Add "Comments"
    'MyColl3.Add "NUMBER"
    MyColl4.Add "NUMBER"          'Column on different sheet
    MyColl5.Add "Review Notes"          'Column on different sheet
    
    colFound = WorksheetFunction.Match("NUMBER", Range(wsReview.Cells(1, 1), wsReview.Cells(1, lastcol)), 0)
    
    With wsReview2
        For k = 1 To lastcol2
            For Each myIterator4 In MyColl4
                If Cells(1, k) = myIterator4 Then
                    Set myRng4 = WB.Worksheets("Review (2)").Range(Cells(2, k), Cells(lastrow2, k))
                End If
            Next
        Next
    End With
    With wsReview2
        For l = 1 To lastcol2
            For Each myIterator5 In MyColl5
                If Cells(1, l) = myIterator5 Then
                    Set myRng5 = WB.Worksheets("Review (2)").Range(Cells(2, l), Cells(lastrow2, l))
                End If
            Next
        Next
    End With
    'With wsReview
        'For j = 1 To lastcol
            'For Each myIterator3 In MyColl3
                'If Cells(1, j) = myIterator3 Then
                    'Set myRng3 = Range(Cells(2, j), Cells(lastrow, j))
                    'For Each mycell3 In myRng3
                        'mycell3 = mycell3.Address
                    'Next
                'End If
            'Next
        'Next
    'End With
    wsReview.Select
    With wsReview
        For i = 1 To lastcol
            For Each myIterator2 In MyColl2
                If Cells(1, i) = myIterator2 Then
                    Set myRng2 = Range(Cells(2, i), Cells(lastrow, i))
                    For Each mycell2 In myRng2
                        mycell2.Value = "=XLOOKUP(Cells(mycell3, colFound),myRng4,myRng5,""Previous Notes Not Found"",0,1)"
                        mycell3 = mycell3 + 1
                    Next
                    
                End If
            Next
        Next
    End With

Thanks in advance for any help!!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If you only need a value in the cell you.should.use Application.Worksheetfunction
If you need a formula in the cell:

mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound). address & "," & myRng4.address & "," & myRng5.address & ", ""Previous Notes Not Found"",0,1)"

You may also have to include sheet names.
Sorry for not being too thorough and deyailed, but i type this on my phone.
For some reason i can't even use code tags at the moment.
 
Upvote 0
That fixes the formula and identifies the correct columns! But it does not select the myRng4/myRng5 from sheet("Review (2)"), even when I try to add the sheets before each variable:

VBA Code:
mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & Worksheets("Review (2)").myRng4.Address & "," & Worksheets("Review (2)").myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & WB.Worksheets("Review (2)").myRng4.Address & "," & WB.Worksheets("Review (2)").myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & Sheets("Review (2)").myRng4.Address & "," & Sheets("Review (2)").myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & wsReview2.myRng4.Address & "," & wsReview2.myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & WB.wsReview2.myRng4.Address & "," & WB.wsReview2.myRng5.Address & ", ""Previous Notes Not Found"",0,1)"

All of these return Run-time error '438' except one line:
1631399080327.png


VBA Code:
mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & wsReview2.myRng4.Address & "," & wsReview2.myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
This line returns a compile error:
1631399162315.png


Any thoughts??
 
Upvote 0
Sorry, when i said add sheet names I meant like in a formula.
Which looks like: Sheet1!A1:B5
So try like:
... & "Review (2)!" & myRng5.Address & ...

Since you seem to have a space in the sheet name, you'll probably need some more quotes.
 
Upvote 0
I feel like I am missing something incredible simple lol - I am very new to VBA so please forgive me.

I took the space out of the sheet name so it is now "Review2" and tried various methods after attempting the original ...& "Review (2)!" & myRng5.Address & ... and it is still failing for Run-time error '9': subscript out of range. Here are some lines I attempted below:

VBA Code:
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & Sheets(Review2) & "" & myRng4.Address & "," & Sheets(Review2) & "" & myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & Review2! & myRng4.Address & "," & Review2! & myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & Worksheets(Review2).myRng4.Address & "," & Worksheets(Review2).myRng5.Address & ", ""Previous Notes Not Found"",0,1)"
'mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & Sheets(Review2!).myRng4.Address & "," & Sheets(Review2!).myRng5.Address & ", ""Previous Notes Not Found"",0,1)"

Thanks again for any help!!
 
Upvote 0
okay - I was mixed up in what the "" was actually going around. Now I have:

VBA Code:
mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & "Review2!" & myRng4.Address & "," & "Review2!" & myRng5.Address & ", ""Previous Notes Not Found"",0,1)"

myRng4 and myRng5 are absolute tho and not changing so it is still returning a value of 0 instead of "Previous Notes Not Found"
 
Last edited:
Upvote 0
I adjusted the line to read:

VBA Code:
mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & "Review2!" & myRng4.Address & "," & "Review2!" & myRng5.Address & ",Previous Notes Not Found)"

With no change - it still populates 0 instead of "Previous Notes Not Found" when the cell is blank...
 
Upvote 0
VBA Code:
mycell2.Formula = "=XLOOKUP(" & Cells(mycell3, colFound).Address & "," & "Review2!" & myRng4.Address & "," & "Review2!" & myRng5.Address & "," & ""Previous Notes Not Found"" & ")"

This also gives me Compile error: Syntax error and will not return the desired response....
 
Upvote 0
Try the below:
Your original code looks to have some other errors in it but if this works you must have fixed somewhere along the line.
The is assuming your sheet is still called "Review (2)" per your original code.
If it has been changed to Review2 then make the 2 changes in the below.

VBA Code:
                        mycell2.Formula = "=XLOOKUP(" & _
                                                Cells(mycell3, colFound).Address & "," & _
                                                "'Review (2)'!" & myRng4.Address & "," & _
                                                "'Review (2)'!" & myRng5.Address & "," & _
                                                """Previous Notes Not Found""" & ")"
 
Upvote 0
I am very new to VBA
So a couple of tips

1.
VBA Code:
Dim myRng3, myRng4, myRng5 As Range
This only declares myRng5 as Range. myRng3 and myRng4 will be allocated as Variant. If you want them all to be declared as Range, you need to list it out in full like this
VBA Code:
Dim myRng3 As Range, myRng4 As Range, myRng5 As Range

2.
VBA Code:
Dim colFound As Integer
Nothing at wrong with this technically. However, vba converts all Integer values to Long even if they are declared as Integer, so you might as well declare them as Long to start with to save the bother of conversion. Besides, Long is shorter to type anyway. ;)
VBA Code:
Dim colFound As Long
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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