我想在保存到工作表时更改一些文本,单元格中有多个文本框值。
当文本框在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
我现在有这个,但它不起作用。
答案 0 :(得分:0)
如何检查文本框的长度,然后使用它来更改单元格上多个字符的颜色:
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