vba Excel VBA更改USERFORM上的值的颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21925406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 02:04:32  来源:igfitidea点击:

Excel VBA Change Color Of value on USERFORM

excelvbaexcel-vba

提问by Fulvio

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.

我想知道如何根据值更改值或文本框的颜色。例如,如果得到“F”,我希望这个值在用户表单上变成红色或文本框。

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

这是我想在哪里使用它或我想如何使用它的示例。

Thank you in advantage.

谢谢你的优势。

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

结束子

回答by PatricK

Change Color of the TextBox to Red:

将文本框的颜色更改为红色:

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

Change Text Color of the TextBox to Red:

将 TextBox 的文本颜色更改为红色:

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

UPDATE: Full solution (assuming txtPuntuacionis the TextBox):

更新:完整解决方案(假设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