0

数据:

我有下表:

160  89  85 116 161
147 117 133 148 191
 93  91  94  92 107
147 148 177 133 205
116 147 117 190 148

问题:

我想将数据合并到一个列中。

单行的代码,通过一个按钮运行。

代码:

Sub mergeStuff()
    Range("M3").Value = CStr(Range("H3").Value) + "," + CStr(Range("I3").Value) + "," + CStr(Range("J3").Value) + "," + CStr(Range("K3").Value) + "," + CStr(Range("L3").Value)
End Sub

输出示例:

    160,89,85,116,161
    147,117,133,148,191
     93,91,94,92,107
    147,148,177,133,205
    116,147,117,190,148

问题:

数字已格式化。它们有不同的颜色。如何在一个单元格中合并并保持颜色格式?

4

2 回答 2

1

您可以使用 .Characters 方法在单元格内或在单元格的字符串值内格式化字符。

一个例子是:

Cells(1, 1).Characters(1, 3).Font.Color = RGB(0, 255, 0)
于 2013-09-04T15:31:54.243 回答
1

此代码将存储颜色值,连接单元格,然后根据存储的颜色值对字符串进行着色。1 to 5应该更改以反映您要连接的列数。在您的示例中,有 5 列。该1 to 3部分可以单独放置。

Sub mergeStuff()

    Dim arrColors(1 To 5, 1 To 3) As Long
    Dim rIndex As Long
    Dim cIndex As Long
    Dim StartColor As Long
    Dim strOutput As String
    Dim i As Long

    For rIndex = 3 To Cells(Rows.Count, "H").End(xlUp).Row
        StartColor = 1
        strOutput = vbNullString
        For cIndex = Columns("H").Column To Columns("L").Column
            strOutput = strOutput & "," & Cells(rIndex, cIndex).Value
            arrColors(cIndex - Columns("H").Column + 1, 1) = StartColor
            arrColors(cIndex - Columns("H").Column + 1, 2) = Len(Cells(rIndex, cIndex).Value)
            arrColors(cIndex - Columns("H").Column + 1, 3) = Cells(rIndex, cIndex).Font.Color
            StartColor = StartColor + Len(Cells(rIndex, cIndex).Value) + 1
        Next cIndex

        With Cells(rIndex, "M")
            .Value = Mid(strOutput, 2) 'Remove beginning comma
            For i = 1 To UBound(arrColors, 1)
                .Characters(arrColors(i, 1), arrColors(i, 2)).Font.Color = arrColors(i, 3)
            Next i
        End With
    Next rIndex

End Sub
于 2013-09-04T15:46:48.613 回答