Max value in a range

Krb00

New Member
Joined
Jan 21, 2023
Messages
8
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi,
in the attached file I'd like the code to find the max value in a range and highlight it. I'd also like to highlight only the first occurrence, the value in B107.
What's wrong with the code?

Hope someone can help me with this
Thank you

VBA Code:
Sub test()
Dim rngmax As Range
Dim maxT As Double
Dim maxrow As Integer
Set rngmax = Sheets(1).Range("B107:B150")
maxT = Round(Application.WorksheetFunction.Max(rngmax), 2)
maxrow = rngmax.Find(maxT, , xlValues).Row
Sheets(1).Range("B" & maxrow).Interior.ColorIndex = 45
End Sub
 

Attachments

  • img.PNG
    img.PNG
    38.9 KB · Views: 6

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If you only have one sheet, then, Try this:

VBA Code:
Sub test()
  Dim rngmax As Range
  Dim maxT As Double
  Dim maxrow As Integer
  Set rngmax = Sheets(1).Range("B107:B150")
  maxT = WorksheetFunction.Max(rngmax)
  maxrow = rngmax.Find(maxT, , xlValues, xlWhole).Row
  Sheets(1).Range("B" & maxrow).Interior.ColorIndex = 45
End Sub
 
Upvote 0
Unfortunately, it keeps highlighting the second occurrence in B108.
 
Upvote 0
I think Conditional Formatting would be an easier solution.
Book1
CD
1Date/TimeValue
21/1/23 10:00:00121.76
31/1/23 10:00:30121.92
41/1/23 10:01:00121.37
51/1/23 10:01:30121.60
61/1/23 10:02:00121.14
71/1/23 10:02:30121.40
81/1/23 10:03:00121.59
91/1/23 10:03:30121.04
101/1/23 10:04:00121.76
111/1/23 10:04:30121.69
121/1/23 10:05:00121.43
131/1/23 10:05:30121.62
141/1/23 10:06:00121.48
151/1/23 10:06:30121.09
161/1/23 10:07:00121.50
171/1/23 10:07:30121.66
181/1/23 10:08:00121.24
191/1/23 10:08:30121.14
201/1/23 10:09:00121.37
211/1/23 10:09:30121.06
221/1/23 10:10:00121.44
231/1/23 10:10:30121.94
241/1/23 10:11:00121.95
251/1/23 10:11:30121.97
261/1/23 10:12:00121.90
271/1/23 10:12:30121.58
281/1/23 10:13:00121.40
291/1/23 10:13:30121.37
301/1/23 10:14:00121.19
311/1/23 10:14:30121.23
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
C2:D31Expression=$D2=MAX($D$2:$D$31)textNO
Applies to:
Excel Formula:
=$C$2:$D$31
If you turned it into a table, the Conditional Formatting would grow as the table grows.
 
Upvote 0
Yes but I need it in vba, it's just a part of a larger code.

The code works fine only if I change the range to "B106:B150" but I can't figure out why.
 
Upvote 0
Unfortunately, it keeps highlighting the second occurrence in B108.
Try this. Add this parameter:

Rich (BB code):
Sub test()
  Dim rngmax As Range
  Dim maxT As Double
  Dim maxrow As Integer
  Set rngmax = Sheets(1).Range("B107:B150")
  maxT = WorksheetFunction.Max(rngmax)
  maxrow = rngmax.Find(maxT, rngmax(rngmax.Rows.Count), xlValues, xlWhole).Row
  Sheets(1).Range("B" & maxrow).Interior.ColorIndex = 45
End Sub

The code works fine only if I change the range to "B106:B150" but I can't figure out why.
The Find method starts searching from the second cell of the range, it is a bug of the method, that is why it does not find the value in the first cell. And that's also why he finds the second cell. If no other equal value exists, then let's say the find method loops through all cells and returns to the first cell in the range (B107) then finds it and marks it.

And that's also why, when you changed the range to B106, then it starts searching in the second cell, that is, B107, then it finds it.
Another option is to tell the Find method to start at the last cell, this will make the find method search the second cell, that is, the first cell in the range.
 
Last edited:
Upvote 0
Solution
Yes but I need it in vba, it's just a part of a larger code.

The code works fine only if I change the range to "B106:B150" but I can't figure out why.
When you copy the sample, you need to paste it into Cell C1, not A1. I make that mistake all the time, and hate when samples don't start at A1. Don't know why I did that.
You can change the ranges to cover the entire column, so that the Conditional Formatting Formula would be
Excel Formula:
=$B2=MAX($D:$D)
and the Applies To formula is
Excel Formula:
=$C:$D
Don't understand what would force the need for VBA.
 
Upvote 0
Try this. Add this parameter:

Rich (BB code):
Sub test()
  Dim rngmax As Range
  Dim maxT As Double
  Dim maxrow As Integer
  Set rngmax = Sheets(1).Range("B107:B150")
  maxT = WorksheetFunction.Max(rngmax)
  maxrow = rngmax.Find(maxT, rngmax(rngmax.Rows.Count), xlValues, xlWhole).Row
  Sheets(1).Range("B" & maxrow).Interior.ColorIndex = 45
End Sub


The Find method starts searching from the second cell of the range, it is a bug of the method, that is why it does not find the value in the first cell. And that's also why he finds the second cell. If no other equal value exists, then let's say the find method loops through all cells and returns to the first cell in the range (B107) then finds it and marks it.

And that's also why, when you changed the range to B106, then it starts searching in the second cell, that is, B107, then it finds it.
Another option is to tell the Find method to start at the last cell, this will make the find method search the second cell, that is, the first cell in the range.
Now it works! Thank you!
I appreciated the explanation, I didn't know that.
 
Upvote 1

Forum statistics

Threads
1,215,547
Messages
6,125,461
Members
449,228
Latest member
moaz_cma

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