finding data maximums
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Finding data maximums

Finding data maximums

resolvedResolved · Medium Priority · Version 2003

replyReply Fri 16 Dec 2011, 16:15Delegate Steven said...

Steven has attended:
Excel VBA Intro Intermediate course

Finding data maximums

if i have a list of data with values in column 1 (some of which may be the same number), and the related parameter in column b, how can i extract the maximum value of column b for each value in column a?

e.g.

a b
1 3
2 5
1 6
1 8
2 8
3 1
3 8
1 9

i would need a table producing

a b
1 9
2 8
3 8

For upcoming training course dates see: Pricing & availability

replyReply Sun 18 Dec 2011, 23:32Trainer Anthony said...

RE: finding data maximums

Hi Steven, thanks for your query. There are many ways of doing this, here is a rather rough and ready bit of code to do it.

First of all, you'll need headings on your data to do it this way. I put your data onto a sheet called "mydata" with the headings "A" and "B".

I used the advanced filter to pull out the unique values in column A

I brought across the second heading.

I use a series of loops and conditional test to pull out the largest values.

Finally, I attach the whole thing to a command button on the sheet. Here comes the code:

***************

Option Explicit

Private Sub CommandButton1_Click()

Dim introwcount As Integer
Dim listrowcount As Integer
Dim myvalue As Integer
Dim mystorednumber As Integer

myvalue = 0

Columns("A:A").AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("D1"), Unique:=True

Sheets("mydata").Range("e1") = Sheets("mydata").Range("b1")

For introwcount = 2 To Sheets("mydata").Range("d1").CurrentRegion.Rows.Count

mystorednumber = Sheets("mydata").Cells(introwcount, 4).Value

For listrowcount = 2 To Sheets("mydata").Range("a1").CurrentRegion.Rows.Count

If mystorednumber = Sheets("mydata").Cells(listrowcount, 1).Value Then

If Sheets("mydata").Cells(listrowcount, 2).Value > myvalue Then

myvalue = Sheets("mydata").Cells(listrowcount, 2).Value

End If


End If

Next listrowcount

Sheets("mydata").Cells(introwcount, 5).Value = myvalue

myvalue = 0

Next introwcount

End Sub

***************

Hope this helps,

Anthony

Sun 25 Dec 2011: Automatically marked as resolved.

 

Please browse our web site to find out more about
microsoft.excel.courses.in.london and other Microsoft training courses.

Excel tip:

Select only cell that contain text to lock format

For selecting cells that only contain Text in Excel

By selecting cells that only contain text, you can delete, fill or protect cells of this type.

Use short cut to Go to box (F5) or Edit, Go to
In the dialog box, click special button & select Constants and only check text or any other desired type.

Click OK.

And text cells will all be highlighted for you to apply format. Please note only works on one sheet at a time.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard