Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel 2k VBA problem - For loops interating a sort

Running MS Office 2000, SP3 on NT 4 SP6

Goal: Given a workbook in which worksheet 2, "Data" has the form:
H T U
Account Name Jan 04 Feb 04
account name1 #s for jan #s for feb
account name2 ...
account name1
account name4
...

this does the full year so it goes to AE.

I'm trying to have a macro calculate the top 10 and top 20 for 'accoun
name1' per month and store them in worksheet 3, "Derived" in whic
account name1's top 10 for column T go into B8 and the top 20 into b9
then the top 10/20 for column U go into c8,c9 &c.

I can sort by account name1 and one of the columns but it would hurt m
to use copy and paste 12 times instead of a for loop! I'm brand new t
VBA (started yesterday, got this far from reading previous posts o
this forum) and really appreciate your help!

Thanks in advance,

Rob

Code:
Sub TopTenTwenty()
i = 2
For j = 20 To 31
ActiveWorkbook.Sheets("Data").Activate
Sheets("Data").Range("A:AE").Sort _
Key1:=Worksheets("Data").Columns("H"), _
Order1:=xlAscending, _
Key2:=Worksheets("Data").Columns(j), _
Order2:=x1Descending, _
Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

'Takes top ten/twenty
ActiveWorkbook.Sheets("Derived").Activate
ActiveSheet.Cells(8, i).Value
WorksheetFunction.Sum(Worksheets("Data").Range(Cel ls(2, j), Cells(11
j)))
ActiveSheet.Cells(9, i).Value
WorksheetFunction.Sum(Worksheets("Data").Range(Cel ls(2, j), Cells(21
j)))
i = i + 1
Next j
End Su

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 136
Default Excel 2k VBA problem - For loops interating a sort

Look up the LARGE function in XL help.

robert_m_mills < wrote:
Running MS Office 2000, SP3 on NT 4 SP6

Goal: Given a workbook in which worksheet 2, "Data" has the form:
H T U
Account Name Jan 04 Feb 04
account name1 #s for jan #s for feb
account name2 ...
account name1
account name4
..

this does the full year so it goes to AE.

I'm trying to have a macro calculate the top 10 and top 20 for 'account
name1' per month and store them in worksheet 3, "Derived" in which
account name1's top 10 for column T go into B8 and the top 20 into b9,
then the top 10/20 for column U go into c8,c9 &c.

I can sort by account name1 and one of the columns but it would hurt me
to use copy and paste 12 times instead of a for loop! I'm brand new to
VBA (started yesterday, got this far from reading previous posts on
this forum) and really appreciate your help!

Thanks in advance,

Rob

Code:
Sub TopTenTwenty()
i = 2
For j = 20 To 31
ActiveWorkbook.Sheets("Data").Activate
Sheets("Data").Range("A:AE").Sort _
Key1:=Worksheets("Data").Columns("H"), _
Order1:=xlAscending, _
Key2:=Worksheets("Data").Columns(j), _
Order2:=x1Descending, _
Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

'Takes top ten/twenty
ActiveWorkbook.Sheets("Derived").Activate
ActiveSheet.Cells(8, i).Value =
WorksheetFunction.Sum(Worksheets("Data").Range(Cel ls(2, j), Cells(11,
j)))
ActiveSheet.Cells(9, i).Value =
WorksheetFunction.Sum(Worksheets("Data").Range(Cel ls(2, j), Cells(21,
j)))
i = i + 1
Next j
End Sub


---
Message posted from http://www.ExcelForum.com/

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Excel 2k VBA problem - For loops interating a sort

wouldn't that mean reading each column into the array and then callin
large 20 times while adding the value to an accumulator and writing i
@ 10 values and 20 values

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 136
Default Excel 2k VBA problem - For loops interating a sort

yeah it would. It looks like it would be better to use Pivot Tables
instead of VBA.

robert_m_mills < wrote:

wouldn't that mean reading each column into the array and then calling
large 20 times while adding the value to an accumulator and writing it
@ 10 values and 20 values?


---
Message posted from http://www.ExcelForum.com/

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
excel 2007 sort problem cindy Excel Worksheet Functions 2 October 3rd 07 01:01 PM
excel sort problem Daniel Excel Worksheet Functions 5 April 28th 07 09:34 PM
if function in excel should 12 loops Madhukar Excel Discussion (Misc queries) 2 September 7th 06 01:30 PM
Excel 2000 Slow Loops scain2004 Excel Programming 7 April 4th 04 02:35 AM
excel 97: if then and for next loops help JMCN Excel Programming 2 November 10th 03 02:24 PM


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