I want to change some text when saving to worksheet, the cell has multiple textbox values together.
When textbox is colored red in VBA-excel
, it has to save that text value in the cell in red font.
When it is not red, it has to save in black font color.
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
I have this for now, but it doesn't work.
How about checking the length of your textbox and then using that to change the color of that many characters on the Cell:
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
EDIT:
Changing the above to match your code, I believe the following should do what you expect:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.