?
Solved

Excel VBA - Find Address of a Cell with Largest Value in a Category

Posted on 2011-03-16
3
Medium Priority
?
420 Views
Last Modified: 2012-05-11
Hi Experts! How do I find address of a cell with largest value per category of cells in a range, using VBA? I have a large file and doing it through a formula takes too much RAM, and takes too much time. "=MAX(IF($A$2:$A$33=D2,$B$2:$B$33))", so I want to do it with an efficient macro.

Cheers, N2V
Address-of-Cell-with-Largest-Val.xls
0
Comment
Question by:NewToVBA
[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
3 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 1000 total points
ID: 35152419
Here is a user-defined function for that. Put it in a module and call it from cell e2 as

=getmax(D2,$A$2:$B$33,2)

and f2 as

=getmax(D2,$A$2:$B$33,2,1)

Thomas

Function getMax(strCat As String, rgLookup As Range, lgColumn As Long, Optional blAddress As Boolean = False)
Dim rg As Range, dblValue As Double, rgMax As Range

For Each rg In rgLookup.Columns(1).Cells
    If rg = strCat Then
        If rg.Offset(0, lgColumn - 1) > dblValue Then
            Set rgMax = rg.Offset(0, lgColumn - 1)
            dblValue = rgMax
        End If
    End If
Next

If blAddress = True Then getMax = rgMax.Address Else getMax = rgMax.Value

End Function

Open in new window

0
 

Author Closing Comment

by:NewToVBA
ID: 35153113
Just perfect, exactly what I was looking for.
Many thanks, Thomas, well done!

Cheers, V
0
 
LVL 39

Expert Comment

by:nutsch
ID: 35153140
Glad to help. Thanks for the grade.

Thomas
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 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