Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Zeepowlee
 
Posts: n/a
Default Macro for column sum

Hi all

I need a macro to add a column its sum, right below the end of the column.
Column length (i.e. # of rows) is variable.

For instance, let's say data is in D1:D50,
I want the column total in cell D51.
If the data was in D1:D15, I would want the sum in D16.


Thanks for help



  #2   Report Post  
Posted to microsoft.public.excel.worksheet.functions
excelent
 
Posts: n/a
Default Macro for column sum

Sub SelectFirstToLastInColumn()
Dim TopCell, BottomCell, x
Set TopCell = Cells(1, ActiveCell.Column)
Set BottomCell = Cells(16384, ActiveCell.Column)
If IsEmpty(TopCell) Then Set TopCell = TopCell.End(xlDown)
If IsEmpty(BottomCell) Then Set BottomCell = BottomCell.End(xlUp)
If TopCell.Row = 16384 And BottomCell.Row = 1 Then ActiveCell.Select
Else Range(TopCell, BottomCell).Select
x = Selection.Address
Cells(BottomCell.Row + 1, ActiveCell.Column).Select
ActiveCell.Formula = "=sum(" & x & ")"
End Sub

  #3   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Freightliner
 
Posts: n/a
Default Macro for column sum

I dont understand where you close the If..Then blocks.
It doesn't work.

thanks for help




"excelent" wrote in message
...
Sub SelectFirstToLastInColumn()
Dim TopCell, BottomCell, x
Set TopCell = Cells(1, ActiveCell.Column)
Set BottomCell = Cells(16384, ActiveCell.Column)
If IsEmpty(TopCell) Then Set TopCell = TopCell.End(xlDown)
If IsEmpty(BottomCell) Then Set BottomCell = BottomCell.End(xlUp)
If TopCell.Row = 16384 And BottomCell.Row = 1 Then ActiveCell.Select
Else Range(TopCell, BottomCell).Select
x = Selection.Address
Cells(BottomCell.Row + 1, ActiveCell.Column).Select
ActiveCell.Formula = "=sum(" & x & ")"
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.worksheet.functions
excelent
 
Posts: n/a
Default Macro for column sum

hope this help:

Sub SelectFirstToLastInColumn() ' line 1
Dim Top, Bot, x ' line 2
Set Top = Cells(1, ActiveCell.Column) ' line 3
Set Bot = Cells(16384, ActiveCell.Column) ' line 4
If IsEmpty(Top) Then Set Top = Top.End(xlDown) ' line 5
If IsEmpty(Bot) Then Set Bot = Bot.End(xlUp) ' line 6
If Top.Row = 16384 And Bot.Row = 1 Then ActiveCell.Select Else
Range(Top, Bot).Select ' line 7
x = Selection.Address ' line 8
Cells(Bot.Row + 1, ActiveCell.Column).Select ' line 9
ActiveCell.Formula = "=sum(" & x & ")" ' line 10
End Sub ' line 11

  #5   Report Post  
Posted to microsoft.public.excel.worksheet.functions
excelent
 
Posts: n/a
Default Macro for column sum

remember select column then run macro

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
Search, Copy, Paste Macro in Excel [email protected] Excel Worksheet Functions 0 January 3rd 06 06:51 PM
Closing File Error jcliquidtension Excel Discussion (Misc queries) 4 October 20th 05 12:22 PM
macro with F9 Kenny Excel Discussion (Misc queries) 1 August 3rd 05 02:41 PM
Make Alignment options under format cells available as shortcut dforrest Excel Discussion (Misc queries) 1 July 14th 05 10:58 PM
Playing a macro from another workbook Jim Excel Discussion (Misc queries) 1 February 23rd 05 10:12 PM


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