Using variable in formula

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
Ok, need some help in the formula line of the code below I need it to use the stored value instead of a set cell reference but I can't figure out the syntax.

Code:
Sub test()

Dim c As Range
Dim R1 As Integer
Dim R2 As Integer
R1 = 2
Dim fillto As String
Dim newa As String

For Each c In Range("A:A")
       If Columns("A").Find(What:="Total", After:=ActiveCell, LookIn:= _
        xlFormulas, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate Then
        R2 = ActiveCell.Row
        fillto = "D" & R2
        Cells(R1, 4).Select
[COLOR=Red]        ActiveCell.FormulaR1C1 = "=RC[-1]" / fillto[/COLOR]
        Range("D2").Select
        Selection.Autofill Destination:=Range("D2", fillto), Type:=xlFillDefault
        newa = "A" & R2
        R1 = R2 + 1
        Range(newa).Select
        End If
Next c

End Sub

Thanks for any help you can give me!
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Code:
ActiveCell.Formula = "=" & fillto
 
Upvote 0
Not so much, it just references the new cell as text and not as a cell for comparison

Code
ActiveCell.FormulaR1C1 = "=RC[-1]/" & fillto

generates the following formula
=C2/'C5'

intead of just
=c2/c5
 
Last edited:
Upvote 0
I guess what I really need is a line of code that will convert the string value stored in "Fillto" into a range. Any help?
 
Upvote 0
In plain words, what is the code supposed to do?
 
Upvote 0
The data takes the individual values from column C and divides them from the total count that is under each group of them in column C. The issue is I can't set a fixed value for the formula to reference since the number of individual items in each group can change and would thus throw off where the total value to divide by is stored.
 
Upvote 0
In a little more detail, maybe?

And post something that shows your data layout?
 
Upvote 0
Not so much, it just references the new cell as text and not as a cell for comparison

Code
ActiveCell.FormulaR1C1 = "=RC[-1]/" & fillto

generates the following formula
=C2/'C5'

intead of just
=c2/c5

You are mixing reference types. Either use Formula with A1 style references or FormulaR1C1 with R1C1 style references. Maybe:
Code:
        fillto = "R" & R2 & "C4"
 
Upvote 0

Forum statistics

Threads
1,221,418
Messages
6,159,790
Members
451,589
Latest member
Harold14

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