Textbox que sólo permitan números y algunos con decimales para Dante Amor

Tengo varios textbox que deben completar personas externas pero necesito dos cosas:

1ero que sólo puedan introducir números y no letras

2do que en otro textbox sólo introduzcan máximo dos decimales.

Please tu apoyo.

Respuesta
1

Para poner solamente números

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Por.Dante Amor
    If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
    End If
End Sub

En otro textbox, para poner números, el punto decimal y 2 decimales

Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Por.Dante Amor
    If Not (KeyAscii >= 48 And KeyAscii <= 57) And Not KeyAscii = 46 Then
        KeyAscii = 0
    ElseIf KeyAscii = 46 Then
        puntos = 1
        For i = 1 To Len(TextBox2)
            If Mid(TextBox2, i, 1) = "." Then
                puntos = puntos + 1
            End If
        Next
        If puntos > 1 Then
            KeyAscii = 0
        End If
    ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then
        lpunto = InStr(1, TextBox2, ".")
        If lpunto > 0 Then
            If TextBox2.SelStart < lpunto Then
            Else
                cad = Mid(TextBox2, lpunto + 1)
                If Len(cad) = 2 Then KeyAscii = 0
            End If
        End If
        Label2 = cad
    End If
End Sub

Ahora si también quieres limitar la cantidad de número permitidos en el textbox, modifica la propiedad MaxLength del textbox

Por ejemplo si solamente quieres 5 enteros, entonces

MaxLength = 8

Ejemplo:

12345.12

Debes considerar en la longitud el punto y 2 decimales.

Otro Ejemplo

MaxLength = 11

Ejemplo:

12345678.12


Le hice unos ajustes a la macro para aceptar números y 2 decimales

Private Sub TextBox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Por.Dante Amor
    pto = InStr(1, TextBox2, ".")
    If KeyAscii = 46 Then
        If pto > 0 Then KeyAscii = 0
    ElseIf KeyAscii >= 48 And KeyAscii <= 57 Then
        If Len(Mid(TextBox2, pto + 1)) = 2 And TextBox2.SelStart >= pto Then KeyAscii = 0
    Else
        KeyAscii = 0
    End If
End Sub

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas