variable formula

schroederdj

New Member
Joined
Jun 21, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
I have the following formula in VBA code that gets copied down to all rows. Everything works in the code but I need J4 to change to J5, J6, J...... as the formula is copied down.

Range("K4:K" & lr).FormulaR1C1 = "=IFERROR(IF(XLOOKUP(RC[-7],(INDIRECT(""'""&R1C[2]&""'!D:D"")),(INDIRECT(""'""&R1C[2]&""'!I:I"")))=-J4,""MATCH"",""PARTIAL MATCH""),0)"

Can anyone please help with how to make J4 variable as copied down?

Here is the entire code:

Sub Match_OS()

Sheets("Retail Prior OS").Select
Range("m1").Select

ws1 = Sheets(1).Name
ActiveCell = ws1
Selection.NumberFormat = "@"

'XLOOKUP Formula to previous report
Range("k4").Select

' Find last row with data in column A
lr = Cells(Rows.Count, "A").End(xlUp).Row

' Apply lookup formula to column J down to last row

Range("K4:K" & lr).FormulaR1C1 = "=IFERROR(IF(XLOOKUP(RC[-7],(INDIRECT(""'""&R1C[2]&""'!D:D"")),(INDIRECT(""'""&R1C[2]&""'!I:I"")))=-J4,""MATCH"",""PARTIAL MATCH""),0)"

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try:

Rich (BB code):
  Range("K4:K" & lr).FormulaR1C1 = "=IFERROR(IF(XLOOKUP(RC[-7],(INDIRECT(""'""&R1C[2]&""'!D:D"")),(INDIRECT(""'""&R1C[2]&""'!I:I"")))=-RC[-1],""MATCH"",""PARTIAL MATCH""),0)"

OR:

VBA Code:
  Range("K4:K" & lr).Formula = "=IFERROR(IF(XLOOKUP(D4,(INDIRECT(""'""&M$1&""'!D:D"")),(INDIRECT(""'""&M$1&""'!I:I"")))=-J4,""MATCH"",""PARTIAL MATCH""),0)"
 
Last edited:
Upvote 0
Solution
Try:

Rich (BB code):
  Range("K4:K" & lr).FormulaR1C1 = "=IFERROR(IF(XLOOKUP(RC[-7],(INDIRECT(""'""&R1C[2]&""'!D:D"")),(INDIRECT(""'""&R1C[2]&""'!I:I"")))=-RC[-1],""MATCH"",""PARTIAL MATCH""),0)"

OR:

VBA Code:
  Range("K4:K" & lr).Formula = "=IFERROR(IF(XLOOKUP(D4,(INDIRECT(""'""&M$1&""'!D:D"")),(INDIRECT(""'""&M$1&""'!I:I"")))=-J4,""MATCH"",""PARTIAL MATCH""),0)"
Thank you DanteAmor. I used the 1st example you sent and it worked as expected. I'm wondering if you could explain the loigic around the coding RC[-1] that makes the cell variable?
 
Upvote 0
FormulaR1C1
First, you are indicating that you are going to put a formula with the style notation R1C1 (Row - Column)

R (Row) without square brackets is a relative position, so it starts at row R, in this example, at row 4 and when it moves down it will increment to 5, 6...
C[-1] (Column), with square brackets is an absolute position, it must be one column to the left of where you put the formula, in this case you are putting the formula in column K, column K -1 column, so it is the J column.

Regularly the macro recorder returns the formulas with the nomenclature R1C1, it is difficult to read and understand, especially if you don't know where the formula is.

The most practical option is the second formula that I gave you, it uses the A1 nomenclature. ;)
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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