Hacer que solo se puedan poner números en muchos textbox

Hola Aprendemos.!!

Quiero consultarte nuevamente, como debo hacer para que al menos un textbox me permita escribir letras ( en un userform donde se colocan datos operativos "números" pero tiene un textbox de "Observaciones" donde se debería escribir palabras.)

Adjunto la consulta y respuesta de aquella vez.

Desde ya muchas gracias, slds.

Hola.!! Tengo un Userform con 80 textbox para la carga de datos, estos datos pueden ser solamente números, tengo resuelto esto pero para un textbox en particular:
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
'KeyAscii = 8 es el retroceso o BackSpace
If KeyAscii <> 8 And KeyAscii <> Asc(".") Then
KeyAscii = 0
End If
End If
End Sub
La consulta es como puedo aplicar este código para que aplique a todos los textbox a la vez ( se me había ocurrido con algún bucle for next, pero no encontré la forma ).
Desde ya muchas gracias.
Slds,
Tú me diste esta solución de funciono muy bien:
Option Explicit
Public WithEvents tbxCustom1 As MSForms.TextBox 'Custom Textbox
'Referencia
'http://www.ozgrid.com/forum/showthread.php?t=80631
Private Sub tbxCustom1_Change()
'Message Box To Display Which Textbox Was Changed
'MsgBox "You added A Number To: " & tbxCustom1.Name
'This is just to show you can handle múltiple events of the textbox
End Sub
Private Sub tbxCustom1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim n As Integer
'Allow only Numbers To be Entered Into Textbox
n = KeyAscii
Select Case KeyAscii
Case 46 To 57 'números (./0123456789)
Case Else
KeyAscii = 0
End Select
End Sub
Private Sub Class_Terminate()
'Destroy The Class Object And Free Up Memory
Set tbxCustom1 = Nothing
End Sub
Dim colTbxs As Collection 'Collection Of Custom Textboxes
Private Sub UserForm_Initialize()
Dim ctlLoop As MSForms.Control
Dim clsObject As clase1
'Create New Collection To Store Custom Textboxes
Set colTbxs = New Collection
'Loop Through Controls On Userform
For Each ctlLoop In Me.Controls
'Check If Control Is A Textbox
If TypeOf ctlLoop Is MSForms.TextBox Then
'Create A New Instance Of The Event Handler CLass
Set clsObject = New clase1
'Set The New Instance To Handle The Events Of Our Textbox
Set clsObject.tbxCustom1 = ctlLoop
'Add The Event Handler To Our Collection
colTbxs.Add clsObject
End If
Next ctlLoop
End Sub
Private Sub UserForm_Terminate()
'Destroy The Collection To Free Memory
Set colTbxs = Nothing
End Sub

1 Respuesta

Respuesta
1

Cambia esta macro Private Sub UserForm_Initialize()

Private Sub UserForm_Initialize()
    Dim ctlLoop As MSForms.Control
    Dim clsObject As Clase1
     'Create New Collection To Store Custom Textboxes
    Set colTbxs = New Collection
     'Loop Through Controls On Userform
    For Each ctlLoop In Me.Controls
         'Check If Control Is A Textbox
        If ctlLoop.Name <> "TextBox3" Then
        If TypeOf ctlLoop Is MSForms.TextBox Then
             'Create A New Instance Of The Event Handler CLass
            Set clsObject = New Clase1
             'Set The New Instance To Handle The Events Of Our Textbox
            Set clsObject.tbxCustom1 = ctlLoop
             'Add The Event Handler To Our Collection
            colTbxs.Add clsObject
        End If
        End If
    Next ctlLoop
End Sub

Cambia TextBox3, por el nombre del textbox de observaciones

Saludos. DAM
Si es lo que necesitas.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas