繁体   English   中英

使用VBA Excel更改单元格中文本的颜色

[英]Change color in text from cell with VBA excel

我要在保存到工作表时更改一些文本,该单元格具有多个文本框值。

VBA-excel ,当文本框显示为红色时,它必须以红色字体将该文本值保存在单元格中。
如果不是红色,则必须保存为黑色字体。

Range("w" & lmaxrows + 1).Value = "S " & TextBox42.Value & " |VP " & TextBox43.Value & " |NP " & TextBox47.Value & " |E " & TextBox48.Value

If TextBox42.BackColor = vbRed Then
    With Range("w" & lmaxrows)
    d = "S " & TextBox42.Value
    If Range("w" & lmaxrows).Value > "" Then Replace(Range("w"), d, Range("w")).Font.Color = vbRed Range("w")).Font.Color = vbRed
end if 
end if

我现在有这个,但是不起作用。

如何检查文本框的长度,然后使用它来更改单元格上许多字符的颜色:

Private Sub CommandButton1_Click()
    Sheet1.Cells(1, 1).Value = TextBox1.Text & " some other text"
    lengthofRed = Len(TextBox1.Text)
    'check the length of the text box
    If TextBox1.BackColor = vbRed Then
        Sheet1.Cells(1, 1).Characters(1, lengthofRed).Font.Color = vbRed
    End If
End Sub

编辑:

更改上面的内容以使其与您的代码匹配,我相信以下内容应能达到您的期望:

Range("w" & lmaxrows + 1).Value = "S " & TextBox42.Value & " |VP " & TextBox43.Value & " |NP " & TextBox47.Value & " |E " & TextBox48.Value

If TextBox42.BackColor = vbRed Then
    With Range("w" & lmaxrows)
    d = "S " & TextBox42.Value
    If Range("w" & lmaxrows).Value > "" Then
        Range("W" & lmaxrows + 1).Characters(1, Len(d)).Font.Color = vbRed
    End If
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM