Consulting

Results 1 to 9 of 9

Thread: If cells starts with a number and exceeds a certain number of characters

  1. #1
    VBAX Regular
    Joined
    Jun 2018
    Posts
    42
    Location

    If cells starts with a number and exceeds a certain number of characters

    Hi All,

    I'm trying to write a macro to highlight a cell IF

    - the cell starts with a number and the number of characters in the cell exceeds 8

    For example, I would want the followings values in red to be highlighted

    • 10000011
    • 100212549
    • 10020022, 10236598
    • 10856985 & 56987569
    • 12659875 73965413
    • 12665485 - 85698755
    • Orange
    • Red Black Red
    • 102554 Red
    • Orange 10658


    Any help would be much appreciated

  2. #2
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location

    Post

    Try this:

    Option Explicit
    
    Sub cjw_14
    
    Dim rng as range, c as range
    
    Set rng = Selection
    For each c in rng
      If Len(c.value) > 8 and IsNumeric(Left(c.value, 2)) Then
         c.Font.Color = vbRed
      End If
    Next c
    End Sub
    Last edited by MagPower; 03-16-2019 at 10:10 AM.

  3. #3
    VBAX Regular
    Joined
    Jun 2018
    Posts
    42
    Location
    Thanks alot MagPower!, works great

  4. #4
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
    Happy to help!

    -- Russ

  5. #5
    Quote Originally Posted by MagPower View Post
    Try this:

    Option Explicit
    
    Sub cjw_14
    
    Dim rng as range, c as range
    
    Set rng = Selection
    For each c in rng
      If Len(c.value) > 8 and IsNumeric(Left(c.value, 2)) Then
         c.Font.Color = vbRed
      End If
    Next c
    End Sub
    Just out of curiosity, why the left 2 characters and not just the first character on the left as digits (that seemed to be all the OP was restricting it to)?

    Another, simpler way to write your If..Then statement but requiring only the left character to be a digit (which is all I think the OP was restricting the value to)...
    If c.Value Like "#????????*" Then
    Change the quoted pattern to "##???????*" if there is a reason the left two characters must both be digits of this test.

  6. #6
    VBAX Regular
    Joined
    Dec 2018
    Posts
    23
    Location
    @Rick - you are correct, it should only be the first char, not the first two as per OP request. I was tired that day ��

    Russ
    Last edited by MagPower; 03-24-2019 at 08:58 AM.

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I'd add a safety check to make sure a Range was selected, and a way to 'un-red' any cells that were updated and didn't meet the criteria anymore

    Option Explicit
    
    Sub phh_1()
        Dim c As Range
    
        If Not TypeOf Selection Is Range Then Exit Sub
        For Each c In Selection.Cells
             c.Font.ColorIndex = IIf(c.Value Like "#????????*", 3, xlColorIndexNone)    '   # 8?s *
        Next c
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  8. #8
    Quote Originally Posted by Paul_Hossler View Post
    I'd add a safety check to make sure a Range was selected, and a way to 'un-red' any cells that were updated and didn't meet the criteria anymore

    Option Explicit
    
    Sub phh_1()
        Dim c As Range
    
        If Not TypeOf Selection Is Range Then Exit Sub
        For Each c In Selection.Cells
             c.Font.ColorIndex = IIf(c.Value Like "#????????*", 3, xlColorIndexNone)    '   # 8?s *
        Next c
    
    End Sub
    Do you real want the non-red font color to be xlColorIndexNone? I would think it should be 1 (black)?

  9. #9
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Probably should be xlColorIndexAutomatic

    My test case wasn't as robust as it should have been

    Capture.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

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