Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Summing columns in Excel

Status
Not open for further replies.

RicksAtWork

Programmer
Nov 1, 2005
120
GB
Using the following code I am filtering a worksheet.

Set individuals = UniqueItemCollection(Range("Data_Individuals"))

For Each individual In individuals

Selection.AutoFilter
Selection.AutoFilter Field:=1, Criteria1:=CStr(individual)


//Code here

Next individual

This works very well.

This data now needs to be aggregated i.e. there may be 5 rows for each individual - I want to merge this into one row - without using a pivot table.

I want to use VBA.

How should I approach this - note that some columns are text based i.e. Country = Japan others are numerical i.e. Score =10


 

Hi,

Since you are using the AutoFilter, you can aggregate using the spreadsheet function SUBTOTAL. first argument, 9, sums visible cells.


Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 
Thanks!!

So SumTotal(9,???)

What do I put in the second arguement, I dont know how many rows there are - I only know that I want column 2 to be totalled over the visible range???

 



if your values are in column A...
[tt]
=SUBTOTAL(9,A:A)
[/tt]


Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 
I get a function not defined error - I am using Excel 2003!
 


And how are you using it in your code?


Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 
It seems that Subtotal runs from Selection.SubTotal ...

?

I dont want to loose my reference to the selection - because it takes lots of filtering to get hold of it.

So, using the Subtotal function how do I specify that I want to sum column b in reference to this selection without loosing the selection???

I'm going mad!
 


If you use a spreadsheet function ON A SHEET...
[tt]
=SUBTOTAL(9,A:A)
[/tt]
in VB code...
Code:
MySum = Application.SUBTOTAL(9, MySheet.Range("A:A"))



Skip,

[glasses] [red]Be Advised![/red]
A wee deranged psychic may be runnin' around out there!
SMALL MEDUIM @ LARGE[tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top