Relative Cell Reference in VBA Not Working

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
For some reason, the below snippet of code inserts a formula where the lookup value for the match functions actually refers to cell RC1, rather than the first column of the sheet and the row of the active cell. Does anyone know why this code would do this? I think it has something to do with the named arrays in the functions, but I'm not sure. Thanks in advance!

Code:
With Sheets("Control").Cells(LastRow1 * 2 - 2, 2)
    .Formula = _
    "=INDEX(Exposures,MATCH(RC1,ME_Dates,0),COLUMN())*INDEX(Weights,MATCH(RC1,MW_Dates,0),COLUMN())"
    .NumberFormat = "0.0%"
    .HorizontalAlignment = xlRight
End With
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
try:


"=INDEX(Exposures,MATCH(" & RC1 & ",ME_Dates,0),COLUMN())...

or create a string with the relative cell reference:
s = somecell.address(0,0)
"=INDEX(Exposures,MATCH(" & s & ",ME_Dates,0),COLUMN())...


Sidenote:
I'm not as familiar with RxCx, so I'm not 100% on that one. The string version will work as this is what I use most of the time.
 
Last edited:
Upvote 0
Thank you! The second suggestion worked. Is there a way to fix the column reference with this? So, if I wanted the reference to be $A45 instead of A45, could I do that with your method?
 
Upvote 0
In this case, you could use:
s = somecell.address(0,0)
"=INDEX(Exposures,MATCH($" & s & ",ME_Dates,0),COLUMN())...


I'm sure there's a way to change "s = somecell.address(0,0)" to something else... but I'm not sure off the top of my head.
 
Upvote 0

Forum statistics

Threads
1,216,073
Messages
6,128,646
Members
449,462
Latest member
Chislobog

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