I want to automatically divide the string entered in one cell with different colors in Excel and replace it with a new cell.

Asked 1 years ago, Updated 1 years ago, 332 views

How can I automatically divide a string entered in one cell with different colors in Excel and replace it with a cell?

For example,
Cell [A1] Ah (black) Good (blue) U (black)

and what is entered in the cell

Cell [A2] Ah (black)
Cell [A3] Good (blue)
Cell [A4] yomiireru (black)

I would like to split it up with

vba excel

2022-12-21 09:48

1 Answers

Range.Characters property allows you to retrieve fonts one character at a time, so you can achieve your goal by honestly looping them.

sample code

Sub button 1_Click()
    Dimcell Asrange
    Set cell= range ("A1")
    Dim color As Integer 'Current color index
    Dimrow As Integer' Current Output Leading Number
    Dim start As Integer 'Current Color Start Location
    color=cell.Characters(start:=1, Length:=1).Font.ColorIndex' First Color
    row = 2
    start = 1
    
    Fori=1 Tocell.Characters.Count
        Dimch As Characters
        Dimc As Integer
        'Check the character color one character at a time
        Setch=cell.Characters (start:=i, length:=1)
        c=ch.Font.ColorIndex
        If c<>color Then
            'If the current string is different from the previous string, output up to the previous string.
            Cells(row,1).Value=cell.Characters(start, i-start).Text
            Cells(row,1).Font.ColorIndex=color
            'Update variable with current string
            start=i
            row=row+1
            color=c
        End If
    Next
    'Output up to last string
    Cells(row,1).Value=cell.Characters(start, i-start).Text
    Cells(row,1).Font.ColorIndex=color
End Sub


2022-12-22 05:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.