Code for Constant Row Look Up

Carl Clements

Board Regular
Joined
Jun 13, 2008
Messages
95
Hi,

I need some VBA code that puts in formulas to pull data in various columns within row 2 of another worksheet.

The formulas need to be entered below some other data, which is populated from another prior macro.

The below code works well but as the prior macro can populate a different number of rows on different days, the Row count then also changes and doesn't always refer to row 2 of 'HLDRT before' tab.

ActiveCell.FormulaR1C1 = _
"=IF('HLDRT before'!R[-22]C[-18] = ""A17"",RIGHT('HLDRT before'!R-20C[14],3)&'HLDRT before'!R-20C[9]&'HLDRT before'!R-20]C[-13],"""")"

Is there a way I can tell the above code to always use row 2 from the 'HLDRT before' sheet, while keeping the column number lookups the same?

I think there is a way to do it by removing the [] signs but I can't get it to work. I also then need the macro to copy these down so I'm not sure if using $ will cause problems?

Thanks,
Carl
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Do you mean?

Code:
ActiveCell.FormulaR1C1 = _
"=IF('HLDRT before'!R2C[-18] = ""A17"",RIGHT('HLDRT before'!R2C[14],3)&'HLDRT before'!R2C[9]&'HLDRT before'!R2C[-13],"""")"
 
Upvote 0
Thanks. It works although it puts $ after the column letter, eg B$2, which means I can't copy the formula down to then look at B3 etc. Is there a way around that?
 
Upvote 0
You would have to calculate the offset between the active cell's row and row 2 and assign it to a variable. Then you can use that variable in your formula. Example:

Code:
Sub Test()
    Dim ROffset As Long
    With ActiveCell
        ROffset = 2 - .Row
        .FormulaR1C1 = _
            "=IF('HLDRT before'!R[" & ROffset & "]C[-18] = ""A17"",RIGHT('HLDRT before'!R[" & ROffset & "]C[14],3)&'HLDRT before'!R[" & ROffset & "]C[9]&'HLDRT before'!R[" & ROffset & "]C[-13],"""")"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,203,680
Messages
6,056,704
Members
444,885
Latest member
Mark Prillman

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