Min and Max value ignoring blank cells

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hi,
want to get Min and Max value ignoring blank cells,
the code bellow not works,
VBA Code:
Sub Makro5()
'
Dim rng As Range
Dim sht As Worksheet
Dim lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row '

For i = 2 To lastrow
    Set rng = Range("AG" & i & ",AI" & i)
    sht.Range("AP" & i).Value = Application.WorksheetFunction.Min(rng)
    sht.Range("AQ" & i).Value = Application.WorksheetFunction.Max(rng)
Next i

End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
In what way doesn't it work?
 
Upvote 0
In what way doesn't it work?
se bellow

Data Year1Data Year2Data Year3Result MinResult Max
201720182019
2017​
2019​
20212022
2022 (the answer is 2021)
2022​
2022
2022​
2022​
20152018
2018​
2018​
20172020
2017​
2017​
0​
0​
2016
2016​
2016​
20182024
2018​
2024​
20152018
2015​
2015​
0​
0​
 
Upvote 0
Are you only checking columns AG and AI, or do you also want to include column AH?
If you want to include column AH, I believe that you need to change this line:
VBA Code:
    Set rng = Range("AG" & i & ",AI" & i)
to this:
VBA Code:
    Set rng = Range("AG" & i & ":AI" & i)
Subtle difference, but it makes all the difference!
 
Upvote 0
Are you only checking columns AG and AI, or do you also want to include column AH?
If you want to include column AH, I believe that you need to change this line:
VBA Code:
    Set rng = Range("AG" & i & ",AI" & i)
to this:
VBA Code:
    Set rng = Range("AG" & i & ":AI" & i)
Subtle difference, but it makes all the difference!
you are right,
it works,
but if all 3 cells are empty the result is 0 (zero), why? the result must be empty,
 
Upvote 0
That is way that the MIN and MAX functions work because they have to return numbers. If you apply it to an empty range, it will return a 0.
If you do not want it to return anything if the range is empty, then check the range first for entries, i.e.
VBA Code:
Sub Makro5()
'
Dim rng As Range
Dim sht As Worksheet
Dim lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row '

For i = 2 To lastrow
    Set rng = Range("AG" & i & ":AI" & i)
    If Application.WorksheetFunction.Count(rng) > 0 Then
        sht.Range("AP" & i).Value = Application.WorksheetFunction.Min(rng)
        sht.Range("AQ" & i).Value = Application.WorksheetFunction.Max(rng)
    End If
Next i

End Sub
 
Upvote 0
Solution
That is way that the MIN and MAX functions work because they have to return numbers. If you apply it to an empty range, it will return a 0.
If you do not want it to return anything if the range is empty, then check the range first for entries, i.e.
VBA Code:
Sub Makro5()
'
Dim rng As Range
Dim sht As Worksheet
Dim lastrow As Long
Set sht = ThisWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "B").End(xlUp).Row '

For i = 2 To lastrow
    Set rng = Range("AG" & i & ":AI" & i)
    If Application.WorksheetFunction.Count(rng) > 0 Then
        sht.Range("AP" & i).Value = Application.WorksheetFunction.Min(rng)
        sht.Range("AQ" & i).Value = Application.WorksheetFunction.Max(rng)
    End If
Next i

End Sub
Thanks Joe4,
it works perfect ??
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,214,533
Messages
6,120,076
Members
448,943
Latest member
sharmarick

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