Hello,
I'm very new to VBA and struggling with the best way to incorporate XLOOKUP in my VBA code. To me the tricky part is getting my variables to be dynamic based on the row count in my data.
I have 2 worksheets called 'Election Summary' and 'Autorebal.' The XLOOKUP function's argument lookup-value is a range from 'Election Summary' workseet.The lookup_array argument is a Range in the 'Autorebal' worksheet. The return_array is also a range in the 'Autorebal' worksheet
My understanding is that since the ranges need to be dynamic and grab the last row, that creating variables for the lookup_value, lookup_array, and return_array is my best bet.
Here is my code so far:
Dim vRows As Integer
Dim aRows As Integer
Dim rRows As Integer
vRows = Range(Range("A5"), Range("A5").End(xlDown)).Rows.Count + 4
aRows = Range(Range("A2"), Range("A5").End(xlDown)).Rows.Count - 1
Sheets("Election Summary").Select
Range("D5").Select
ActiveCell.Formula2R1C1 = _
"=XLOOKUP(R5C1:R" & vRows & "C1,Autorebal!R2C1:R27C1,Autorebal!R[-3]C[-2]:R[22]C[-1],"""")"
Range("D5").Select
Selection.AutoFill Destination:=Range("D5:E5"), Type:=xlFillDefault
vRows would be a Range in the 'Election Summary' worksheet
aRows would be a Range in the 'Autorebal' workseet
rRows would be a Range in the 'Autorebal' worksheet
In the Xlookup Formula above, I have inserted the vRows variable in the string where the lookup_value argument is and that seems to work ok.
The problem is inserting a variable in the lookup_array argument that works because that range is on on different worksheet (Autorebal). I haven't been able to create a variable that is dynamic that recognizes the range is on Autorebal worksheet.
My main question is how to create a dynamice variable for the lookup_array and return array where both of these arguments are on a different worksheet (both on the 'Autorebal' worksheet)?
I've attached the spreadsheet with the code that I'm currently working on.
Any help is greatly appreciated!