+ Reply to Thread
Results 1 to 17 of 17

macro to color all specific letters in a range of text

Hybrid View

  1. #1
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    macro to color all specific letters in a range of text

    Hello,

    I'm trying to set up a macro to color all specific letters in a range of text in one cell. So for example the text ERFLEEWLEFDE, I want to give all the "E" a blue font color and font = bold.
    the macro code I came up with only colors the first "E" it finds.
    who can help me with some changes in the code to color all the "E" in the text.

    thanks in advance

    text in cell: ERFLEWLEFDE

    code so far:

    Sub Neg_Click()
    Dim LR As Long, i As Long
    LR = Range("G" & Rows.count).End(xlUp).Row
    Negcolor = vbBlue
    
    Select Case Neg.Value
    Case True
    Neg.Caption = "Negative"
    Neg.ForeColor = Negcolor
    For i = 3 To LR
        With Range("G" & i)
            If InStr(.Value, "E") <> 0 Then .Characters(InStr(.Value, "E"), 1).Font.Color = Negcolor
            If InStr(.Value, "E") <> 0 Then .Characters(InStr(.Value, "E"), 1).Font.Bold = True
        End With
    Next i
    Case False
    Neg.Caption = "Negative"
    Neg.ForeColor = vbBlack
    
    For i = 3 To LR
        With Range("G" & i)
            If InStr(.Value, "E") <> 0 Then .Characters(InStr(.Value, "E"), 1).Font.Color = vbBlack
            If InStr(.Value, "E") <> 0 Then .Characters(InStr(.Value, "E"), 1).Font.Bold = False
        End With
    Next i
    End Select
    
    End Sub

  2. #2
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    Hello,

    I'm sorry,
    trying to fix it but it then the site is blocked..?

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: macro to color all specific letters in a range of text

    Try

    Sub Neg_Click()
        Dim LR As Long, i, k As Long
        LR = Range("G" & rows.Count).End(xlUp).row
        Negcolor = vbBlue
        
        Select Case Neg.Value
            Case True
                Neg.Caption = "Negative"
                Neg.ForeColor = Negcolor
                For i = 3 To LR
                    With Range("G" & i)
                        For k = 1 To Len(.Value)
                            If .Characters(k, 1) = "E" Then
                                With .Characters(k, 1)
                                    .Font.Color = Negcolor
                                    .Font.Bold = True
                                End With
                            End If
                        Next
                    End With
                Next i
            Case False
                Neg.Caption = "Negative"
                Neg.ForeColor = vbBlack
                
                For i = 3 To LR
                    With Range("G" & i)
                        For k = 1 To Len(.Value)
                            If .Characters(k, 1) = "E" Then
                                With .Characters(k, 1)
                                    .Font.Color = vbBlack
                                    .Font.Bold = True
                                End With
                            End If
                        Next
                    End With
                Next i
        End Select
    
    End Sub
    多么想要告诉你 我好喜欢你

  4. #4
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    Hello,
    thanks for your help,
    it isn't working, it stuck on (If .Characters(k, 1) = "E" Then)
    error: runtime error 438. object doesn't support this property or method

  5. #5
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: macro to color all specific letters in a range of text

    Quote Originally Posted by WillemB View Post
    it isn't working, it stuck on (If .Characters(k, 1) = "E" Then)
    Sorry about that, missed out .Caption.

    Sub Neg_Click()
        Dim LR As Long, i, k As Long
        LR = Range("G" & rows.Count).End(xlUp).row
        Negcolor = vbBlue
        
        Select Case Neg.Value
            Case True
                Neg.Caption = "Negative"
                Neg.ForeColor = Negcolor
                For i = 3 To LR
                    With Range("G" & i)
                        For k = 1 To Len(.Value)
                            If .Characters(k, 1).Caption = "E" Then
                                With .Characters(k, 1)
                                    .Font.Color = Negcolor
                                    .Font.Bold = True
                                End With
                            End If
                        Next
                    End With
                Next i
            Case False
                Neg.Caption = "Negative"
                Neg.ForeColor = vbBlack
                
                For i = 3 To LR
                    With Range("G" & i)
                        For k = 1 To Len(.Value)
                            If .Characters(k, 1).Caption = "E" Then
                                With .Characters(k, 1)
                                    .Font.Color = vbBlack
                                    .Font.Bold = True
                                End With
                            End If
                        Next
                    End With
                Next i
        End Select
    
    End Sub

  6. #6
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: macro to color all specific letters in a range of text

    Hi.. Try this.. it will color the "e" character in the usedrange in column G in blue...

    Private Sub CommandButton1_Click()
        Dim f, x, j As Long
        For Each f In Range("G3:G" & Range("G" & Rows.Count).End(xlUp).Row)
            For j = 1 To Len(f)
                x = Mid(f, j, 1)
                If x = "e" Then
                    Range(f.Address).Characters(Start:=InStr(1, f, x), Length:=1).Font.Color = -4165632
                End If
            Next j
        Next f
    End Sub

    Ooops.. after this line..
    Range(f.Address).Characters(Start:=InStr(1, f, x), Length:=1).Font.Color = -4165632
    Add this line to make the character be Bold..
     Range(f.Address).Characters(Start:=InStr(1, f, x), Length:=1).Font.Bold = True
    Attached Files Attached Files
    Last edited by apo; 02-14-2014 at 05:13 AM.

  7. #7
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    Thanks for your help

    it works!

  8. #8
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    It's Working

    Thanks for the help!

  9. #9
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: macro to color all specific letters in a range of text

    This will work now.. just needed to replace the "1" with the "j" variable..

    Private Sub CommandButton1_Click()
        Dim f, x, j As Long
        For Each f In Range("G3:G" & Range("G" & Rows.Count).End(xlUp).Row)
            For j = 1 To Len(f)
                x = Mid(f, j, 1)
                If x = "e" Then
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=1).Font.Color = -4165632
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=1).Font.Bold = True
                End If
            Next j
        Next f
    End Sub

  10. #10
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    dear Millz,

    the code is working nice,
    is it possible to select more letters in once? like the "E" and the "D" ant maybe other letters?

    thanks

  11. #11
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: macro to color all specific letters in a range of text

    Hi..
    Did you try my code..? I am pretty sure it does what you want..

    To select more letters.. just add the "Or" operator..

    Example:
    Private Sub CommandButton1_Click()
        Dim f, x, j As Long
        For Each f In Range("G3:G" & Range("G" & Rows.Count).End(xlUp).Row)
            For j = 1 To Len(f)
                x = Mid(f, j, 1)
               If x = "e" Or x = "a" Or x = "p" Then
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=1).Font.Color = -4165632
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=1).Font.Bold = True
                End If
            Next j
        Next f
    End Sub

  12. #12
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    Hey Apo,

    I'am trying to put it in a toggle button like the code here:
    it gives an error; Next without for?
    can you help

     
    Dim f, x, j As Long
        For Each f In Range("G3:G" & Range("G" & Rows.count).End(xlUp).Row)
    Negcolor = vbBlue
    
    Select Case Neg.Value
    Case True
    Neg.Caption = "Negative"
    Neg.ForeColor = Negcolor
            
            For j = 1 To Len(f)
                x = Mid(f, j, 1)
               If x = "E" Or x = "D" Or x = "" Then
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=j).Font.Color = vbBlue
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=j).Font.Bold = True
                End If
            Next j
        Next f
           
    Case False
    Ne.Caption = "Negative"
    Neg.ForeColor = vbBlack
    
    For j = 1 To Len(f)
                x = Mid(f, j, 1)
               If x = "E" Or x = "D" Or x = "" Then
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=j).Font.Color = vbblack
                    Range(f.Address).Characters(Start:=InStr(j, f, x), Length:=j).Font.Bold = False
                End If
            Next j
        Next f
    End Select
    End Sub

  13. #13
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: macro to color all specific letters in a range of text

    hi.. watching a movie right now.. ..

    but just quickly.. remove the first "Next f" and move the secondd "Next f" below the "End Select" statement.. thats a start anyway..

  14. #14
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    Hi,

    Not working yet, I will wait till end of the movie
    thanks

  15. #15
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: macro to color all specific letters in a range of text

    You can add more conditions like this:

    Sub Neg_Click()
        Dim LR As Long, i, k As Long
        LR = Range("G" & rows.Count).End(xlUp).row
        Negcolor = vbBlue
        
        Select Case Neg.Value
            Case True
                Neg.Caption = "Negative"
                Neg.ForeColor = Negcolor
                For i = 3 To LR
                    With Range("G" & i)
                        For k = 1 To Len(.Value)
                            Select Case .Characters(k, 1).Caption
                                Case "E", "D", "F", "Z" 'F and Z just as example.
                                    With .Characters(k, 1)
                                        .Font.Color = Negcolor
                                        .Font.Bold = True
                                    End With
                            End Select
                        Next
                    End With
                Next i
            Case False
                Neg.Caption = "Negative"
                Neg.ForeColor = vbBlack
                
                For i = 3 To LR
                    With Range("G" & i)
                        For k = 1 To Len(.Value)
                            Select Case .Characters(k, 1).Caption
                                Case "E", "D", "F", "Z" 'F and Z just as example.
                                    With .Characters(k, 1)
                                        .Font.Color = vbBlack
                                        .Font.Bold = True
                                    End With
                            End Select
                        Next
                    End With
                Next i
        End Select
    
    End Sub

  16. #16
    Registered User
    Join Date
    11-08-2013
    Location
    the Netherlands
    MS-Off Ver
    Excel 2007
    Posts
    47

    Re: macro to color all specific letters in a range of text

    It's working thanks,
    but if the value of the cells in column G is linked to another cell t gives error 1004 unable to get caption property of the characters class
    can you help me with this error

  17. #17
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: macro to color all specific letters in a range of text

    I don't think there's a way to highlight the letters if you are linking cell values. You must change them into values first.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how to find a color in a row where the color is blue , if true fill with a specific text
    By jayakumar.chandran in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-01-2012, 03:38 AM
  2. Macro to format row color in a range based on text value in column
    By toaksie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-25-2012, 12:54 PM
  3. Change text color of specific date range by macro in Excel
    By Morgan LeFay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-03-2006, 02:55 PM
  4. [SOLVED] how can i get EXcel to add up letters in a specific color?
    By lyssette in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-08-2006, 02:10 PM
  5. count by specific text color in range of cell
    By Tii99 in forum Excel General
    Replies: 2
    Last Post: 04-04-2006, 04:58 AM

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.6.0 RC 1