Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
DG DG is offline
external usenet poster
 
Posts: 46
Default Sum a column of data

How do I sum a column of data in a macro?

Assume I have a worksheet and cells C1 through C20 have numbers. How can I
get the sum of those numbers without a loop?

I tried
Total = Sum(Range("C1").End(xlDown))

But got an error that Sum is not defined as a funtion.

DG


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11
Default Sum a column of data

On Oct 5, 6:37 pm, "DG" wrote:
How do I sum a column of data in a macro?

Assume I have a worksheet and cells C1 through C20 have numbers. How can I
get the sum of those numbers without a loop?

I tried
Total = Sum(Range("C1").End(xlDown))

But got an error that Sum is not defined as a funtion.

DG


Try
Total=WorksheetFunction.Sum(Range("C1",Range("C1") .End(xlDown))

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Sum a column of data

Try

Total = Application.Sum(Range("C1").End(xlDown))


--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"DG" wrote in message
...
How do I sum a column of data in a macro?

Assume I have a worksheet and cells C1 through C20 have numbers. How can I
get the sum of those numbers without a loop?

I tried
Total = Sum(Range("C1").End(xlDown))

But got an error that Sum is not defined as a funtion.

DG



  #4   Report Post  
Posted to microsoft.public.excel.programming
DG DG is offline
external usenet poster
 
Posts: 46
Default Sum a column of data

Sweet.... Works like a charm.

Thanks

DG

wrote in message
oups.com...
On Oct 5, 6:37 pm, "DG" wrote:
How do I sum a column of data in a macro?

Assume I have a worksheet and cells C1 through C20 have numbers. How can
I
get the sum of those numbers without a loop?

I tried
Total = Sum(Range("C1").End(xlDown))

But got an error that Sum is not defined as a funtion.

DG


Try
Total=WorksheetFunction.Sum(Range("C1",Range("C1") .End(xlDown))



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Sum a column of data

Okay, now I have a question for the experts.
What are the pros/cons/differences between:

Total=WorksheetFunction.Sum(...)
and
Total=Application.Sum(...)

Thanks, I'm just trying to learn more about VBA programming.
-pb



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default Sum a column of data

There is no difference.

"cubbybear3" wrote in message
ups.com...
Okay, now I have a question for the experts.
What are the pros/cons/differences between:

Total=WorksheetFunction.Sum(...)
and
Total=Application.Sum(...)

Thanks, I'm just trying to learn more about VBA programming.
-pb



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Sum a column of data

There is no difference.

Not true; there is a difference when it comes to error handling. If you
omit the "WorksheetFunction", you can trap the error in a Variant and test
that with IsError. If you include "WorksheetFunction", you must trap a
run-time error with an On Error statement.

Sub AAA()
Dim V As Variant
V = Application.Sum(Range("A1:A3"))
If IsError(V) = True Then
Debug.Print "ERROR USING SUM"
Else
Debug.Print CStr(V)
End If

On Error Resume Next
Err.Clear
V = Application.WorksheetFunction.Sum(Range("A1:A3"))
If Err.Number < 0 Then
Debug.Print "ERROR USING SUM"
Else
Debug.Print CStr(V)
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Zone" wrote in message
...
There is no difference.

"cubbybear3" wrote in message
ups.com...
Okay, now I have a question for the experts.
What are the pros/cons/differences between:

Total=WorksheetFunction.Sum(...)
and
Total=Application.Sum(...)

Thanks, I'm just trying to learn more about VBA programming.
-pb




  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 373
Default Sum a column of data

I stand corrected!

"Chip Pearson" wrote in message
...
There is no difference.


Not true; there is a difference when it comes to error handling. If you
omit the "WorksheetFunction", you can trap the error in a Variant and test
that with IsError. If you include "WorksheetFunction", you must trap a
run-time error with an On Error statement.

Sub AAA()
Dim V As Variant
V = Application.Sum(Range("A1:A3"))
If IsError(V) = True Then
Debug.Print "ERROR USING SUM"
Else
Debug.Print CStr(V)
End If

On Error Resume Next
Err.Clear
V = Application.WorksheetFunction.Sum(Range("A1:A3"))
If Err.Number < 0 Then
Debug.Print "ERROR USING SUM"
Else
Debug.Print CStr(V)
End If
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Zone" wrote in message
...
There is no difference.

"cubbybear3" wrote in message
ups.com...
Okay, now I have a question for the experts.
What are the pros/cons/differences between:

Total=WorksheetFunction.Sum(...)
and
Total=Application.Sum(...)

Thanks, I'm just trying to learn more about VBA programming.
-pb






  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 40
Default Sum a column of data

Not true; there is a difference when it comes to error handling. If you
omit the "WorksheetFunction", you can trap the error in a Variant and test
that with IsError. If you include "WorksheetFunction", you must trap a
run-time error with an On Error statement.


Sub AAA()
Dim V As Variant
V = Application.Sum(Range("A1:A3"))
If IsError(V) = True Then
Debug.Print "ERROR USING SUM"
Else
Debug.Print CStr(V)
End If


On Error Resume Next
Err.Clear
V = Application.WorksheetFunction.Sum(Range("A1:A3"))
If Err.Number < 0 Then
Debug.Print "ERROR USING SUM"
Else
Debug.Print CStr(V)
End If
End Sub


Thanks Chip. BTW, the newsletters are very educational.
-pb

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 for a column based on the column header and then past data from it to another column in another workbook minkokiss Excel Programming 2 April 5th 07 01:12 AM
Macro to paste data to a new column if previous column has data Neil[_29_] Excel Programming 3 February 13th 07 09:58 PM
Based on a condition in one column, search for a year in another column, and display data from another column in the same row look [email protected] Excel Programming 2 December 30th 06 06:23 PM
Based on a condition in one column, search for a year in another column, and display data from another column in the same row look [email protected] Excel Discussion (Misc queries) 1 December 27th 06 05:47 PM
Matching one column against another column of data to show the same amount of data. dodat Excel Worksheet Functions 0 December 30th 05 06:19 PM


All times are GMT +1. The time now is 07:59 PM.

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"