Trying to get cell address of min value with no luck using vba

vba317

Board Regular
Joined
Oct 7, 2015
Messages
58
I am trying to get the cell address of the min value in a range I am getting a Run-time error 424 Object required. The error is highlighted in yellow at the Set minCell line.

Any help is appreciated.



Code:
Sub LowHighUpdate1()


    Dim ws As Worksheet
    Dim wrksh As Excel.Worksheet
    Dim R1 As Long
    Dim LR As Long
    Dim minRng As Range
    Dim maxRange As Range
    Dim minCell As Range
    Dim maxCell As Range
    Dim lMin As Long
    Dim lMax As Long
    
    For Each wrksh In Sheets
        Select Case wrksh.Name
            Case "Parameters"
                GoTo Skip
            Case Else
                wrksh.Select
                    LR = wrksh.Range("A" & Rows.Count).End(xlUp).Row
                        For R1 = 7 To LR
                            Set minRng = Range("D" & R1 & ":D" & (R1 - 5))
                            lMin = Application.WorksheetFunction.Min(minRng)
               [YELLOW]  Set minCell = minRange.Find(minRng)   [/YELLOW]
                            Range(minCell).Interior.Color = vbGreen
                            'B leg High header
                            Set maxRange = Range("C" & R1 & ":C" & (R1 - 5))
                            lMax = Application.WorksheetFunction.Max(maxRng)
                            Range(maxCell).Interior.Color = vbRed
                        Next R1
                Columns("G:Z").NumberFormat = "0.00"
                wrksh.Columns("A:Z").EntireColumn.AutoFit
        End Select
Skip:
    Next wrksh
    Sheets("Parameters").Select
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Change:

Code:
Set minCell = minRange.Find(minRng)

to

Code:
Set minCell = minRange.Find(lMin)
 
Upvote 0
Try

Code:
Sub LowHighUpdate1()
    Dim ws As Worksheet
    Dim wrksh As Excel.Worksheet
    Dim R1 As Long
    Dim LR As Long
  [COLOR=#ff0000]  Dim minRange As Range[/COLOR]
    Dim maxRange As Range
    Dim minCell As Range
    Dim maxCell As Range
    Dim lMin As Long
    Dim lMax As Long
    
    For Each wrksh In Sheets
        Select Case wrksh.Name
            Case "Parameters"
                GoTo Skip
            Case Else
                wrksh.Select
                    LR = wrksh.Range("A" & Rows.Count).End(xlUp).Row
                        For R1 = 7 To LR
                            Set minRange = Range("D" & R1 & ":D" & (R1 - 5))
                            lMin = Application.WorksheetFunction.Min(minRange)
[COLOR=#ff0000]                            Set minCell = minRange.Find(lMin)[/COLOR]
[COLOR=#ff0000]                            minCell.Interior.Color = vbGreen[/COLOR]
                            Set maxRange = Range("C" & R1 & ":C" & (R1 - 5))
                            lMax = Application.WorksheetFunction.Max(maxRange)
[COLOR=#ff0000]                            Set maxCell = maxRange.Find(lMax)[/COLOR]
[COLOR=#ff0000]                            maxCell.Interior.Color = vbRed[/COLOR]
                        Next R1
                Columns("G:Z").NumberFormat = "0.00"
                wrksh.Columns("A:Z").EntireColumn.AutoFit
        End Select
Skip:
    Next wrksh
    Sheets("Parameters").Select
End Sub

Hope this helps

M.
 
Last edited:
Upvote 0
I have started running the following code and I am getting a run-time error 91 Object variable with block not set. This doesn't make sense to me because I am setting the variable. The code errors on the maxCell.Interior.Color = vbRed line. When I look at the lines before it
lMax found the correct max value for that range. But maxcell says it is set to nothing. Any help would be appreciated.

Code:
Sub LowHighUpdate1()


   Dim ws As Worksheet
    Dim wrksh As Excel.Worksheet
    Dim R1 As Long
    Dim LR As Long
    Dim minRange As Range
    Dim maxRange As Range
    Dim minCell As Range
    Dim maxCell As Range
    Dim lMin As Long
    Dim lMax As Long
    
    For Each wrksh In Sheets
        Select Case wrksh.Name
            Case "Parameters"
                GoTo Skip
            Case Else
                wrksh.Select
                    LR = wrksh.Range("A" & Rows.Count).End(xlUp).Row
                        For R1 = 8 To LR
                            Set minRange = Range("D" & (R1 - 5) & ":D" & R1)
                            lMin = Application.WorksheetFunction.Min(minRange)
                            Set minCell = minRange.Find(lMin)
                            minCell.Interior.Color = vbGreen
                            Set maxRange = Range("C" & (R1 - 5) & ":C" & R1)
                            lMax = Application.WorksheetFunction.Max(maxRange)
                            Set maxCell = maxRange.Find(lMax)
                            maxCell.Interior.Color = vbRed
                        Next R1
                Columns("G:Z").NumberFormat = "0.00"
                wrksh.Columns("A:Z").EntireColumn.AutoFit
        End Select
Skip:
    Next wrksh
    Sheets("Parameters").Select
 
 End Sub
 
Upvote 0
I figured out the problem. I had to change lMin and lmax to variant. After that everything worked out great.
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,763
Members
449,120
Latest member
Aa2

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