Solved

ExcelVBA: Find max Value in filtered range

Posted on 2010-11-07
4
1,611 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

615 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