+ Reply to Thread
Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    10-06-2009
    Location
    Diemen, The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    89

    Thumbs up Code error due to less values

    Good evening friends,


    The code down below selects the 4 lowest values in Range("A1:Z1") and gives them a red color.

    The problem; if there are only 1,2 or 3 values in Range("A1:Z1") instead of 4 then it errors.

    My query; how should the code be modified so it also works when there are less then 4 values to process?


    Thank you for any help offered!

    Greetings from Roberto, The Netherlands



    Code:
        Dim oneCell As Range
        Dim smallVal4 As Double
        With Sheet1.Range("D1:RD1")
        smallVal4 = Application.Small(.Cells, 4)
        For Each oneCell In .Cells
        If Val(CStr(oneCell.Value)) <= smallVal4 Then oneCell.Font.ColorIndex = 3 
        If oneCell = "" Then oneCell.Font.ColorIndex = 1
        Next oneCell
        End With
    Last edited by roberto1111; 11-08-2009 at 05:30 PM.

  2. #2
    Valued Forum Contributor Norie's Avatar
    Join Date
    02-02-2005
    Posts
    512

    Re: Code Error

    Try this.
    Code:
    Dim oneCell As Range
    Dim smallVal As Double
    
        With ActiveSheet.Range("A1:Z1")
            If Application.CountA(.Cells) >= 4 Then
                smallVal = Application.Small(.Cells, 4)
            Else
                smallVal = Application.Small(.Cells, Application.CountA(.Cells))
            End If
    
            For Each oneCell In .Cells
                If Val(CStr(oneCell.Value)) <= smallVal Then
                    oneCell.Font.ColorIndex = 3
                End If
                If oneCell = "" Then oneCell.Font.ColorIndex = 1
            Next oneCel

  3. #3
    Registered User
    Join Date
    10-06-2009
    Location
    Diemen, The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    89

    Re: Code error due to less values

    Hello Norie,

    Thank you for your help.

    The code still gives an error when there are only 1,2 or 3 values to process.
    I hope you know what the problem is.


  4. #4
    Forum Guru jaslake's Avatar
    Join Date
    02-21-2009
    Location
    mineral city, ohio
    MS-Off Ver
    Excel 2007; Excel 2000
    Posts
    3,551

    Re: Code error due to less values

    Hi roberto1111
    You could possibly wrap the parameter line of code with an error handler like this
    Code:
    On Error Resume Next
    smallVal4 = Application.Small(.Cells, 4)
    On Error GoTo 0
    if you're comfortable with what your code is doing.
    John
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  5. #5
    Forum Guru mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    2,929

    Re: Code error due to less values

    if there are less than 4 values this will set smallVal4 to the largest of those few values.
    Code:
    With Sheet1.Range("D1:RD1")
        .Offset(0, .Columns.Count).Resize(1, 4).Value = Application.Max(.Cells)
        With .Resize(.Rows.Count, .Columns.Count + 4)
            smallVal4 = Application.Small(.Cells, 4)
        End With
        .Offset(0, .Columns.Count).Resize(1, 4).ClearContents
        MsgBox smallVal4
    End With
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  6. #6
    Valued Forum Contributor Norie's Avatar
    Join Date
    02-02-2005
    Posts
    512

    Re: Code error due to less values

    Well the code I posted worked for me - no errors and the relevant data was formatted.

    Could you please tell us what error you are getting in the first place?

    And an explanation of what you are trying to achieve wouldn't go amiss either.

  7. #7
    Registered User
    Join Date
    10-06-2009
    Location
    Diemen, The Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    89

    Re: Code error due to less values

    Norie, MickRickson and Jaslake thank you for helping me out!

    Greetings Roberto

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.2.0