Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 21
Default Total by Selection by Column, Help Please.

Suppose i select a certain range, then a start a macro.
i want that macro to sum all the values for each column
included in my selection and to have the results just below.


suppose i have a situation like this :
A B C
----------------------------
1 3 9 7
2 5 1 8
3 1 5 3
3 2 1 1


I select range A1:C3, i start the macro, then i have the results like this ...

A B C
----------------------------
1 3 9 7
2 5 1 8
3 1 5 3
3 2 1 1
----------------------------
4 11 16 19

Important Note : range A1:C3 may be whatever, like F7:M26 or B9:F12,
for the case F7:M26 the results will be in F27:M27
for the case B9:F12 the results will be in B13:F13

Any Help ?

Best Regards.

Robert.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Total by Selection by Column, Help Please.

Hi Robert,

Here's one way to do it:

Sub AddSUMs()
Dim col As Range
Dim lTopRow As Long
Dim lRows As Long

If Selection.Areas.Count 1 Then
MsgBox "Unable to process multiple areas."
Else
lTopRow = Selection(1, 1).Row
lRows = Selection.Rows.Count
For Each col In Selection.Columns
With Cells(lTopRow, col.Column)
.Offset(lRows).Formula = _
"=SUM(" & .Resize(lRows, 1).Address & ")"
End With
Next col
End If
End Sub

You may want to add some error handling, but this should work for you,
assuming you select a rectangular range. If not, you'd have to loop through
each column and find the cell below the last selected cell.

BTW, I looked at the code generated by the AutoSum button in Excel. I
thought there may be a corresponding VBA function, but there isn't. And the
generated code was pretty ugly. <g

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]


Bubu wrote:
Suppose i select a certain range, then a start a macro.
i want that macro to sum all the values for each column
included in my selection and to have the results just below.


suppose i have a situation like this :
A B C
----------------------------
1 3 9 7
2 5 1 8
3 1 5 3
3 2 1 1


I select range A1:C3, i start the macro, then i have the results like
this ...

A B C
----------------------------
1 3 9 7
2 5 1 8
3 1 5 3
3 2 1 1
----------------------------
4 11 16 19

Important Note : range A1:C3 may be whatever, like F7:M26 or B9:F12,
for the case F7:M26 the results will be in F27:M27
for the case B9:F12 the results will be in B13:F13

Any Help ?

Best Regards.

Robert.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Total by Selection by Column, Help Please.


hi babu..may be this helps you.

Dim sumrow As Double

Sub aaa()
Sheets("Sheet1").Activate
For i = Selection.Column To Selection.Columns.Count + Selection.Column
- 1
For j = Selection.Row To Selection.Rows.Count + Selection.Row - 1
sumrow = sumrow + Cells(j, i)
Next j
Cells(Selection.Row + Selection.Rows.Count, i) = sumrow
sumrow = 0
Next i
End Sub


------------------------------------------------
~~ Message posted from http://www.ExcelTip.com/
~~View and post usenet messages directly from http://www.ExcelForum.com/

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
Count number of cells and total in one column, based on another column suffix Pierre Excel Worksheet Functions 5 October 31st 07 12:28 AM
Total column changes colors when total equals sum of other columns newstacy New Users to Excel 1 April 21st 07 09:00 PM
disable Total and/or Sub total for a single column Voyager Excel Worksheet Functions 3 February 14th 07 10:51 PM
XL formula - total of row = total of column topaz Excel Worksheet Functions 2 March 17th 05 10:04 PM


All times are GMT +1. The time now is 12:08 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"