Inicio > Microsoft Excel > leone > Si se deja un Textbox en blanco

Si se deja un Textbox en blanco

Experto:
Usuario:
Fecha: 10/09/2008
Valoración: (5,00 sobre 5) Categoría: Microsoft Excel
04/09/2008
enryke11, usuario preguntando en Microsoft Excel
Usuario
Tengo este codigo en un formulario de excel:
Private Sub TextBox1_Change()
largo_entrada = Len(Me.TextBox1)
Select Case largo_entrada
Case 2
Me.TextBox1.Value = Me.TextBox1.Value & "/"
Case 5
Me.TextBox1.Value = Me.TextBox1.Value & "/2008"
TextBox2.SetFocus
End Select
End Sub
Pero quiero que si no escribo nada en este Textbox1 que me aparezca la palabra "Anulada" además en la fecha que solo tenga que escribir el día y que el mes y año lo obtenga automaticamente de la fecha del sistema, eso si se pudiera y si no pues que si me equivoco a la hora de ingresar el formato de fecha que me envie un mensaje "Fecha mal ingresada" Ej: 02/99/2008 y que se quede en el TextBox1 para poder corregir el error.
Espero me entiendas que es lo que deseo hacer y de antemano agradezco tu pronta ayuda  Att. Enrique Castro
04/09/2008
enryke11, experto respondiendo en Microsoft Excel
Experto
<span style="font-family: 'Times New Roman'; font-size: 16px;">
<div style="color: #000000; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px; background-image: initial; background-repeat: initial; background-attachment: initial; -webkit-background-clip: initial; -webkit-background-origin: initial; background-color: #ffffff; background-position: initial initial; margin: 8px;">Hola,
Segun lo que puedo apreciar puedes validar el caso para la palabra "Anulada" con un "Case 0" en tu Select Case y utilizar para validar una Fecha Correcta preguntando si la Fecha introducida es Valida asi:
If IsDate(Me.TextBox1.Text) = False then
MsgBox "La Fecha instrucida es Invalida"
Me.TextBox1.Text
Else
'Continua la Ejecucion del Codigo
End If

