View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 6,953
Default increment a row reference with addition

with very minimal testing, this worked for me

Sub FormulaCvt(rng As Range, rowoffst As Long)
Dim cell As Range, cell1 As Range
For Each cell In rng
s = cell.Formula
On Error Resume Next
Set cell1 = cell.Offset(-1 * rowoffst, 0)
On Error GoTo 0
If Not cell1 Is Nothing Then
s1 = Application.ConvertFormula(s, xlA1, _
xlR1C1, , ActiveCell.Offset(-1 * rowoffst, 0))
cell.FormulaR1C1 = s1
Else
MsgBox "Can't convert"
Exit Sub
End If
Next
End Sub

Sub Tester1()
ActiveCell.Formula = "=H325+H320+H308+H303"
Debug.Print ActiveCell.Formula
FormulaCvt ActiveCell, 2
Debug.Print ActiveCell.Formula
End Sub

---------------
Results in the immediate window we
=H325+H320+H308+H303
=H327+H322+H310+H305


--
Regards,
Tom Ogilvy


"cass calculator" wrote:

Here is what I am trying to do:

1. select a range of cells, which may or may not be continuous
2. change the row number reference in each of the cell's formulas by
adding or subtracting to an integer to each reference to a row number

i.e. if one of the cells in the range had the formula

=H325+H320+H308+H303

I would like to be able to add or subtract an integer to it to get (in
the instance the integer was +2)

=H327+H322+H310+H305

This operation would need to repeat for each cell in the selected
range.

I read a few similar posts to try and get this to work, but didn't
have any success. A solution would be greatly appreciated !

Thanks,

Joshua