Consulting

Results 1 to 15 of 15

Thread: Changing Font Colour

  1. #1

    Changing Font Colour

    Hi

    I have some code which has an text output and I need to change the latter part of the text to a different colour. Is this possible?

    For e.g. I have added

    <code> & cell.Offset(0, -1).Color = vbRed </code>

    to the end of

    <code> " ( POSITION NUMBER : " & cell.Offset(0, -1).Value & cell.Offset(0, -1).Color = vbRed & " ) " </code>

    but not getting the desired output.

    Any help would be appreciated.

    TIA

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Hi Branston,

    Below is a sub i used once to change the colour of text within text, it should give you a steer.

    Sub ChangeColour(rCell As Range)    
        Dim Counter As Integer
        Dim MyString As String
        
        MyString = rCell.Value
        For Counter = 1 To Len(MyString)
            If Mid(MyString, Counter, 1) = ">" Then
                rCell.Characters(Start:=Counter, Length:=1).Font.Color = vbRed
            ElseIf Mid(MyString, Counter, 1) = "|" Then
                rCell.Characters(Start:=Counter, Length:=1).Font.Color = vbBlue
            End If
        Next
        
    End Sub
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Thanks. Have tried different variations without joy.

    I've added the text before in case it helps.

    <code> " [ Points : " & Round(cell.Offset(0, 2).Value * 100, 0) & "% ]" & _ " [ POSITION NUMBER : " & cell.Offset(0, -1).Value & " ] " </code>

    I need the 'POSITION NUMBER' bit in line 2 above in a different colour to the 'Points' in line 1.

    Anyone?

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Use the macro recorder while changing the colour of a part of the text in the cell.

  5. #5
    Thanks snb. The issue I have is that the text string is created dynamically ie. changes depending on the candidate number (and also the text is placed in a pic which was created using the camera tool). I'm not sure how to use the macro recorder in this situation.

    Just need to know if I can actually add code like <code>& cell.Offset(0, -1).Color = vbRed</code> to my existing code to not only create the text string but to also change the colour at the end of the string.

    Thanks for your help and patience.

  6. #6
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Just try and adapt the code.

  7. #7
    Thanks. I have tried and seemed to have had some success by amending the worksheets function. Except now that whole string is Red instead of the just the latter part.

    <code>
    Worksheets(i).Range("B" & count + 1).Font.Color = vbRed
    Worksheets(i).Range("B" & count + 1) = " [ Points : " & Round(cell.Offset(0, 3).Value * 100, 0) & "% ]" & _
    " [ POSITION NUMBER : " & cell.Offset(0, -2).Value & " ] "
    </code>

    How could I change this now so that just the POSITION NUMBER is red ?

  8. #8
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    Could you not use something like this:

    Range("A1").Characters(InStr(Range("A1").Value, "POSITION NUMBER"), 15).Font.Color = vbRed
    Cheers
    Last edited by georgiboy; 12-10-2021 at 08:18 AM. Reason: Left the wrong range in the code A7 should heve been A1
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  9. #9
    I've played with it but it's liking any of my combinations............

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Do you want the literal text POSITION NUMBER in red or the value (number) after it? (Or both?)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,198
    Location
    The way i see it whether you want the words or just the number to change colour in the cell then you are going to have to do it after the value has been placed into the cell.

    Below is probably not he best example but it is an example of how you could change a specific part of the string colour after the text has been added:

    Sub test()    
        Dim cell As Range, pos As Long, str As String
        
        Set cell = Range("B1")
    
        Range("A2").Value = " ( POSITION NUMBER : " & cell.Offset(0, -1).Value & " ) "
        str = Range("A2").Value
        pos = InStr(str, ":")
        Range("A2").Characters(pos + 1, (Len(str) - pos) - 2).Font.Color = vbRed
    End Sub
    Hope this helps
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved
    Click here for a guide on how to upload a file with your post

    Excel 365, Version 2403, Build 17425.20146

  12. #12
    Hi

    I need both …..

    thanks

  13. #13
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    With Worksheets(i).Range("B" & Count + 1)
      .Value = " [ Points : " & Round(cell.Offset(0, 3).Value * 100, 0) & "% ]" & " [ POSITION NUMBER : " & cell.Offset(0, -2).Value & " ] "
      .Characters(InStr(.Value, "POSITION NUMBER : "), 18 + Len(cell.Offset(0, -2).Value)).Font.Color = vbRed
    End With
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  14. #14
    Thanks Pascal - will give that a try.

  15. #15
    .Characters(InStr(.Value, "POSITION NUMBER : "), 18 + Len(cell.Offset(0, -2).Value)).Font.Color = vbRed
    worked a treat - thanks again P45cal !

Posting Permissions

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