Minimum search which crashes when there are no values

Thanks Thanks:  0
Likes Likes:  0
Results 1 to 4 of 4

Thread: Minimum search which crashes when there are no values

  1. #1
    New Member
    Join Date
    Oct 2002
    Location
    East Yorkshire England
    Posts
    36
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    Here is code which finds minimum values in certain columns, it works great,except when there are no values in some columns, can anyone suggest any modifications that would help.
    Regards
    Simmo

    ' Sub FormatMinimum()
    Range("C4:C120,E4:E120,G4:G120,I4:I120,K4:K120,L4:L120").Interior.ColorIndex = 0

    Dim MinRange1 As Range, MinRange2 As Range, MinRange3 As Range, MinRange4 As Range, MaxRange5 As Range, MaxRange6 As Range

    Dim MinVal1 As Long, MinVal2 As Long, MinVal3 As Long, MinVal4 As Long, MaxVal5 As Long, MaxVal6 As Long

    Set MinRange1 = Range("N4:N120")
    Set MinRange2 = Range("O4:O120")
    Set MinRange3 = Range("P4:P120")
    Set MinRange4 = Range("Q4:Q120")
    Set MaxRange5 = Range("K4:K120")
    Set MaxRange6 = Range("L4:L120")

    MinVal1 = Application.WorksheetFunction.Min(MinRange1)
    MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False).Offset(0, -11).Interior.ColorIndex = 44

    MinVal2 = Application.WorksheetFunction.Min(MinRange2)
    MinRange2.Find(What:=MinVal2, After:=Range("O4"), LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False).Offset(0, -10).Interior.ColorIndex = 44

    MinVal3 = Application.WorksheetFunction.Min(MinRange3)
    MinRange3.Find(What:=MinVal3, After:=Range("P4"), LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _
    MatchCase:=False).Offset(0, -9).Interior.ColorIndex = 44

  2. #2
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Something like this:

    Code:
    Dim x As Range
    
    MinVal1 = Application.WorksheetFunction.Min(MinRange1) 
    Set x = MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ 
    MatchCase:=False)
    If Not x Is Nothing Then
       x.Offset(0, -11).Interior.ColorIndex = 44
    End If

  3. #3
    New Member
    Join Date
    Oct 2002
    Location
    East Yorkshire England
    Posts
    36
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    Thanks for that piece of code Andrew, do i replace my "MinVal1" statement with the one you have written ? and then replace / edit MinVal2,MinVal3 and MinVal4. I dont mean to sound dumb, but I have lots to learn.

  4. #4
    MrExcel MVP
    Moderator
    Andrew Poulsom's Avatar
    Join Date
    Jul 2002
    Posts
    73,092
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    Replace:

    Code:
    MinVal1 = Application.WorksheetFunction.Min(MinRange1) 
    MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ 
    MatchCase:=False).Offset(0, -11).Interior.ColorIndex = 44
    with

    Code:
    MinVal1 = Application.WorksheetFunction.Min(MinRange1) 
    Set x = MinRange1.Find(What:=MinVal1, After:=Range("N4"), LookIn:=xlValues, _ 
    LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, _ 
    MatchCase:=False)
    If Not x Is Nothing Then
       x.Offset(0, -11).Interior.ColorIndex = 44
    End If
    and repeat for the other two blocks, changing variable and range references as necessary.

    Dim x As Range

    goes in once at the top.

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com