Digitar somente numeros em um textbox [RESOLVIDO]

Edu

01-08-2011 14:48:50

Ola teria como fazer uma macro ou funcao para que um textbox so aceite numeros seguido de "."(ponto) ou ","(virgula)
se houver ficarei muito grato

JValq

01-08-2011 15:17:43

Boa tarde,

Veja se este código ajuda:

Código: Selecionar todos

Private Sub TextBox1_Change()
    If Not IsNumeric(TextBox1.Text) Then TextBox1.Text = Empty
End Sub
Abraço

geroeane

04-08-2011 22:57:27

veja se ajuda.
Select Case KeyAscii
Case 8 'Backspace (seta de apagar)
Case 48 To 57 'Números de 0 a 9
Case 44 'Vírgula
If InStr(LarguraQuadrado.Text, ",") Then 'Se o campo já tiver vírgula então ele não adiciona
KeyAscii = 0 'Não adiciona a vírgula caso ja tenha
Else
KeyAscii = 44 'Adiciona uma vírgula
End If
Case Else
KeyAscii = 0 'Não deixa nenhuma outra caractere ser escrito
End Select

Melo

04-08-2011 23:37:51

Boa noite

Estou usando em meu projeto o seguinte para aceitar somente Numeros

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Dim strValid As String
strValid = "0123456789"

If InStr(strValid, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
' ESTE A BAIXO É PARA ESPECIFICAR QUE A CADA 3 NUMEROS COLOCAR UM TRAÇO (-) E DEPOIS MAIS 4
If Len(txtPlaca) = 3 Or Len(txtPlaca) = 4 Then
txtPlaca.Text = txtPlaca.Text & "-"
SendKeys "{End}", True
End If

Aproveito para PERGUNTAR
Como ficaria para digitar em uma TextBox uma placa, ex: Obrigar a Digitar somente (3) Letras e gerar o Traço (-) e depois aceitar os 4 numeros.

Como ficaria?

Mauro Coutinho

09-08-2011 02:28:00

Melo, pelas minhas pesquisas e conhecimento, o ideal seria crair uma Function expecifica, mas como ainda preciso estudar um pouco mais a possibilidade, criei uma forma paliativa, com a utilização de 02 Textbox, de uma olhada e veja se ajuda.

TextBox- Somente Letras e Numeros
viewtopic.php?f=17&t=1206

[]s

Pedro Jhonata

02-10-2016 06:19:11

Melo escreveu:Boa noite

Estou usando em meu projeto o seguinte para aceitar somente Numeros

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Dim strValid As String
strValid = "0123456789"

If InStr(strValid, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
' ESTE A BAIXO É PARA ESPECIFICAR QUE A CADA 3 NUMEROS COLOCAR UM TRAÇO (-) E DEPOIS MAIS 4
If Len(txtPlaca) = 3 Or Len(txtPlaca) = 4 Then
txtPlaca.Text = txtPlaca.Text & "-"
SendKeys "{End}", True
End If

Aproveito para PERGUNTAR
Como ficaria para digitar em uma TextBox uma placa, ex: Obrigar a Digitar somente (3) Letras e gerar o Traço (-) e depois aceitar os 4 numeros.

Como ficaria?

Muito bom, Melo!

Thigodoy

01-10-2019 16:21:26

Melo escreveu: ↑Qui Ago 04, 2011 8:37 pm Boa noite

Estou usando em meu projeto o seguinte para aceitar somente Numeros

Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)

Dim strValid As String
strValid = "0123456789"

If InStr(strValid, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
' ESTE A BAIXO É PARA ESPECIFICAR QUE A CADA 3 NUMEROS COLOCAR UM TRAÇO (-) E DEPOIS MAIS 4
If Len(txtPlaca) = 3 Or Len(txtPlaca) = 4 Then
txtPlaca.Text = txtPlaca.Text & "-"
SendKeys "{End}", True
End If

Aproveito para PERGUNTAR
Como ficaria para digitar em uma TextBox uma placa, ex: Obrigar a Digitar somente (3) Letras e gerar o Traço (-) e depois aceitar os 4 numeros.

Como ficaria?

Para cada textbox terei que colocar essa Private Sub substituindo pelo nome da textbox? ou tem como fazer um global? como seria