Solved

ExcelVBA: Find max Value in filtered range

Posted on 2010-11-07
4
1,254 Views
Last Modified: 2012-08-13
What VBA would I need so that it will find a maximum value from a filtered worksheet.
(See Example Below...)

Pretend I have a worksheet with data that looks like below... (Autofilters turned on)

Column     A                 B
Row1:    Names          Age
Row2:    John             20
Row3:    Mat               24
Row4:    John             28
Row5:    Matt              15
Row6:    John              22

Pretend the user autofilters Colum A("Names") to only show rows with "John".
(Only 3 rows will show in this case because there are 3 rows with "John" in Column A.)

When a user presses a button on this worksheet a message will display saying the following:

"The oldest person in the filtered range is 28"

Please submit the fastest code to accomplish this task. (i.e. if the user had 40,000 rows, it could do this task quickly)
0
Comment
Question by:ouestque
  • 2
  • 2
4 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34081398
MsgBox WorksheetFunction.Max(Intersect(UsedRange, Range("B:B").SpecialCells(xlCellTypeVisible)))
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 200 total points
ID: 34081401
Rather,

MsgBox "The oldest person in the filtered range is " & _
WorksheetFunction.Max(Intersect(UsedRange, Range("B:B").SpecialCells(xlCellTypeVisible)))
0
 
LVL 6

Assisted Solution

by:FernandoFernandes
FernandoFernandes earned 300 total points
ID: 34081533
you have to use the SUBTOTAL function...
just right this in any cell out of the filtered area (preferentially above it...)

=SUBTOTAL(4,B:B)
0
 
LVL 6

Assisted Solution

by:FernandoFernandes
FernandoFernandes earned 300 total points
ID: 34081538
if you need it to be part of a macro:

dim strMsg as tring
strMsg = Application.WorksheetFunction.Subtotal(4,Range("B:B"))
msgbox strMsg

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now