vba - find max values

Ben_UK

Board Regular
Joined
Feb 19, 2003
Messages
206
Hiya Board

vba newbie again!

In Column A I have a random range of numerical data (5000 - 7000 entries)
I have the following code to highlight the row location of the max value.
--------------
Sub FindMax()

MaxVal = Application.WorksheetFunction. _
Max(Range("A:A"))
For Row = 1 To 65536
If Range("A1").Offset(Row - 1, 0).Value = MaxVal Then
Range("A1").Offset(Row - 1, 0).Activate
MsgBox "The max value can be found in row " & Row
Exit For
End If
Next Row

End Sub

------------

Occasionally up to 5 rows may contain the max figure,
How do I highlight all those row locations?

Once again, Any help would be greatly appreciated

Kind Regards

Ben.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Not sure if you want only the cell(s) containing the maximum value highlighted, or the entire row. The macro below highlights just the cell, but can be easily modified by an .EntireRow script. Modify for desired color; this example is for yellow.

Sub ShadeMax()
Application.ScreenUpdating = False
Dim MaxValRange, MaxVal
Set MaxValRange = Range("A:A")
MaxVal = Application.WorksheetFunction.Max(MaxValRange)
MaxValRange.Interior.ColorIndex = 0
With MaxValRange
Dim C, BegAdd
Set C = .Find(MaxVal, LookIn:=xlValues)
If Not C Is Nothing Then
BegAdd = C.Address
Do
C.Interior.ColorIndex = 6
Set C = .FindNext(C)
Loop While Not C Is Nothing And C.Address <> BegAdd
End If
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
max values

Hiya Tom,

Many apologies for not responding quickly,

Your code fits my requirements perfectly,

Thanks for your time and effort

Kind Regards

Ben
 
Upvote 0
Sub ShadeMax()
Application.ScreenUpdating = False
Dim MaxValRange, MaxVal
Set MaxValRange = Range("A:A")
MaxVal = Application.WorksheetFunction.Max(MaxValRange)
MaxValRange.Interior.ColorIndex = 0
With MaxValRange
Dim C, BegAdd
Set C = .Find(MaxVal, LookIn:=xlValues)
If Not C Is Nothing Then
BegAdd = C.Address
Do
C.Interior.ColorIndex = 6
Set C = .FindNext(C)
Loop While Not C Is Nothing And C.Address <> BegAdd
End If
End With
Application.ScreenUpdating = True
End Sub

Why you didn't end your macro after the line:
If Not C Is Nothing Then
?

Why you continued with 'BegAdd = C.Address ..." etc?
If Not C Is Nothing Then
C.interior.color = 'something'
exit sub
end if

I simple wonder...
 
Upvote 0

Forum statistics

Threads
1,215,172
Messages
6,123,428
Members
449,099
Latest member
COOT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top