Consulting

Results 1 to 17 of 17

Thread: Compare comma separated numbers

  1. #1
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location

    Compare comma separated numbers

    I got two columns, Low and High, with values like this:

    LowHigh.jpg

    I have a code that is to fill the low cell with red color if Low.Value > High.Value.

    If .Cells(i, ColLow).Value > .Cells(i, ColHigh).Value Then
    .Cells(i, ColLow).Interior.Color = vbRed
    End If


    The problem is with the comma separated values.
    If I have ex.
    Low.Value is 3,2 and High.Value is 16.
    It seems like the code is reading Low.Value 32 and High.Value 16, and executing wrong.

    Can anyone help on this?
    Thanks.

  2. #2
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    It would appear that your cells contain text, not true numbers. Try using:
    If CDbl(.Cells(i, ColLow).Value) > CDbl(.Cells(i, ColHigh).Value) Then
    assuming your regional settings use commas as decimal point.
    Be as you wish to seem

  3. #3
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    I get an error message "Type mismatch" when using CBbl. And yes, cells contain text.
    do you know any other ways?

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Do your regional settings use a comma as a decimal point?
    Be as you wish to seem

  5. #5
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    Where can I find those settings?

    When I hold the mouse pointer over the cell I get this message:
    "the number in this cell is formatted as text or preceded by an apostrophe"

    And Format setting --> Number Category --> General format

  6. #6
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    Regional settings is European --> Norwegian

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    If you were to enter 12 and a half in a cell, would you enter 12,5 or 12.5?
    Be as you wish to seem

  8. #8
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    I would enter 12,5

  9. #9
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That's odd then since CDbl should use your regional settings.
    if you change the code to this, does it help:
    If CDbl(Trim(.Cells(i, ColLow).Value)) > CDbl(Trim(.Cells(i, ColHigh).Value)) Then
    Be as you wish to seem

  10. #10
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    No, I get the same error message... "Type mismatch"

  11. #11

  12. #12
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    To make the values a number when single quotes were used, use a method like:
    Range("A2:A5").Value = Range("A2:A5").Value

  13. #13
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    That's odd - it works fine for me if I change my settings to Norwegian.

    Just for debugging, can you try using:
    If Not IsNumeric(.Cells(i, ColLow).Value) Then    MsgBox .Cells(i, ColLow).Value & " is not a number"
    ElseIf Not IsNumeric(.Cells(i, ColHigh).Value) Then
        MsgBox .Cells(i, ColHigh).Value & " is not a number"
    ElseIf CDbl(.Cells(i, ColLow).Value) > CDbl(.Cells(i, ColHigh).Value) Then
        .Cells(i, ColLow).Interior.Color = vbRed
    End If
    and tell me what the message says?
    Be as you wish to seem

  14. #14
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Quote Originally Posted by sindrefm View Post
    Where can I find those settings?

    When I hold the mouse pointer over the cell I get this message:
    "the number in this cell is formatted as text or preceded by an apostrophe"

    And Format setting --> Number Category --> General format
    Format Cells >> Number >> Category >> Special >> Locale (Location) >> Norwegian (Bokmal or Nynorsk)
    Then go to
    Number >> Category >> Number and adjust appearance to your desire.

    That should correct this workbook.

    Then go to the computers Control Panel >> Regional and Language Options

    Then go to Excel Menu >> Tools >> Options >> International.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  15. #15
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    SamT, I tried the debugging and I got the message box " is not a number".

  16. #16
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    OK - that makes sense; you're processing blank cells. You can add a check like this:
    If IsNumeric(.Cells(i, ColLow).Value) And IsNumeric(.Cells(i, ColHigh).Value) Then    If CDbl(.Cells(i, ColLow).Value) > CDbl(.Cells(i, ColHigh).Value) Then .Cells(i, ColLow).Interior.Color = vbRed
    End If
    Be as you wish to seem

  17. #17
    VBAX Regular
    Joined
    Nov 2014
    Posts
    47
    Location
    Awesome!
    Thank you very much for the help

Posting Permissions

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