2014-02-21 177 views
0

我想知道如何根據值更改值或文本框的顏色。例如,如果獲得「F」,我希望此值在用戶表單上以任何方式變爲紅色或文本框。Excel VBA更改USERFORM上的值的顏色

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 

末次

回答

1

文本框的顏色變爲紅色:

文本框的
UserForm1.TextBox1.BackColor = RGB(255,0,0) 

更改文本顏色爲紅色:

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

UPDATE:完整的解決方案(假設txtPuntuacion是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 
+0

THX帕特里克但這種解決方案是永久性的,我想情況下,如果得到一些值(如「F」)變成紅色,但如果讓其他的值(「B」)保持黑色 – Fulvio

+0

我純屬點你改變文本框/文本顏色的方向。爲「txtPuntuacion」添加另一個「If」塊以在黑/紅之間切換。 – PatricK

+0

非常感謝您的幫助,完美的工作 – Fulvio