Macro and Subtotals in one column problem

  • Thread starter Thread starter Tomas
  • Start date Start date
T

Tomas

Anybody could help me out with this:

I am adding up numbers in one column that are seperated by a blank
cell. I would like to set up a macro that would add up numbers that
are above the active cell; the macros should not add up the entire
column though, only numbers above the current cell up to an empty
cell. I tried to set up a macro myself but it would record the exact
current position of my active cell, whereas I need the active cell to
change from one sum to another. As example, in a column I would have:

1
2
3
Sum1
<empty cell>
4
5
6
Sum2
<empty cell>
7
8
etc.

Can somebody advise what function I need to use or how to solve this
problem?

Thanks!

Tomas
(e-mail address removed)
 
Tomas,

Macros aren't usually used for totals. You haven't provided enough
information about your sheet for me to know if the following two suggestions
are useful:

If you always have the same count of rows to be added (you show three in
your example), you can make a formula (=SUM(A2:A4) ) and paste it into the
places where you want totals.

Or consider using Data - Subtotals.
 
Earl,

thank you for your reply.

The problem is following:

I am adding up payments of different people, the first row always
contains name/identification number etc. and also the first payment.
Then the second row contains next payment, third row third payment
etc. The number of payments varies for each person, though. The
purpose of a macro is that I add the persons' payments individually,
maybe 1, 2 or more each day, and then I need the resulting sum be
copied in the cell next to it. Therefore I cannot use the Subtotals
function, I believe. Each person is separated by a blank row from each
other. A basic example would be:

FirstName1, LastName1, 10
20
30
40
Total

FirstName2, LastName2, 30
40
Total

FirstName3, LastName3, 10
30
40
50
60
80
Total
etc...

Could you please advise?

Thanks in advance!

Tomas
 
I think if you put the corresponding naems on each row, you could do a lot more
with your data.

If you clean up the subtotals (remove those rows), then remove the blank lines,
you can fill the name columns using techniques described at Debra Dalgleish's
site:

http://www.contextures.com/xlDataEntry02.html

Then after you clean up the data, when you insert a new row, you can just copy a
similar one and paste it. Then change the value in column C(?).

But if that's not an option, maybe you could just fix those total formulas so
that the range expands when you add a new row above the total.

For instance:
If the first cell that has a total is in C5 and your formula looks like:
=sum(C1:C4)
you could replace it with this:
=SUM(C1:OFFSET(C5,-1,0))

That offset stuff says to start at the cell that contains the total and move up
one row. So it reacts nicely when you insert a new row 5.

If there are too many to do manually, maybe you could have a macro do it for
you.

But first, try it against a copy of your workbook--or close without saving if it
screws up.

Option Explicit
Sub testme()

Dim myRng As Range
Dim myCell As Range
Dim wks As Worksheet
Dim myFormula As String
Dim colonPos As Long

Set wks = ActiveSheet

With wks
Set myRng = Nothing
On Error Resume Next
Set myRng = .Range("c:c").Cells.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0

If myRng Is Nothing Then
MsgBox "No Formulas Found"
Exit Sub
End If

For Each myCell In myRng.Cells
myFormula = myCell.Formula
If LCase(myFormula) Like "=sum(*:*)" Then
'=sum(c1:c4)
'=SUM($C$1:OFFSET(C5,-1,0))
colonPos = InStr(1, myFormula, ":")
myFormula = Left(myFormula, colonPos) & "offset(" & _
myCell.Address(0, 0) & ",-1,0))"
myCell.Formula = myFormula
Else
MsgBox myCell.Address(0, 0) & " was not changed"
End If
Next myCell
End With

End Sub

If looks for formulas like:

=sum(c1:c99) (in cell C100)
and converts it to
=sum(c1:offset(c100,-1,0))

But there's almost no error checking to make sure it finds the correct formulas,
so double check the output.

If your formula is something different, then don't use this. (or modify it to
do what you want.)

If you're new to macros, you may want to read David McRitchie's intro at:
http://www.mvps.org/dmcritchie/excel/getstarted.htm

Short course:
Hit alt-f11 to get to the VBE (where macros/UDF's live)
hit ctrl-R to view the project explorer
Find your workbook.
should look like: VBAProject (yourfilename.xls)

right click on the project name
Insert, then Module
You should see the code window pop up on the right hand side

Paste the code in there. But change the cell addresses to save and the names of
the worksheets.

Now go back to excel.
click on Tools|macro|macros...
click on the macro name (testme)
and then click run.

After you're done, you can dump the macro (or keep it for future use???).

But if you're bothered by any "contains macros" warning message when you open
the file, you can read Debra Dalgleish's site to see how to clean it up.

http://www.contextures.com/xlfaqMac.html#NoMacros
 
This sentence was copied from a previous post. It should have been editted.

Paste the code in there. But change the cell addresses to save and the names of
the worksheets.

to

Paste the code in there.

<<snipped>>
 
Hi,

thanks for youe suggestions. However, isn't there a simplier way? All
I need the macro to do is basicly the following (I have a very very
little knowledge of Basic/Pascal ..:-)


Begin

PaymentTotal: real
Counter, OriginalCellRow: integer

PaymentTotal = 0
Counter = ActiveCell.Row - 1
OriginalCellRow = ActiveCell.Row

Do Until ActiveCell(Counter) = 0
PaymentTotal = PaymentTotal + ActiveCell
Counter = Counter - 1
Loop

GoTo ActiveCell(OriginalCellRow) (this would move back to the
cell below the payments)
Print PaymentTotal

End.


In another words, the macro would pick the number from the current
cell, add it up to the PaymentTotal and continue until the CurrentCell
equals zero - that is the blank cell seperating the individual
persons.

Can somebody translate this into an Excel Macro, please?

Thank you in advance,

Tomas
 
I thought you already had subtotals in your data? How does your code account
for them?

Or have you removed them -- by clearing the contents (and leaving the row) or
deleting the row.

I'm hesitant to suggest anything more without more info about your data.
 
Back
Top