View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Mat P:son[_2_] Mat P:son[_2_] is offline
external usenet poster
 
Posts: 97
Default inserting formula

Quick and dirty (using RC notation for the formula would be neater though):

==============================

Private Sub CommandButton4_Click()

Dim rng As Range, cell As Range
Set rng = Columns(1).SpecialCells(xlConstants)

Dim row As Long
For Each cell In rng

row = cell.row
If row 1 Then
cell.Offset(0, 8).Formula = _
"=sum(9,F" & row & ":H" & row & ")"
End If
Next cell

End Sub

==============================

But I can't help wondering: do you really need a macro to do this? Maybe
using a macro is a bit of overkill for this particular problem...? Couldn't
you use a normal Excel formula instead, and copy it to the cells you want to
fill in?

What about putting this into cell I9:

=IF($A2="";"";SUM(9;$F2:$H2))

And then "pulling it down" all the way to the end of your range (i.e., use
the mouse to grab the plus sign at the bottom right of cell I9, then click
and drag). Would that do the trick, perhaps?

Cheers,
/MP


"enyaw" wrote:

How do i ignore the first row? I also need to be able to sum three cells in
the row. Any ideas on how to this? This is what i am using.

Sub CommandButton4_Click()
Dim rng As range, cell As range
Set rng = Columns(1).SpecialCells(xlConstants) ' or xlformulas
For Each cell In rng
cell.Offset(0, 8).Formula = "=sum(9,F2:H2)"
Next

End Sub

"Tom Ogilvy" wrote:

Dim rng as Range, cell as Range
set rng = Columns(1).Specialcells(xlConstants) ' or xlformulas
for each cell in rng
cell.offset(0,1).formula = "=if(" & cell.Address(0,0) & _
"<"""",""Filled"",""empty"")"
Next

--
regards,
Tom Ogilvy


"enyaw" wrote:

I want to insert a formula into a row if the first cell in the row has
something in it. I need to check through a range of cells but the range of
cells will be different every time. How would i program this?