tags:

views:

524

answers:

1

Hi, I'm trying to sum up a column in Excel VBA , however because there are blank spaces, I'm getting 0 as the sum.

Dim rExternalTotal As Range , dExternalTotal as Double
Set rExternalTotal = Range(rReportData.Offset(0, 0), rReportData.Offset(261, 0).End(xlUp))
dExternalTotal = Application.WorksheetFunction.Sum(rExternalTotal)

dExternalTotal is always zero. I cannot delete the blank spaces on the work sheet. Can anyone suggest a way to do the sum programatically?

+1  A: 

I think you are misinterpreting the source of the error; rExternalTotal appears to be equal to a single cell. rReportData.offset(0,0) is equal to rReportData
rReportData.offset(261,0).end(xlUp) is likely also equal to rReportData, as you offset by 261 rows and then use the .end(xlUp) function which selects the top of a contiguous data range.
If you are interested in the sum of just a column, you can just refer to the whole column:

dExternalTotal = Application.WorksheetFunction.Sum(columns("A:A"))

or

dExternalTotal = Application.WorksheetFunction.Sum(columns((rReportData.column))

The worksheet function sum will correctly ignore blank spaces.

Let me know if this helps!

goggin13
Thanks for replying, however it's stil giving me zero as the sum total.
Kojof
This one line of code, without any of the above works for me (with an additional line of code to initialize rReportData), I tested in on various columns; are you initializing rReportData? Can you post the full code, including where you display dExternalTotal?
goggin13
sorry the code is too long to send. But rReportData is just a normal range. The data looks like this: COLUMN C <BR> 7,000,000.00 7,130,000.00 5,165,000.00 3,360,000.00 2,030,000.00 3,420,000.00 5,170,000.00 1,650,000.00
Kojof
I'm not quite sure what the issue is; this runs fine for me. Are you sure that there isn't a typo when you are referencing dExternalTotal the second time (or wherever you display it)? Mistyping the variable name could result in VBA creating a new variable which would default to 0. Otherwise I'm somewhat at a loss. Does this code dExternalTotal = Application.WorksheetFunction.Sum(columns("A:A"))give you the correct sum from column A?
goggin13
No it doesn't. The first 2 cells are blank and so are several cells in that column. I'm doing excactly the same thing for 3 other ranges and it works fine. i will retest your code again.
Kojof