0

I would like to know how can I change the color of value or Text-box depending the value. example if the get "F" I want this value turn RED or the Text-box either way on the User-form.

Here is a example of where I want to use it or how I would like to use it.

Private Sub cmdAceptar_click()
Dim n1 As Double, n2 As Double, n3 As Double, n4 As Double
Dim promedio As Integer

n1 = Val(txtn1): n2 = Val(txtn2)
n3 = Val(txtn3): n4 = Val(txtn4)
promedio = CInt((n1 + n2 + n3 + n4) / 4)
txtPromedio = Str(promedio)

If promedio >= 90 And promedio <= 100 Then
    txtPuntuacion = "A"
ElseIf promedio >= 80 And promedio <= 89 Then
    txtPuntuacion = "B"
ElseIf promedio >= 70 And promedio <= 79 Then
    txtPuntuacion = "C"
ElseIf promedio >= 60 And promedio <= 69 Then
    txtPuntuacion = "D"
ElseIf promedio >= 0 And promedio <= 59 Then
    txtPuntuacion = "F"
Else: MsgBox "Error de datos", vbCritical, "Mensaje"
End If
End Sub

1 Answer 1

2

Change Color of the TextBox to Red:

UserForm1.TextBox1.BackColor = RGB(255,0,0)

Change Text Color of the TextBox to Red:

UserForm1.TextBox1.ForeColor = RGB(255,0,0)

UPDATE: Full solution (assuming txtPuntuacion is the TextBox):

Private Sub cmdAceptar_click()
    Dim n1 As Double, n2 As Double, n3 As Double, n4 As Double
    Dim promedio As Integer
    Dim sGrade As String

    n1 = Val(txtn1): n2 = Val(txtn2)
    n3 = Val(txtn3): n4 = Val(txtn4)
    promedio = CInt((n1 + n2 + n3 + n4) / 4)
    txtPromedio = Str(promedio)

    Select Case promedio
        Case 90 To 100: sGrade = "A"
        Case 80 To 89:  sGrade = "B"
        Case 70 To 79:  sGrade = "C"
        Case 60 To 69:  sGrade = "D"
        Case 0 To 59:   sGrade = "F"
        Case Else
            MsgBox "Error de datos", vbCritical, "Mensaje"
            Exit Sub
    End Select
    txtPuntuacion.Value = sGrade
    If sGrade = "F" Then
        txtPuntuacion.ForeColor = RGB(255, 0, 0)
    Else
        txtPuntuacion.ForeColor = RGB(0, 0, 0)
    End If
End Sub
Sign up to request clarification or add additional context in comments.

2 Comments

Thx patricK but this solutions is permanent, i want in case that if get some value (as "F") turn red but if get other value (as "B") keep the black color
I was purely point you the direction to change color of the text box/text. Add another If block for txtPuntuacion to switch between Black/Red.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.