Check if all cells between first Min.value and last Min.value in range, are equal.

soidog

New Member
Joined
May 26, 2016
Messages
45
Hi all,
I have a problem defining the range between first and last Min.value i range("B189:B288").

Code:
Sub FindMidVal()


Dim FRowMin     As Double
Dim LRowMin     As Double
Dim AVal        As Double
Dim SearchRange As range
Dim MinValue    As Double
Dim MinRow      As Integer


'define the search range, in this case it is range("B189:B288")
Set SearchRange = range("B189:B288")
'--------------------------------------------------------------------------------------------------------------
'Check if all cells in range have the same value
If WorksheetFunction.CountIf(range("B189:B288"), range("B189")) _
    = WorksheetFunction.CountA(range("B189:B288")) Then
        MsgBox "All values are equal!"
        Exit Sub
End If
'--------------------------------------------------------------------------------------------------------------
'get the Minimum value in the range
MinValue = Application.Min(SearchRange)
'--------------------------------------------------------------------------------------------------------------
'determine where first MinRow is located (if you want all occurances, use VBA find)
MinRow = Application.Match(MinValue, SearchRange, 0)
'--------------------------------------------------------------------------------------------------------------
'One or more "Min.Value"
If Application.CountIf(SearchRange, MinValue) = 1 Then
    range("B184") = SearchRange(MinRow, 0).Value    'This is an offset to col."A"
Else
    'First occurance of "Min.Value" in range
    FRowMin = range("B188:B288").Find(what:=MinValue, after:=range("B188")).Offset(, -1)
    'Last occurance of "Min.Value" in range
    LRowMin = range("B188:B288").Find(what:=MinValue, after:=range("B188"), searchdirection:=xlPrevious).Offset(, -1)


        'Get average value from Col."A"
        AVal = (FRowMin + LRowMin) / 2
        range("B184") = AVal
End If
'--------------------------------------------------------------------------------------------------------------


Dim LMinRow
LMinRow = Columns("B").Cells.Find(MinValue, searchorder:=xlByRows, searchdirection:=xlPrevious).Row


If "all cells between first Min.value and last Min.value in range, are not equal" Then
    MsgBox "All values in Min.value range are NOT equal!"
    Exit Sub
End If
End Sub

I hope this question makes sense. If not, please ask for clarification.
Any help will be greatly appreciated.
Thanks!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
An example:


Each cell in range("B202:B210") have value 5.
Range("B216") have value 5.
All other cells in range("B189:B288") have value 10.


First Min.value and last Min.value are dynamic.
How to define range (in this example range = ("B202:B216")), with first Min.value and last Min.value?


If "all cells between first Min.value and last Min.value, are not equal" Then
MsgBox "All values in Min.value range are NOT equal!"
Exit Sub
End If


Thanks in advance.
 
Upvote 0
'It works hard coded (with my example above) and it looks like this:


Code:
If WorksheetFunction.CountIf(range("B202:B216"), range("B202")) _
    = WorksheetFunction.CountA(range("B202:B216")) Then
Else
    MsgBox "All values in Min.value range are NOT equal!"
    Exit Sub
End If
But that isn't going to work because Min.values are dynamic.
Any help would be greatly appreciated.
 
Upvote 0

Forum statistics

Threads
1,216,474
Messages
6,130,841
Members
449,598
Latest member
sunny_ksy

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