Como poner un condicional que solo acepte numero a varios textbox

Tengo un código que cuando dígito una letra me arroja que solo admite numero pero esta aliado al presionar.

Este es el código

Private Sub s_total_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If (KeyAscii >= 97) And (KeyAscii < 122) Or (KeyAscii >= 65) And (KeyAscii < 90) Then
MsgBox "INGRESE SOLO EL NUMERO"
KeyAscii = 8
End If
End Sub

Pero busco hacer este mismo código pero para reducir tamaño del archivo; lo quiero en una sola operación a 11 textbox que tengo

3 Respuestas

Respuesta
2

 H o l a: 

esto ingresa en un módulo 

Public Function ValidarCaracter(ByVal KeyAscii As Integer) As Integer
    If (KeyAscii >= 97) And (KeyAscii < 122) Or (KeyAscii >= 65) And (KeyAscii < 90) Then
        MsgBox "INGRESE SOLO EL NUMERO"
        KeyAscii = 8
    End If
End Function

y en cada textbox pon esto

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 KeyAscii = Módulo1.ValidarCaracter(KeyAscii)
End Sub

valora la respuesta para finalizar saludos!

¡Gracias! Buen dato pero no quiero quiero trabajar con modulos;necesito un código no se como ; que cuando se active el user form los textbox solo dejen ingresar numero y el caso que ingresen letras le de un mensaje de advertencia

Entonces en el formulario pon la función

Public Function ValidarCaracter(ByVal KeyAscii As Integer) As Integer
    If (KeyAscii >= 97) And (KeyAscii < 122) Or (KeyAscii >= 65) And (KeyAscii < 90) Then
        MsgBox "INGRESE SOLO EL NUMERO"
        KeyAscii = 8
    End If
End Function

y en cada textbox pon esto

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
 KeyAscii = ValidarCaracter(KeyAscii)
End Sub
Respuesta
2

Para considerar todos los textbox que quieras, agrega el siguiente código en tu userform

Dim colTbxs As Collection 'Collection Of Custom Textboxes
'
Private Sub UserForm_Initialize()
    Dim ctlLoop As MSForms.Control
    Dim clsObject As Clase1
    Set colTbxs = New Collection
    For Each ctlLoop In Me.Controls
        Select Case ctlLoop.Name
            'Nombres de los textbox
            Case "TextBox1", "TextBox2", "TextBox3", "TextBox4"
                Set clsObject = New Clase1
                Set clsObject.tbxCustom1 = ctlLoop
                colTbxs.Add clsObject
        End Select
    Next ctlLoop
End Sub
'
Private Sub UserForm_Terminate()
    Set colTbxs = Nothing
End Sub

Crea una clase, entra al menú de VBA, Insertar, Módulo de clase. Revisa que la clase tenga el nombre "Clase1"

Ahora, pon el siguiente código dentro de la Clase

Public WithEvents tbxCustom1 As MSForms.TextBox 'Custom Textbox
'Referencia
'http://www.ozgrid.com/forum/showthread.php?t=80631
Private Sub tbxCustom1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
    If Not (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0 '
        MsgBox "Ingrese números", vbInformation, "CAPTURA"
    End If
End Sub
Private Sub Class_Terminate()
     'Destroy The Class Object And Free Up Memory
    Set tbxCustom1 = Nothing
End Sub

Si tienes dudas, envíame tu archivo para adaptar los códigos.

Mi correo [email protected]

En el asunto del correo escribe tu nombre de usuario “andres gomez” y el título de esta pregunta.

Avísame en esta pregunta cuando me lo hayas enviado.


'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias

ok mensaje enviado

No puedo abrir tu archivo. Te envié mi archivo de prueba.

'S aludos. Dante Amor. Recuerda valorar la respuesta. G racias
Respuesta
1

Para que solo te permita ingresar números

del  48 al 57 = son numeros, 13 = enter, 8 eliminar , 32 espacios

If AscW(e.KeyChar) >= 48 And AscW(e.KeyChar) <= 57 Or AscW(e.KeyChar) = 13 Or AscW(e.KeyChar) = 32 Or AscW(e.KeyChar) = 8 Then
            If AscW(e.KeyChar) = 13 Then
                RucClienteTextBox.Focus()
                e.Handled = True
            End If
        Else
            e.KeyChar = " "
        End If

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas