Advice for adding line of code to existing code please
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Advice for adding line of code to existing code please

  1. #1
    Board Regular
    Join Date
    Nov 2010
    Posts
    2,379
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Default Advice for adding line of code to existing code please

    Hi,

    I am using the code below which works fine.

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)  Dim c As Range
      On Error GoTo AllowEvents
      If Target.Count > 1000 Then Exit Sub
      If Not Intersect(Target, Range("B:B")) Is Nothing Then
        For Each c In Target
          If c.Row > 5 And c.Column = 2 Then
              If Len(c.Value) <> 17 And Len(c.Value) > 0 Then
              Application.EnableEvents = False
              MsgBox "VIN MUST BE 17 CHARACTERS", vbCritical, "VIN CHARACTER COUNT MESSAGE"
              c.Value = ""
              c.Select
            End If
          End If
        Next
      End If
    AllowEvents:
      Application.EnableEvents = True
    
    
    End Sub

    What i am having an issue with now is trying to add something like the below into the code above.

    Code:
    Range("B").Characters(Start:=10, Length:=1).Font.ColorIndex = 3
    The code needs to work for any cell in column B so when a value is typed into the cell the 10th character will be shown in a Red font colour.
    I have learning difficulties so please be patient if i'm slow on the uptake,Thanks Very Much...

  2. #2
    Board Regular Akuini's Avatar
    Join Date
    Feb 2016
    Location
    Indonesia
    Posts
    1,961
    Post Thanks / Like
    Mentioned
    33 Post(s)
    Tagged
    4 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Maybe this:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
      On Error GoTo AllowEvents
      If Target.Count > 1000 Then Exit Sub
      If Not Intersect(Target, Range("B:B")) Is Nothing Then
        For Each c In Target
          If c.Row > 5 And c.Column = 2 Then
              If Len(c.Value) <> 17 And Len(c.Value) > 0 Then
              Application.EnableEvents = False
              MsgBox "VIN MUST BE 17 CHARACTERS", vbCritical, "VIN CHARACTER COUNT MESSAGE"
              c.Value = ""
              c.Select
              Else
                    c.Characters(Start:=10, Length:=1).Font.ColorIndex = 3
                    
             End If
          End If
        Next
      End If
    AllowEvents:
      Application.EnableEvents = True
    
    
    End Sub

  3. #3
    Board Regular
    Join Date
    Nov 2010
    Posts
    2,379
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Thanks but that made no difference
    I have learning difficulties so please be patient if i'm slow on the uptake,Thanks Very Much...

  4. #4
    Board Regular
    Join Date
    Mar 2015
    Location
    Syria
    Posts
    143
    Post Thanks / Like
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Quote Originally Posted by ipbr21054 View Post
    Thanks but that made no difference
    It Dose work perfectly

  5. #5
    Board Regular
    Join Date
    Nov 2010
    Posts
    2,379
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Hi,
    Strange.

    Here is my worksheet if maybe you could see the difference.

    http://www.mediafire.com/file/mhevof...EST2.xlsm/file
    Last edited by ipbr21054; Aug 25th, 2019 at 08:00 AM.
    I have learning difficulties so please be patient if i'm slow on the uptake,Thanks Very Much...

  6. #6
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,450
    Post Thanks / Like
    Mentioned
    86 Post(s)
    Tagged
    19 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Quote Originally Posted by ipbr21054 View Post
    Here is my worksheet if maybe you could see the difference.
    Yes, that Worksheet_Change event code does not include the two lines of code Akuini suggested for you.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the VBHTML Maker
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  7. #7
    Board Regular
    Join Date
    Nov 2010
    Posts
    2,379
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Please check link again as first link was incorrect
    I have learning difficulties so please be patient if i'm slow on the uptake,Thanks Very Much...

  8. #8
    MrExcel MVP
    Moderator
    Peter_SSs's Avatar
    Join Date
    May 2005
    Location
    Macksville, Australia
    Posts
    40,450
    Post Thanks / Like
    Mentioned
    86 Post(s)
    Tagged
    19 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Quote Originally Posted by ipbr21054 View Post
    Please check link again as first link was incorrect
    Works for me. If I enter 17 characters in column B, or even just confirm an existing value (F2 -> Enter) then the 10th character turns red.
    Hope this helps, good luck.
    Peter
    Excel 365 - Windows 10
    - Want to help your helpers by posting a small, copyable, screen shot directly in your post? Look here
    - If posting vba code, please use Code Tags - like this [code]Paste code here[/code] - or use the VBHTML Maker
    - Read: Forum Rules, Forum Use Guidelines, & FAQ

  9. #9
    Board Regular
    Join Date
    Nov 2010
    Posts
    2,379
    Post Thanks / Like
    Mentioned
    9 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Please see my excel sheet TEST2
    I have learning difficulties so please be patient if i'm slow on the uptake,Thanks Very Much...

  10. #10
    Board Regular Akuini's Avatar
    Join Date
    Feb 2016
    Location
    Indonesia
    Posts
    1,961
    Post Thanks / Like
    Mentioned
    33 Post(s)
    Tagged
    4 Thread(s)

    Default Re: Advice for adding line of code to existing code please

    Worksheet_Change event runs if you change a cell content, if your data is already there and then you add the code then it won’t run.
    Just select the data in column B then hit ctrl+C (copy) , the font will turn red.

Some videos you may like

User Tag List

Tags for this Thread

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
  •