Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# ExcelVBA: Find max Value in filtered range

Posted on 2010-11-07
Medium Priority
1,725 Views
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"

0
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
• 2
• 2

LVL 58

Expert Comment

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

LVL 58

Accepted Solution

cyberkiwi earned 800 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

FernandoFernandes earned 1200 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

FernandoFernandes earned 1200 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
``````
0

## Featured Post

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…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
###### Suggested Courses
Course of the Month9 days, 16 hours left to enroll