# Macro

11 views

### Peter

Sep 1, 2003, 10:41:59 AM9/1/03
to
I need a macro that will do the following for me:
I have an accounts programme that outputs to Excel. Cells
C1:C70 contain the code "1300"; cells C74:c80 contain the
code "1310" and so on for 820 rows. I need to insert a
row under row 70 and to put the total of J1:J70 in cell
N71. So the macro will travel down Column C and when the
code changes, insert a row, and put the total in Column
N. Thus I will end up with the total of each code in
Column N. I'm sure it's not difficult, but I can't work
it out myself.
Thanks.
Peter

### Dave Peterson

Sep 1, 2003, 11:14:26 AM9/1/03
to
There's an option built into excel that almost does what you want. (but it puts
subtotals in the same column as the raw data).

Take a look at Data|subtotals

(Since this is such a useful function, I'd try to accept the "wrong" column
stuff!)

--

Dave Peterson
ec3...@msn.com

### J.E. McGimpsey

Sep 1, 2003, 11:40:51 AM9/1/03
to
One way:

Public Sub SubtotalIt()
Dim cell As Range
Cells.Subtotal GroupBy:=3, _
Function:=xlSum, _
TotalList:=Array(10), _
Replace:=True, _
PageBreaks:=False, _
SummaryBelowData:=True
For Each cell In Columns(3).Cells.SpecialCells( _
xlCellTypeConstants, xlTextValues)
If InStr(cell.Text, " Total") Then _
cell.Offset(0, 7).Cut cell.Offset(0, 11)
Next cell
End Sub

In article <084201c37097\$32da5d50\$a601...@phx.gbl>,

### SteveS

Sep 1, 2003, 2:23:09 PM9/1/03
to
Here is another way. Paste the following code in a macro.
Select the cell you want to start from (1300) and run the
macro:

(watch for line wrap)

Dim Cell2Check
'holds the current total of the coulumn
Dim ColTotal As Single

Dim Col2Comp As Integer
Dim ValCol As Integer
Dim TotCol As Integer

Dim CompareColumn As String
Dim Total2Column As String

' CHANGE THESE LETTERS TO
' can be upper or lower case
'==============================
CompareColumn = "C" ' 1300, 1310 ,etc
Total2Column = "n" ' column to put total
'==============================

' Initialize variables

' convert to uppercase and subtract 64 to get column number
Col2Comp = Asc(UCase(CompareColumn)) - 64
' these two are relative positions to the activecell
(Col2Comp)
' and are GT so subtract to get offset
ValCol = Asc(UCase(Numbers2AddCol)) - 64 - Col2Comp
TotCol = Asc(UCase(Total2Column)) - 64 - Col2Comp
ColTotal = 0

' goto first cell
'ActiveSheet.Cells(Row2Start, Col2Comp).Select
'Get first value
Cell2Check = ActiveCell.Value
'get first number
'ColTotal = ActiveCell.Offset(0, ValCol).Value

Do While ActiveCell.Value <> ""
' if current cell value = saved value
Do While ActiveCell.Value = Cell2Check
' add current number to running total
ColTotal = ColTotal + ActiveCell.Offset(0,
ValCol).Value
' move down one row
ActiveCell.Offset(1, 0).Activate
Loop

ActiveCell.EntireRow.Insert
' write total to cell
ActiveCell.Offset(0, TotCol).Value = ColTotal
' move down one row
ActiveCell.Offset(1, 0).Activate
'starting again so....
' get the new value of the cell to check
Cell2Check = ActiveCell.Value
'clear column total
ColTotal = 0
Loop

Yes, longer but more control....

HTH

Steve
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)

>.
>

### Peter

Sep 2, 2003, 7:27:05 AM9/2/03
to
Very many thanks for doing this for me. I have run it
and it works nicely. I have been doing this manually for
some years now, and much appreciate the automation.

Peter

>.
>

### Peter

Sep 2, 2003, 7:28:20 AM9/2/03
to
Very many thanks. This is incredible! I have been doing
manuals!

Peter

>.
>

### Peter

Sep 2, 2003, 7:29:16 AM9/2/03
to
Very many thanks. I have run this and it works fine. See
my thanks to the others.

Peter

>.
>