StayAtHomeMom
Technical User
I am using VB code behind a button on a form in Access to create an Excel file (from a query) and format it. Part of the formatting is to place formulas in a column, using cell contents from another column. For instance, starting with cell N2, the formula should read “=L2/$L$15”; cell N3 would read “=L3/$L$15”, and so on. The problem is with the absolute values I’m using. I can’t figure out the proper syntax when using the R1C1 notation within the code. The other complexity is that I’m using this code to create separate sheets within the Excel file, and each sheet will have a different row value for the totals line I’m using in the formula (the denominator). I thought by using a variable to hold the row value that’s holding the total line (what I call “totrow”), I would lock in the value of the row within the formula. But, Excel increments the row value, so I end up with a lot of #DIV/0# errors.
Can someone shed some light on the proper syntax to imbed an absolute cell value within an R1C1 formula (see ---> below)? I’ve pasted a portion of the code below, where I’m calculating the totals for certain columns, and using those totals in the formula I’m trying to come up with.
rowcount = 2
Do While (Cells(rowcount, 1).Value <> "") 'figure out the total number of records/rows for this disty
rowcount = rowcount + 1
Loop
'add totals line for col 5 thru 12
Cells(rowcount, 1).Select
ActiveCell.FormulaR1C1 = "TOTALS"
For numbcol = 5 To 12
Cells(rowcount, numbcol).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-" & rowcount - 1 & "]C)" 'totaling Year and YTD columns
Next numbcol
'compute Market Share; i.e. SalesInTerr/TotalSales
totrow = rowcount - 2 'totrow will be row with cumulative total for the column
rowcount = 2
Cells(rowcount, 14).Select
Do While (Cells(rowcount, 1).Value <> "")
---> ActiveCell.FormulaR1C1 = "=RC[-2]/$R[" & totrow & "]$C[-2]"
rowcount = rowcount + 1
Cells(rowcount, 14).Select
Loop
THANKS IN ADVANCE FOR YOUR EXPERTISE!
Can someone shed some light on the proper syntax to imbed an absolute cell value within an R1C1 formula (see ---> below)? I’ve pasted a portion of the code below, where I’m calculating the totals for certain columns, and using those totals in the formula I’m trying to come up with.
rowcount = 2
Do While (Cells(rowcount, 1).Value <> "") 'figure out the total number of records/rows for this disty
rowcount = rowcount + 1
Loop
'add totals line for col 5 thru 12
Cells(rowcount, 1).Select
ActiveCell.FormulaR1C1 = "TOTALS"
For numbcol = 5 To 12
Cells(rowcount, numbcol).Select
ActiveCell.FormulaR1C1 = "=SUM(R[-2]C:R[-" & rowcount - 1 & "]C)" 'totaling Year and YTD columns
Next numbcol
'compute Market Share; i.e. SalesInTerr/TotalSales
totrow = rowcount - 2 'totrow will be row with cumulative total for the column
rowcount = 2
Cells(rowcount, 14).Select
Do While (Cells(rowcount, 1).Value <> "")
---> ActiveCell.FormulaR1C1 = "=RC[-2]/$R[" & totrow & "]$C[-2]"
rowcount = rowcount + 1
Cells(rowcount, 14).Select
Loop
THANKS IN ADVANCE FOR YOUR EXPERTISE!