#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default SUM Macro

Hi,

I am a novice to the Macro world and need your help.
Can some one help me write a macro of the type:

=SUM("A" & =FIND_FIRST(column_name, value), "A" &
=FIND_LAST(column_name, value))

any help is appreatiated.

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,510
Default SUM Macro

If I interpret you request correctly, you want to find the first and last
values in a column of numbers and sum them. Following is 2 macros. First one
does not take into account column headers. 2nd amendment assumes column
headers and starts at row 2 to find the first value.

Sub SumColumnNoHeaders()

Dim firstRow As Long
Dim lastRow As Long
Dim column_Name As String
Dim sumValue As Double

column_Name = "A"

With ActiveSheet
'Assuming no column headers
If .Cells(1, column_Name) = "" Then
firstRow = .Cells(1, column_Name).End(xlDown).Row
Else
firstRow = 1
End If


lastRow = .Cells(.Rows.Count, column_Name).End(xlUp).Row
End With

sumValue = WorksheetFunction.Sum(Range(Cells(firstRow, column_Name), _
Cells(lastRow, column_Name)))

'alternative code method
'sumValue = WorksheetFunction.Sum(Range(column_Name & firstRow & _
":" & column_Name & lastRow))



End Sub


If you have column headers then replace the If/Else/End If lines with the
following:-

'With column headers present
If .Cells(2, column_Name) = "" Then
firstRow = .Cells(2, column_Name).End(xlDown).Row
Else
firstRow = 2
End If


Regards,

OssieMac


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 14
Default SUM Macro

On Oct 31, 12:29 pm, OssieMac
wrote:
If I interpret you request correctly, you want to find the first and last
values in a column of numbers and sum them. Following is 2 macros. First one
does not take into account column headers. 2nd amendment assumes column
headers and starts at row 2 to find the first value.

Sub SumColumnNoHeaders()

Dim firstRow As Long
Dim lastRow As Long
Dim column_Name As String
Dim sumValue As Double

column_Name = "A"

With ActiveSheet
'Assuming no column headers
If .Cells(1, column_Name) = "" Then
firstRow = .Cells(1, column_Name).End(xlDown).Row
Else
firstRow = 1
End If

lastRow = .Cells(.Rows.Count, column_Name).End(xlUp).Row
End With

sumValue = WorksheetFunction.Sum(Range(Cells(firstRow, column_Name), _
Cells(lastRow, column_Name)))

'alternative code method
'sumValue = WorksheetFunction.Sum(Range(column_Name & firstRow & _
":" & column_Name & lastRow))

End Sub

If you have column headers then replace the If/Else/End If lines with the
following:-

'With column headers present
If .Cells(2, column_Name) = "" Then
firstRow = .Cells(2, column_Name).End(xlDown).Row
Else
firstRow = 2
End If

Regards,

OssieMac


Thank a lot for your response.... but requirement is something like
this:

The original table
ID value
1 10
1 10
2 10
2 10
2 10
3 10
3 10

I want to write a macro that will help me acrive this

ID value
1 10
1 10
20

2 10
2 10
2 10
30

3 10
3 10
20

I want a macro of type
=SUM(.....) that can be put in a cell... because the number of rows
cal increase or decrese dynamically.

Thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default SUM Macro

<<I want to write a macro that will help me derive this

ID value
1 10
1 10
20

2 10
2 10
2 10
30

3 10
3 10
20

I want a macro of type
=SUM(.....) that can be put in a cell... because the number of rows can
increase or decrease dynamically.

You might look at the DSUM worksheet function, but you will have to use
some cells to define the criteria you want. If you are trying to produce
the SUM for ALL groups of data rows, then I would suggest using a pivot
table.

--
Regards,
Bill Renaud



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 417
Default SUM Macro

Actually, I think the SUMIF worksheet function would work best in this
situation. Assume your data is in the following form:

ID value
1 10
1 10
2 10
2 10
2 10
3 10
3 10

=SUMIF(A2:A8,2,B2:B8)

....will sum all values in column $B where the cells in column $A equal 2.

--
Regards,
Bill Renaud





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
Macro recorded... tabs & file names changed, macro hangs Steve Excel Worksheet Functions 3 October 30th 09 11:41 AM
Macro not showing in Tools/Macro/Macros yet show up when I goto VBA editor [email protected] Excel Programming 2 March 30th 07 07:48 PM
how to count/sum by function/macro to get the number of record to do copy/paste in macro tango Excel Programming 1 October 15th 04 01:16 PM
macro to delete entire rows when column A is blank ...a quick macro vikram Excel Programming 4 May 3rd 04 08:45 PM
Start Macro / Stop Macro / Restart Macro Pete[_13_] Excel Programming 2 November 21st 03 05:04 PM


All times are GMT +1. The time now is 06:42 AM.

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

About Us

"It's about Microsoft Excel"