Substring colouring from Excel VBA: why do some obvious methods not work?

后端 未结 2 1904
感动是毒
感动是毒 2020-12-19 08:01

I\'ve been building some interesting visualizations that rely on VBA code\'s ability to set different colours for substrings in Excel. For a cell containing a string the syn

相关标签:
2条回答
  • 2020-12-19 08:11

    Formatting (including coloring) substrigns in a range of excel cells with a macro, see the video:

    http://youtu.be/O0h6T5Z7HwY

    0 讨论(0)
  • 2020-12-19 08:35

    Assigning the .Value does not magically figure how to append to the existing data. It erases the old data and puts in the new data.

    If the characters had colouring, the colour of the first character is used to colour the new string.

    If you want the actual appending, same as if you manually used the formula bar in Excel, then append using .Characters:

    Dim rngTestString As Range
    
    Set rngTestString = Range("colour_string")
    
    Range("colour_string").Characters(Len(Range("colour_string").Value) + 1).Text = "red "
    rngTestString.Characters(1, 4).Font.Color = RGB(255, 0, 0)
    
    Range("colour_string").Characters(Len(Range("colour_string").Value) + 1).Text = "green "
    rngTestString.Characters(5, 10).Font.Color = RGB(0, 255, 0)
    
    Range("colour_string").Characters(Len(Range("colour_string").Value) + 1).Text = "blue"
    rngTestString.Characters(11, 14).Font.Color = RGB(0, 0, 255)
    
    0 讨论(0)
提交回复
热议问题