+ Reply to Thread
Results 1 to 8 of 8

MIN & MAX VBA commands not seeming to work

Hybrid View

  1. #1
    Registered User
    Join Date
    11-11-2009
    Location
    Columbus, Ohio; USA
    MS-Off Ver
    Excel 2002
    Posts
    70

    Wink MIN & MAX VBA commands not seeming to work

    Could someone check out the VBA code here?

    I would like someone to explain the reason why when the program is run, the maximum value in column S successfully is selected by being colored in pink, but the minimum in column R between those pink max's (indicated by the entire row outlined in red) is not the minimum.

    In other words, look at the numbers in column R. Only the 14.08 rows are supposed to be boxed in red. I can't understand why the 30.08 and the 46.08 are boxed in red??? And puzzling to me is why the 46.08 near the top are boxed, yet the 46.08 near the bottom are not boxed.

    Thank you!

    Sub x()
        Dim DstRng As Range
        Dim R As Long
        Dim RngEnd As Range
        Dim rData       As Range
        Dim cell        As Range
        Dim dMin        As Double
    
     Application.ScreenUpdating = False
     Application.Calculation = xlCalculationManual
    
      With Worksheets("INPUT")
       'Set the first row of the destination
        Set DstRng = Range("A11:S11")
       'Find any rows below the first that contain data
        Set RngEnd = .Cells(Rows.Count, DstRng.Column).End(xlUp)
       'Clear the cells
        Set DstRng = IIf(RngEnd.Row < DstRng.Row, DstRng, .Range(DstRng, RngEnd))
        DstRng.Clear
      End With
      
        With Worksheets("Details")
            .UsedRange.Borders.LineStyle = xlNone
            Set rData = .Range("R5", .Range("R604"))
    
            For Each cell In rData
                If cell.Offset(, 1).Value = WorksheetFunction.Max(rData.Offset(, 1)) Then
                    If dMin = 0 Or cell.Value <= dMin Then
                        dMin = cell.Value
                        With Intersect(.UsedRange, cell.EntireRow)
                            .BorderAround LineStyle:=xlContinuous, ColorIndex:=3, Weight:=xlMedium
                            .Copy
                            DstRng.Offset(R, 0).PasteSpecial xlPasteValues
                            R = R + 1
                        End With
                    End If
                End If
            Next cell
        End With
        
    Application.ScreenUpdating = True
    Application.Calculation = xlCalculationAutomatic
        
        Application.CutCopyMode = False
    End Sub
    Attached Files Attached Files
    Last edited by Danexcel; 12-08-2009 at 03:42 PM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: MIN & MAX VBA commands not seeming to work

    Just a quick heads up, it's possible DstRng is incorrect depending on active sheet at run time given you're not fully qualifying, ie I suspect it should be:

    Set DstRng = .Range(A11:S11)
    That said I've not looked at your file... but I suspect the above requires correction nonetheless.

  3. #3
    Registered User
    Join Date
    11-11-2009
    Location
    Columbus, Ohio; USA
    MS-Off Ver
    Excel 2002
    Posts
    70

    Re: MIN & MAX VBA commands not seeming to work

    Quote Originally Posted by DonkeyOte View Post
    Just a quick heads up, it's possible DstRng is incorrect depending on active sheet at run time given you're not fully qualifying, ie I suspect it should be:

    Set DstRng = .Range(A11:S11)
    That said I've not looked at your file... but I suspect the above requires correction nonetheless.

    Thanks for the information.
    I tried replacing the one line in the code with yours, but I got an error with that line as the problem.
    Was there another place I also needed to copy it into?

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: MIN & MAX VBA commands not seeming to work

    For whatever reason I failed to encase the range within quotations...

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: MIN & MAX VBA commands not seeming to work

    OK I've just looked at this properly... try the below (I've tried not to adjust from your existing code unless nec.)

    Sub x()
        Dim DstRng As Range
        Dim R As Long
        Dim RngEnd As Range
        Dim rData       As Range
        Dim cell        As Range
        Dim dMin        As Double
        Dim dMax        As Double
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
        With Worksheets("INPUT")
            'Set the first row of the destination
            Set DstRng = .Range("A11:S11")
            'Find any rows below the first that contain data
            Set RngEnd = .Cells(Rows.Count, DstRng.Column).End(xlUp)
            'Clear the cells
            Set DstRng = IIf(RngEnd.Row < DstRng.Row, DstRng, .Range(DstRng, RngEnd))
            DstRng.Clear
        End With
      
        With Worksheets("Details")
            .UsedRange.Borders.LineStyle = xlNone
            Set rData = .Range("R5:R604")
            dMax = WorksheetFunction.Max(rData.Offset(, 1))
            dMin = .Evaluate("MIN(IF(" & rData.Offset(, 1).Address & "=" & dMax & "," & rData.Address & "))")
            For Each cell In rData
                If cell.Offset(, 1).Value = dMax And cell.Value = dMin Then
                    With Intersect(.UsedRange, cell.EntireRow)
                        .BorderAround LineStyle:=xlContinuous, ColorIndex:=3, Weight:=xlMedium
                        .Copy
                        DstRng.Offset(R, 0).PasteSpecial xlPasteValues
                        R = R + 1
                    End With
                End If
            Next cell
            Set rData = Nothing
        End With
        
        Application.ScreenUpdating = True
        Application.Calculation = xlCalculationAutomatic
        Application.CutCopyMode = False
        
    End Sub
    The key (IMO) is to establish the MIN for the MAX prior to commencing the Loop, this is done via the Evaluate.

  6. #6
    Registered User
    Join Date
    11-11-2009
    Location
    Columbus, Ohio; USA
    MS-Off Ver
    Excel 2002
    Posts
    70

    Re: MIN & MAX VBA commands not seeming to work

    I'm trying your suggested code now. Thank you!

    One thing I'd like to ask is, on the INPUT tab on the worksheet I only want the rows outlined in red from the DETAILS sheet to be listed. It seems now when I run your code nearly 300 lines show up on the INPUT page, when I really want only the actual handful of rows that are outlined in red from the DETAILS page.

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: MIN & MAX VBA commands not seeming to work

    Given the way you're defining DstRng you need to resize it before using it as basis for paste, ie use:

    DstRng.Resize(1).Offset(R).PasteSpecial xlPasteValues

  8. #8
    Registered User
    Join Date
    11-11-2009
    Location
    Columbus, Ohio; USA
    MS-Off Ver
    Excel 2002
    Posts
    70

    Re: MIN & MAX VBA commands not seeming to work

    Hello DonkeyOte,

    I've been busy but I swung back round to working on this. I originally did not know where to add the snippet you sent last, but I believe I figured it out, and I am getting better results. I will mark as solved for now!

    Thank you very much.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1