Fix Row Reference in VBA Using String Variable

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
Hi All,

I'm using this snippet of code to define a cell that changes depending on how the user specifies a couple different parameters.

Code:
Dim matchcell As String
matchcell = Sheets("Control").Cells(LastRow1 * 2 - 2, 1).Address(0, 0)

Then, I'm using the following bit of code to insert a function into the spreadsheet using the matchcell string in place of a relative cell reference.

Code:
With Sheets("Control").Cells(LastRow1 * 2 - 2, 2)
    .Formula = _
    "=INDEX(Data1,MATCH($" & matchcell & ",Dates,0),COLUMN())*INDEX(Data2,MATCH($" & matchcell & ",Weights,0),COLUMN())"
    .NumberFormat = "0.0%"
    .HorizontalAlignment = xlRight
End With

Here, I've manually inserted the "$" character before the string "matchcell" to define a cell reference where the column is fixed, but the row is not. Is there a way to fix the row reference and not the column reference with the string variable and address function? Or, is there even a way to manually insert the "$" character so that the macro inserts a formula with a fixed row reference?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

pplstuff

Well-known Member
Joined
Mar 9, 2012
Messages
951
Try changing:

matchcell = Sheets("Control").Cells(LastRow1 * 2 - 2, 1).Address(0, 0)

to

matchcell = Sheets("Control").Cells(LastRow1 * 2 - 2, 1).Address

and then lose that first "$" sign in the formula.
</pre>
 

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
That doesn't seem to work as now I get a run-time error. Any other thoughts?
 

pplstuff

Well-known Member
Joined
Mar 9, 2012
Messages
951
Use msgboxes to isolate where your error is coming from.

what does this return?
Code:
Dim matchcell As String
matchcell = Sheets("Control").Cells(LastRow1 * 2 - 2, 1).Address
msgbox matchcell
 

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80

ADVERTISEMENT

Wow, that's a neat trick. I had never used msgbox before. The cell reference is now $B$7, which isn't quite what I'm looking for as I'd like it to insert B$7. But, now the debugger is stopping at this line of code:

Code:
With Sheets("Control").Range("B8")
    .Formula = _
    "=INDEX(INDIRECT(R3C4),MATCH(RC1,INDIRECT(R2C4),0),MATCH(" & matchcell & ",INDIRECT(R1C4),0) + 2)"
    .NumberFormat = "0%"
    .HorizontalAlignment = xlRight
End With

I'm all kinds of confused.
 

ttbuson

Board Regular
Joined
Nov 18, 2011
Messages
80
Well, I figured out with the help of this webpage (http://msdn.microsoft.com/en-us/library/office/aa174749(v=office.11).aspx) that you can adjust the absolute cell or row reference by doing this:

Code:
Dim matchcell As String
matchcell = Sheets("Control").Cells(7, 2).Address(ColumnAbsolute:=False)

But, I'm still unable to enter the index formula as it is without encountering a runtime error. Any ideas?
 
Last edited:

pplstuff

Well-known Member
Joined
Mar 9, 2012
Messages
951
My first thought it that you're using a mix of RC references and regular cell references, I don't know if that would give errors. I never use RC style, so I'm afraid I have no tips here. Sorry. :(
 

Forum statistics

Threads
1,136,329
Messages
5,675,142
Members
419,551
Latest member
thangxpm

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
Top