Regresar el color a un textbox en formulario de excel

Quiero pedir su ayuda con un problema simple pero que no puedo dar con la solución, tengo un formulario con unos textbox, y lo tengo condicionado para que dependiendo del valor ingresado cambie de color, sin embargo si lo selecciono y no ingreso ningún valor cambia a color verde pero no puedo hacer que regrese a su color original osea el blanco.

Esta es el código:

Private Sub TextBox24_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.TextBox24 < 1260 Then
    TextBox24.BackColor = RGB(240, 65, 76)
    Else
    If TextBox24 >= 1260 Then
    TextBox24.BackColor = RGB(102, 255, 51)
    ElseIf TextBox24 = "" Then
    TextBox24.BackColor = RGB(234, 234, 234)
    End If
End If
End Sub
Respuesta
2

Nota: con el evento Exit, el color cambia cuando sales del Textbox

Otra opción:

Private Sub TextBox24_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  With TextBox24
    Select Case .Value
      Case "", Empty:   .BackColor = RGB(234, 234, 234)
      Case Is < 1260:   .BackColor = RGB(240, 65, 76)
      Case Else:        .BackColor = RGB(102, 255, 51)
    End Select
  End With
End Sub

Muchas gracias dante por tu apoyo.

Si puse la macro al salir del textbox para que se den cuenta si un valor esta fuera de lo esperado.

Al final quedó así:

Private Sub TextBox24_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Me.TextBox24 = Empty Then
        TextBox24.BackColor = RGB(255, 255, 255)
        Else
            If TextBox24 < 1260 Then
            TextBox24.BackColor = RGB(240, 65, 76)
            Else
                If TextBox24 >= 1260 And TextBox24 <= 1300 Then
                TextBox24.BackColor = RGB(102, 255, 51)
                Else
                    If TextBox24 > 1300 Then
                    TextBox24.BackColor = RGB(240, 65, 76)
                    End If
                End If
            End If
    End If
End Sub

Saludos.

Podrías simplificarlo de esta manera:

Private Sub TextBox24_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  With TextBox24
    Select Case .Value
      Case "", Empty:              .BackColor = RGB(255, 255, 255)
      Case Is < 1260, Is > 1300:   .BackColor = RGB(240, 65, 76)
      Case Is >= 1260, Is <= 1300: .BackColor = RGB(102, 255, 51)
    End Select
  End With
End Sub

1 respuesta más de otro experto

Respuesta
1

Solo para agregar que ya solucione el problema, en el ElseIf TextBox24="" Then

Cambié las comilas por Empty y listo.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas