Vba 更改文本颜色,因为它';附加了什么?

Vba 更改文本颜色,因为它';附加了什么?,vba,excel,ms-project,Vba,Excel,Ms Project,我将通过将MS Project tasks信息附加到彼此来生成一些大型excel单元格值,然后我将计算自上次报告以来某个任务是否发生了更改。我只需要给单元格中已更改的任务上色,但它将与许多其他任务组成一个长字符串。如果我可以在添加任务时更改任务的颜色,那就太好了 我在想我必须使用某种形式的“With”语句,但我不知道从哪里开始 With cell .FutureFormat red .Value = .Value & "abc" End With 或者类似的 Strin

我将通过将MS Project tasks信息附加到彼此来生成一些大型excel单元格值,然后我将计算自上次报告以来某个任务是否发生了更改。我只需要给单元格中已更改的任务上色,但它将与许多其他任务组成一个长字符串。如果我可以在添加任务时更改任务的颜色,那就太好了

我在想我必须使用某种形式的“With”语句,但我不知道从哪里开始

With cell
    .FutureFormat red
    .Value = .Value & "abc"
End With
或者类似的

Stringthing = "ABC"
Stringthing.Format = red
Cell.value = cell.value & Stringthing

下面是一个示例代码:

Option Explicit

Public Sub AppendStringAndColorize()
    Dim str As String
    str = "abc"

    Dim cell As Range
    Set cell = Range("A1")

    Dim CellLength As Long
    CellLength = Len(cell.Value)

    With cell
        .Value = .Value & str
        .Characters(Start:=CellLength + 1, Length:=Len(str)).Font.Color = vbRed
    End With
End Sub
首先需要记住原始值的长度作为起始点,以便为该值之后的字符着色


要保留旧颜色,请执行以下操作:

Public Sub AppendStringAndColorizeKeepingOldColors()
    Dim str As String
    str = "abc"

    Dim cell As Range
    Set cell = Range("A1")

    Dim CharList() As Variant
    Dim CurrentColor As Double
    CurrentColor = cell.Characters(1, 1).Font.Color

    Dim iColor As Long 'color change counter
    iColor = 1
    ReDim CharList(1 To 2, 1 To 1) As Variant
    CharList(1, iColor) = CurrentColor

    Dim CellLength As Long
    CellLength = cell.Characters.Count

    'analyze colors and save into array
    Dim i As Long
    For i = 1 To CellLength
        If cell.Characters(i, 1).Font.Color <> CurrentColor Then
            CurrentColor = cell.Characters(i, 1).Font.Color
            iColor = iColor + 1
            ReDim Preserve CharList(1 To 2, 1 To iColor)
            CharList(1, iColor) = CurrentColor
        End If
        CharList(2, iColor) = CharList(2, iColor) + 1
    Next i

    'change cell value (append only!)
    cell.Value = cell.Value & str

    're-write colors
    Dim ActChar As Long
    ActChar = 1
    For i = LBound(CharList) To UBound(CharList, 2)
        cell.Characters(Start:=ActChar, Length:=CharList(2, i)).Font.Color = CharList(1, i)
        ActChar = ActChar + CharList(2, i)
    Next i

    'color for new appended string
    cell.Characters(Start:=CellLength + 1, Length:=Len(str)).Font.Color = vbYellow 'desired color

End Sub
Public子附录stringandcolorizekeepingoldcolors()
作为字符串的Dim str
str=“abc”
暗淡单元格作为范围
设置单元格=范围(“A1”)
Dim CharList()作为变量
双色
CurrentColor=单元格。字符(1,1)。字体。颜色
与“长”颜色变化计数器相同的暗淡iColor
iColor=1
ReDim字符列表(1到2,1到1)作为变量
字符列表(1,iColor)=当前颜色
长度和长度一样长
CellLength=cell.Characters.Count
'分析颜色并保存到数组中
我想我会坚持多久
对于i=1到单元长度
如果cell.Characters(i,1).Font.Color当前颜色,则
CurrentColor=cell.Characters(i,1).Font.Color
iColor=iColor+1
ReDim保留字符列表(1到2,1到iColor)
字符列表(1,iColor)=当前颜色
如果结束
字符列表(2,iColor)=字符列表(2,iColor)+1
接下来我
'更改单元格值(仅附加!)
cell.Value=cell.Value&str
“重新写颜色
像长一样暗
ActChar=1
对于i=LBound(CharList)到UBound(CharList,2)
cell.Characters(开始:=ActChar,长度:=CharList(2,i)).Font.Color=CharList(1,i)
ActChar=ActChar+CharList(2,i)
接下来我
'新附加字符串的颜色
cell.Characters(开始:=cellength+1,长度:=Len(str)).Font.Color=vbYellow'所需颜色
端接头

下面是一个示例代码:

Option Explicit

Public Sub AppendStringAndColorize()
    Dim str As String
    str = "abc"

    Dim cell As Range
    Set cell = Range("A1")

    Dim CellLength As Long
    CellLength = Len(cell.Value)

    With cell
        .Value = .Value & str
        .Characters(Start:=CellLength + 1, Length:=Len(str)).Font.Color = vbRed
    End With
End Sub
首先需要记住原始值的长度作为起始点,以便为该值之后的字符着色


要保留旧颜色,请执行以下操作:

Public Sub AppendStringAndColorizeKeepingOldColors()
    Dim str As String
    str = "abc"

    Dim cell As Range
    Set cell = Range("A1")

    Dim CharList() As Variant
    Dim CurrentColor As Double
    CurrentColor = cell.Characters(1, 1).Font.Color

    Dim iColor As Long 'color change counter
    iColor = 1
    ReDim CharList(1 To 2, 1 To 1) As Variant
    CharList(1, iColor) = CurrentColor

    Dim CellLength As Long
    CellLength = cell.Characters.Count

    'analyze colors and save into array
    Dim i As Long
    For i = 1 To CellLength
        If cell.Characters(i, 1).Font.Color <> CurrentColor Then
            CurrentColor = cell.Characters(i, 1).Font.Color
            iColor = iColor + 1
            ReDim Preserve CharList(1 To 2, 1 To iColor)
            CharList(1, iColor) = CurrentColor
        End If
        CharList(2, iColor) = CharList(2, iColor) + 1
    Next i

    'change cell value (append only!)
    cell.Value = cell.Value & str

    're-write colors
    Dim ActChar As Long
    ActChar = 1
    For i = LBound(CharList) To UBound(CharList, 2)
        cell.Characters(Start:=ActChar, Length:=CharList(2, i)).Font.Color = CharList(1, i)
        ActChar = ActChar + CharList(2, i)
    Next i

    'color for new appended string
    cell.Characters(Start:=CellLength + 1, Length:=Len(str)).Font.Color = vbYellow 'desired color

End Sub
Public子附录stringandcolorizekeepingoldcolors()
作为字符串的Dim str
str=“abc”
暗淡单元格作为范围
设置单元格=范围(“A1”)
Dim CharList()作为变量
双色
CurrentColor=单元格。字符(1,1)。字体。颜色
与“长”颜色变化计数器相同的暗淡iColor
iColor=1
ReDim字符列表(1到2,1到1)作为变量
字符列表(1,iColor)=当前颜色
长度和长度一样长
CellLength=cell.Characters.Count
'分析颜色并保存到数组中
我想我会坚持多久
对于i=1到单元长度
如果cell.Characters(i,1).Font.Color当前颜色,则
CurrentColor=cell.Characters(i,1).Font.Color
iColor=iColor+1
ReDim保留字符列表(1到2,1到iColor)
字符列表(1,iColor)=当前颜色
如果结束
字符列表(2,iColor)=字符列表(2,iColor)+1
接下来我
'更改单元格值(仅附加!)
cell.Value=cell.Value&str
“重新写颜色
像长一样暗
ActChar=1
对于i=LBound(CharList)到UBound(CharList,2)
cell.Characters(开始:=ActChar,长度:=CharList(2,i)).Font.Color=CharList(1,i)
ActChar=ActChar+CharList(2,i)
接下来我
'新附加字符串的颜色
cell.Characters(开始:=cellength+1,长度:=Len(str)).Font.Color=vbYellow'所需颜色
端接头

以下是在不干扰现有格式的情况下添加新文本的方法

注意:这种方法最多只能使用250个字符。我不确定在你到达那一点后有没有办法

Public Sub Tester()
    Const NUM As Long = 20
    Const TXT As String = "The quick brown for jumped over the lazy dogs"

    Dim colors, i, l

    colors = Array(vbRed, vbBlue)

    With ActiveSheet.Range("A1")

        For i = 1 To NUM
            l = Len(.Value)
            'Error here if trying to access characters after ~250     
            With .Characters(Start:=l + 1, Length:=Len(TXT) + 1)
                .Text = TXT & vbLf
                .Font.Color = colors(i Mod 2)
            End With
        Next i

    End With

End Sub

以下是如何添加新文本而不干扰现有格式

注意:这种方法最多只能使用250个字符。我不确定在你到达那一点后有没有办法

Public Sub Tester()
    Const NUM As Long = 20
    Const TXT As String = "The quick brown for jumped over the lazy dogs"

    Dim colors, i, l

    colors = Array(vbRed, vbBlue)

    With ActiveSheet.Range("A1")

        For i = 1 To NUM
            l = Len(.Value)
            'Error here if trying to access characters after ~250     
            With .Characters(Start:=l + 1, Length:=Len(TXT) + 1)
                .Text = TXT & vbLf
                .Font.Color = colors(i Mod 2)
            End With
        Next i

    End With

End Sub

.Value=.Value&str
是否会删除已应用于该单元格的任何颜色格式?我本来也打算尝试这种方法。@BigBen好吧,现在有些不同了。据我所知,在更改单元格的值时,不可能保持字符的部分着色。它可以通过分析原始单元格值和颜色字符来工作,记住在变量数组(或其他)中,然后更改值并重新设置所有内容的优先级。但这看起来有点麻烦。也许你应该彻底地重新思考你的方法,你可以走另一条路。是的,这正是我不想尝试的,但考虑到“这将是一个漫长的过程,还有很多其他任务”,我担心这是必要的+1至少是一个好的起点。@BigBen好吧,Excel的优势肯定不是在一个单元格中使用不同的格式,这不是它的初衷。它的力量在于计算。您可能会重新考虑它是否是用于此目的的合适工具,或者自己编写繁琐的解决方法。您可以使用字符方法添加文本,而不会干扰现有格式。
.Value=.Value&str
是否会删除已应用于该单元格的任何颜色格式?我本来也打算尝试这种方法。@BigBen好吧,现在有些不同了。据我所知,在更改单元格的值时,不可能保持字符的部分着色。它可以通过分析原始单元格值和颜色字符来工作,记住在变量数组(或其他)中,然后更改值并重新设置所有内容的优先级。但这看起来有点麻烦。也许你应该彻底地重新思考你的方法,你可以走另一条路。是的,这正是我不想尝试的,但我担心这是必要的