dcsimg
Results 1 to 2 of 2

Thread: Automatically coloring specific characters text within cell in Excel

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2018
    Posts
    1

    Question Automatically coloring specific characters text within cell in Excel

    I found a partial answer on a locked thread here but need just a little more help to make it right for my situation.

    I have a massive sheet showing data points like the below. The solution in the link above (and showing below) will change one of the * or # but not both - and not if there is additional characters after the instance of the * or #. I have to be able to have the solution continue to look at all characters in the string, and action each instance of one or both of the characters.


    ORIGINAL DESIRED
    E*2#TEST10 E*2#TEST10
    E*2#STA37 E*2#STA37
    E*2#FS15 E*2#FS15
    E235*JK#123 E235*JK#123
    E#Flk*sda# E#Flk*sda#

    The code I'm using is below and I think the issue lies with Select Case Right(Target.CellsValue, 1) - I just have no idea how to correct this...

    Thanks in advance for any and all suggestions!

    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
     
    Application.EnableEvents = False
    On Error GoTo ErrHandler
     
    Select Case Right(Target.Cells.Value, 1)
     
    Case "#"
        Target.Cells.Characters(Start:=Len(Target.Cells.Value), Length:=1).Font.Color = vbRed
        Target.Cells.Characters(Start:=Len(Target.Cells.Value), Length:=1).Font.Bold = True
    Case "*"
        Target.Cells.Characters(Start:=Len(Target.Cells.Value), Length:=1).Font.Color = vbGreen
        Target.Cells.Characters(Start:=Len(Target.Cells.Value), Length:=1).Font.Bold = True
    End Select
    
    Application.EnableEvents = True
    Exit Sub
    ErrHandler:
     
    Application.EnableEvents = True
     
    End Sub

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,929

    Re: Automatically coloring specific characters text within cell in Excel

    slect case will only work with the first found case, try like
    Code:
    Select Case Right(Target.Cells.Value, 1)
     
    Case "#", "*"
        Target.Cells.Characters(Start:=Len(Target.Cells.Value), Length:=1).Font.Color = vbRed
        Target.Cells.Characters(Start:=Len(Target.Cells.Value), Length:=1).Font.Bold = True
    
    End Select
    alternatively you could use a couple of if statements

    i would have thought the code would only work with the last character when leaving the cell (not as you type), as your specific characters can be anywhere within the cell text, possibly you need to loop through all characters in the cell for each cell
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width