Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable in R1C1 formula pasted in Excel worksheet 2

Status
Not open for further replies.

Numbers1

Technical User
Dec 27, 2003
34
US
Is it possible to place a variable, that is defined in a module, into a formula that is pasted into a cell on a worksheet?
Dim intColRef as Interger
intColRef = 5
ActiveCell.Offset(0, 6).Range("A1").Select
ActiveCell.FormulaR1C1 = "=RC[5]"
Instead
ActiveCell.FormulaR1C1 = "=RC[intColRef]"

Can the value of the variable be carried out to this formula in the cell?
Thanks, Numbers
 
uSe a funtion wrapper for the variable. You might find this kind of function called an Accessor in some terminology.

Code:
' Assuming intColRef is definied as a global
Public Function GetIntColRef() As String
  GetIntColRef = intColRef 
End Function

Then in your cell, you can use ActiveCell.FormulaR1C1 = "=RC[" & GetIntColRef & "]"

Well, I don't know for sure since the last Excel macro I wrote was a million years ago, but the theory is sound ;)

 
Simply use this in your code:
ActiveCell.FormulaR1C1 = "=RC[" & intColRef & "]"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for the help. This works well.
Numbers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top