Espero Te funcione
Saludos
Leone
</div>
</span>
04/09/2008
enryke11, experto respondiendo en Microsoft Excel
Experto
Me falto finalizar parte del codigo , disculpa
If IsDate(Me.TextBox1.Text) = False then
MsgBox "La Fecha introducida es Inválida", vbExclamation, "Fecha Inválida"
Me.TextBox1.Text = ""
Me.TextBox1.SetFocus
Else
'Continua la Ejecucion del Codigo
End If
Saludos
09/09/2008
enryke11, usuario preguntando en Microsoft Excel
Usuario
Muchas gracias por tu ayuda y pronta colaboración, el codigo que me enviaste me sirvio aunque no lo tome literal pero me ayudo mucho, bien ahora me he topado con un problemilla que si eres tan amable de ayudarme ya que en el codigo yo lo cambie por Me.TextBox4.SetFocus ahora el problema es que he intentado de mil maneras pero el cursor siempre se va al TextBox3 en vez del TextBox4 y no se porqué, tengo un codigo que al dejarlo en blanco me anota "Anulada" en el TextBox1, y del TextBox2 al TextBox4, me anote "0.00" pero en realidad lo que yo quiero es que al dejarlo en blanco haga lo anterior pero que se valla al CommandButton1 que tengo, pero como no se como hacerlo, pues opte para que se fuera al TextBox4, pero repito no se porque siempre se queda en el TextBox3, si pudieras ayudarme te lo agradecería, además te envio todo el codigo que tengo para ver si tu podrias encontrar donde esta mi error:
Private Sub CommandButton1_Click()
Rem inserta un renglon
Fila = 9
Col = 2
While Cells(Fila, Col) <> Empty
Fila = Fila + 1
Wend
Cells(Fila, 2) = TextBox1
Cells(Fila, 4) = Val(TextBox2)
Cells(Fila, 6) = Val(TextBox3)
Cells(Fila, 8) = Val(TextBox4)
TextBox1 = Empty
TextBox2 = Empty
TextBox3 = Empty
TextBox4 = Empty
TextBox1.SetFocus
End Sub
Private Sub TextBox1_Change()
largo_entrada = Len(Me.TextBox1)
Select Case largo_entrada
Case 2
Me.TextBox1.Value = Me.TextBox1.Value & "/"
Case 5
Me.TextBox1.Value = Me.TextBox1.Value & "/" & Year(Date)
TextBox2.SetFocus
End Select
End Sub
Private Sub TextBox2_Change()
Range("D8").Select
ActiveCell.FormulaR1C1 = TextBox2
End Sub
Private Sub TextBox3_Change()
Range("F8").Select
ActiveCell.FormulaR1C1 = TextBox3
End Sub
Private Sub TextBox4_Change()
Range("H8").Select
ActiveCell.FormulaR1C1 = TextBox4
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
UserForm1.Hide
End If
End Sub
Private Sub TextBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
UserForm1.Hide
End If
End Sub
Private Sub TextBox3_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
UserForm1.Hide
End If
End Sub
Private Sub TextBox4_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
UserForm1.Hide
End If
End Sub
Private Sub CommandButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
UserForm1.Hide
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text = "" Then
TextBox1 = "Anulada"
TextBox2 = "0.00"
TextBox3 = "0.00"
TextBox4 = "0.00"
Me.TextBox4.SetFocus
End If
End Sub
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox2.Text) Then
TextBox2.Text = Format(TextBox2.Text, "#,##0.00")
Else
TextBox2.Text = "0.00"
End If
End Sub
Private Sub TextBox3_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox3.Text) Then
TextBox3.Text = Format(TextBox3.Text, "#,##0.00")
Else
TextBox3.Text = "0.00"
End If
End Sub
Private Sub TextBox4_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If IsNumeric(TextBox4.Text) Then
TextBox4.Text = Format(TextBox4.Text, "#,##0.00")
Else
TextBox4.Text = "0.00"
End If
End Sub
10/09/2008
enryke11, experto respondiendo en Microsoft Excel
Experto
Hola te envio el codigo Modificado, espero te funcione como me funciono a mi. Lo que hice fue validar la palabra Anulada en el evento keyup que tienes del textbox2
Te valide un poco mas el formato para la fecha del Textbox1
Sustituye el codigo de los siguientes eventos de estos objetos:
Private Sub TextBox2_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 27 Then
UserForm1.Hide
End If
If TextBox1.Value = "Anulada" Then
CommandButton1.SetFocus
End If
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text = "" Then
TextBox1 = "Anulada"
TextBox2 = "0.00"
TextBox3 = "0.00"
TextBox4 = "0.00"
End If
End Sub
Private Sub TextBox1_Change()
If Me.TextBox1.Value <> "Anulada" Then
largo_entrada = Len(Me.TextBox1)
Select Case largo_entrada
Case 1
If Me.TextBox1.Value > 3 Then Me.TextBox1.Value = "0" & Me.TextBox1.Value & "/"
Case 2
If Right(Me.TextBox1.Value, 1) <> "/" Then Me.TextBox1.Value = Me.TextBox1.Value & "/"
Case 3
If Right(Me.TextBox1.Value, 1) > 1 And Right(Me.TextBox1.Value, 1) <> "/" Then
Me.TextBox1.Value = Format(Me.TextBox1.Value & "/" & Year(Date), "DD/MM/YYYY")
If IsDate(Me.TextBox1.Value) = True Then
TextBox2.SetFocus
Else
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox1.SetFocus
SendKeys "{home}+{end}"
End If
End If
Case 4
If Right(Me.TextBox1.Value, 1) = "/" Then
Me.TextBox1.Value = Format(Me.TextBox1.Value & Year(Date), "DD/MM/YYYY")
If IsDate(Me.TextBox1.Value) = True Then
TextBox2.SetFocus
Else
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox1.SetFocus
SendKeys "{home}+{end}"
End If
ElseIf Right(Me.TextBox1.Value, 1) > 1 And Right(Me.TextBox1.Value, 1) <> "/" Then
Me.TextBox1.Value = Format(Me.TextBox1.Value & "/" & Year(Date), "DD/MM/YYYY")
If IsDate(Me.TextBox1.Value) = True Then
TextBox2.SetFocus
Else
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox1.SetFocus
SendKeys "{home}+{end}"
End If
End If
Case 5
If Right(Me.TextBox1.Value, 1) <> "/" Then
Me.TextBox1.Value = Format(Me.TextBox1.Value & "/" & Year(Date), "DD/MM/YYYY")
Else
Me.TextBox1.Value = Format(Me.TextBox1.Value & Year(Date), "DD/MM/YYYY")
End If
If IsDate(Me.TextBox1.Value) = True Then
TextBox2.SetFocus
Else
MsgBox "La Fecha Introducida es Inválida, por favor intente de Nuevo", vbExclamation, "Fecha Inválida"
TextBox1.SetFocus
SendKeys "{home}+{end}"
End If
End Select
End If
End Sub

Saludos Leone
10/09/2008
enryke11, usuario preguntando en Microsoft Excel
Usuario
Muchas gracias por tu ayuda ya que es juestamente lo que queria que hiciera el codigo, te agradecería que me pudieras ayudar con otras dudas que tenga más adelante ya que hace apenas un mes que inicie a trabajar en VB, bueno no esta de más agradecerte por tu pronta respuesta y colaboración con esta pregunta que formule, Att. Enrique Castro
Enlaces patrocinados