簡體   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