Building formula contents through VBA

vmser

Board Regular
Joined
Jan 4, 2010
Messages
72
I'm fairly good with handling data through .Value, but for this specific case I need to work through .Formula, and I'm struggling.

The basic concept is as follows:

1) Fill all needed cells with =formul() (formul being a user defined function)
2) if I need to add an item there, I check if the right two characters of the .formula property match "()"
a) If it does, it's the first item I need to add, and I replace the last ")" with raming!J104) (raming is the name of the tab, column J is fixed, the row number is what I'm filling in)
b) If it doesn't match, it's not the first, and I need to add ;"+";raming!J104)


So the cell contents should be like:
=formul()
=formul(raming!J104)
=formul(raming!J104;"+";raming!J108)

Given the fact I need to force the quotation marks in there, how would I build that from the .formula property? And can I work through 'Replace()' to do so?


EDIT:
I just realised I have a second such problem. I have a variating (but trackable) number of non-consecutive colums of which I have to make a sum of the values.

Assume I need, for each row, to make the sum of the elements in columns 10,13,16,19,.. (so basically 7+3*i, with i an integer ranging from 1 to the amount of 'chapters' I'm dealing with. How would I go and create the formula for each row? (FormulaR1C1 and Formula are both fine, though I don't know if the R1C1 format allows you to used variables within)
 
Last edited:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Given I'm aparantly too late to edit, here's some additional info:

The numerical value (104 in the example) is read from an array. Syntax to call those is like this: postdata(2,counter) (with counter being the counter use, obviously)
 
Upvote 0
To answer the second one first: FormulaR1C1 is your best bet here. An example:
Code:
chapters = 6
startcol = 10
    Cells(10, 4).FormulaR1C1 = "=RC" & startcol
For i = 1 To chapters
    Cells(10, 4).FormulaR1C1 = Cells(10, 4).FormulaR1C1 & "+RC" & startcol + i * 3
Next i

The result of the first FormulaR1C1 line is the formula =$J10 The J is a fixed reference because there is a specific number after the C term in the R1C1 reference. The 10 is a relative reference to the same row in which the formula appears because there is no number after the R term. To create a relative reference to somewhere else, you use brackets, for example R[3]C is a reference to three rows down from the current cell.
The result of the loop of i to chapters is a growing formula, adding the next +$M10, +$P10, +$s10..... terms to the formula one at a time.
 
Last edited:
Upvote 0
To answer the second one first: FormulaR1C1 is your best bet here. An example:
Code:
chapters = 6
startcol = 10
    Cells(10, 4).FormulaR1C1 = "=RC" & startcol
For i = 1 To chapters
    Cells(10, 4).FormulaR1C1 = Cells(10, 4).FormulaR1C1 & "+RC" & startcol + i * 3
Next i

The result of the first FormulaR1C1 line is the formula =$J10 The J is a fixed reference because there is a specific number after the C term in the R1C1 reference. The 10 is a relative reference to the same row in which the formula appears because there is no number after the R term. To create a relative reference to somewhere else, you use brackets, for example R[3]C is a reference to three rows down from the current cell.
The result of the loop of i to chapters is a growing formula, adding the next +$M10, +$P10, +$s10..... terms to the formula one at a time.

Okay, I'll fiddle around with that, thank you. Any suggestion on the first question?
 
Upvote 0

Forum statistics

Threads
1,215,009
Messages
6,122,674
Members
449,091
Latest member
peppernaut

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