Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Add sum formula automatically using VBA

Hi,

I'm just starting to learn vba and I'm trying to write a macro that
automatically writes a sum formula at a specific row (row which has the words
"Total dos custos" in column A). This sum formula adds the values of the rows
above which include a sum formula as well.
For example:
At row 34 the cell A34 has the words "Total dos Custos", then from cell b34
until j34 I would like to have a sum formula which sums the cells above which
also have a sum formula.
Supposing that in column B there are already two sums, in rows 12 and 25,
the cell b34 would have the following formula =sum(b12,b25).
In cell c34 the formula would be =sum(c12,c25), etc.
Basically this creates a Grand Total in the row which has the words "Total
dos Custos".

Any help ?

thanks a lot,
Pedro


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,420
Default Add sum formula automatically using VBA

Sub AddSums()
Dim cell As Range
Dim i As Long
Dim j As Long
Dim tmp As String

Set cell = Columns(1).Find("Total dos Custos")
If Not cell Is Nothing Then

For j = 2 To 10

tmp = ""
For i = 1 To cell.Row - 1

If Left$(Cells(i, j).Formula, 5) = "=SUM(" Then _
tmp = tmp & Cells(i, j).Address(False, False) & ","
Next i

If tmp < "" Then Cells(cell.Row, j).Formula = "=SUM(" &
Left$(tmp, Len(tmp) - 1) & ")"
Next j
End If
End Sub


--
__________________________________
HTH

Bob

"Pmxgs" wrote in message
...
Hi,

I'm just starting to learn vba and I'm trying to write a macro that
automatically writes a sum formula at a specific row (row which has the
words
"Total dos custos" in column A). This sum formula adds the values of the
rows
above which include a sum formula as well.
For example:
At row 34 the cell A34 has the words "Total dos Custos", then from cell
b34
until j34 I would like to have a sum formula which sums the cells above
which
also have a sum formula.
Supposing that in column B there are already two sums, in rows 12 and 25,
the cell b34 would have the following formula =sum(b12,b25).
In cell c34 the formula would be =sum(c12,c25), etc.
Basically this creates a Grand Total in the row which has the words "Total
dos Custos".

Any help ?

thanks a lot,
Pedro




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default Add sum formula automatically using VBA

presume B has values? Isn't the formula in C3 just = B2 + C2
and replicate down

in code

Range("C2").Formula = "=B2"
With range(Range(C3),Range(C3).End(xlDown))
.formular1c1 = "=RC2 + R[-1]C"
End with


"Pmxgs" wrote:

Hi,

I'm just starting to learn vba and I'm trying to write a macro that
automatically writes a sum formula at a specific row (row which has the words
"Total dos custos" in column A). This sum formula adds the values of the rows
above which include a sum formula as well.
For example:
At row 34 the cell A34 has the words "Total dos Custos", then from cell b34
until j34 I would like to have a sum formula which sums the cells above which
also have a sum formula.
Supposing that in column B there are already two sums, in rows 12 and 25,
the cell b34 would have the following formula =sum(b12,b25).
In cell c34 the formula would be =sum(c12,c25), etc.
Basically this creates a Grand Total in the row which has the words "Total
dos Custos".

Any help ?

thanks a lot,
Pedro


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Add sum formula automatically using VBA

It worked exactly like I wanted.

Thanks a lot


"Bob Phillips" wrote:

Sub AddSums()
Dim cell As Range
Dim i As Long
Dim j As Long
Dim tmp As String

Set cell = Columns(1).Find("Total dos Custos")
If Not cell Is Nothing Then

For j = 2 To 10

tmp = ""
For i = 1 To cell.Row - 1

If Left$(Cells(i, j).Formula, 5) = "=SUM(" Then _
tmp = tmp & Cells(i, j).Address(False, False) & ","
Next i

If tmp < "" Then Cells(cell.Row, j).Formula = "=SUM(" &
Left$(tmp, Len(tmp) - 1) & ")"
Next j
End If
End Sub


--
__________________________________
HTH

Bob

"Pmxgs" wrote in message
...
Hi,

I'm just starting to learn vba and I'm trying to write a macro that
automatically writes a sum formula at a specific row (row which has the
words
"Total dos custos" in column A). This sum formula adds the values of the
rows
above which include a sum formula as well.
For example:
At row 34 the cell A34 has the words "Total dos Custos", then from cell
b34
until j34 I would like to have a sum formula which sums the cells above
which
also have a sum formula.
Supposing that in column B there are already two sums, in rows 12 and 25,
the cell b34 would have the following formula =sum(b12,b25).
In cell c34 the formula would be =sum(c12,c25), etc.
Basically this creates a Grand Total in the row which has the words "Total
dos Custos".

Any help ?

thanks a lot,
Pedro





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add sum formula automatically Pmxgs Excel Programming 0 September 6th 09 04:04 PM
formula pick up formula from another cell automatically jjaylad Excel Worksheet Functions 1 July 30th 09 05:35 PM
Replace formula with value automatically. datakman Excel Worksheet Functions 4 July 11th 09 03:12 AM
Run formula automatically kimmiesings Excel Worksheet Functions 3 July 7th 08 01:04 PM
automatically apply formula to new row CrazySwayze Excel Programming 1 May 10th 07 04:28 PM


All times are GMT +1. The time now is 08:55 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"