ludehenrique2cia
19-04-2016 13:33:20
Bom dia! Tenho um formulário onde preciso inserir documentos pelos seus respectivos números, e como é um total de 40 numerados em sequencia, coloquei os TextBox para fazer essa soma de + 1 automático, pra preencher as demais.
Private Sub TextBoxAIinicial_Change()
'soma mais um na textbox
Text_AI1.Value = Val(TextBoxAIinicial.Value)
TextAI2.Value = Val(Text_AI1.Value) + Val(1)
TextAI3.Value = Val(TextAI2.Value) + Val(1)
TextAI4.Value = Val(TextAI3.Value) + Val(1)
End Sub
Obs: Mas preciso da seguinte formatação no TextBox – “Série, Faixa, Números” Ex: C-35-123456 (essa formatação já fiz).
Como posso fazer a soma de + 1 no número nas demais TextBox com essa formatação? Ficando então assim no TextBox:
TextBoxAIinicial – fica - C-35-123456
Text_AI1 – fica - C-35-123456
TextAI2 – fica - C-35-123457
TextAI3 – fica - C-35-123458
TextAI4 – fica - C-35-123459
E assim por diante até o TextAI40!
Segue o código que usei para formatar o TextBox que vai receber o valor:
Private Sub TextBoxAIinicial _KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Limita a Qde de caracteres
TextBoxAIinicial.MaxLength = 20
Select Case KeyAscii
Case 8, 48 To 57 ' BackSpace e numericos
If Len(TextBoxAIinicial) = 1 Or Len(TextBoxAIinicial) = 20 Then
TextBoxAIinicial.Text = TextBoxAIinicial.Text & "-"
SendKeys "{End}", False
ElseIf Len(TextBoxAIinicial) = 4 Then
TextBoxAIinicial.Text = TextBoxAIinicial.Text & "-"
'ElseIf Len(TextBoxAIinicial) = 11 Then
' TextBoxAIinicial.Text = TextBoxAIinicial.Text & "-"
SendKeys "{End}", False
End If
'Case Else ' o resto é travado
' KeyAscii = 0
End Select
Select Case KeyAscii
Case Asc("A") To Asc("Z"), Asc("a") To Asc("z")
If Len(TextBoxAIinicial) >= 1 Then
MsgBox "Favor inserir(C-35-1234567)Faixa-Série-Números!", vbCritical, "Erro de validação"
KeyAscii = 0
End If
Case Asc("0") To Asc("9")
If Len(TextBoxAIinicial) < 2 Then
MsgBox "Favor inserir a Faixa!(C-35-1234567)", vbCritical, "Erro de validação"
KeyAscii = 0
End If
Case Else
MsgBox "Favor inserir a Faixa-Série-Números!", vbCritical, "Erro de validação"
KeyAscii = 0
End Select
If KeyAscii = 14 Then ' tecla BackSpace apaga o último caracter da TextBox, a partir do último digitado.
Dim x As Integer
x = Len(TextBoxAIinicial.Text) - Pos
For i = 1 To x
TextBoxAIinicial.Text = Left(TextBoxAIinicial.Text, Len(TextBoxAIinicial.Text) - 1) ' apaga o último caracter
TextBoxAIinicial.SelStart = Pos
Next
End If
TextBox2.Text = TextBoxAIinicial.Text
TextBox3.Text = TextBox2.Text
TextBox4.Text = TextBox3.Text
End Sub
Private Sub TextBoxAIinicial_Change()
'soma mais um na textbox
Text_AI1.Value = Val(TextBoxAIinicial.Value)
TextAI2.Value = Val(Text_AI1.Value) + Val(1)
TextAI3.Value = Val(TextAI2.Value) + Val(1)
TextAI4.Value = Val(TextAI3.Value) + Val(1)
End Sub
Obs: Mas preciso da seguinte formatação no TextBox – “Série, Faixa, Números” Ex: C-35-123456 (essa formatação já fiz).
Como posso fazer a soma de + 1 no número nas demais TextBox com essa formatação? Ficando então assim no TextBox:
TextBoxAIinicial – fica - C-35-123456
Text_AI1 – fica - C-35-123456
TextAI2 – fica - C-35-123457
TextAI3 – fica - C-35-123458
TextAI4 – fica - C-35-123459
E assim por diante até o TextAI40!
Segue o código que usei para formatar o TextBox que vai receber o valor:
Private Sub TextBoxAIinicial _KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Limita a Qde de caracteres
TextBoxAIinicial.MaxLength = 20
Select Case KeyAscii
Case 8, 48 To 57 ' BackSpace e numericos
If Len(TextBoxAIinicial) = 1 Or Len(TextBoxAIinicial) = 20 Then
TextBoxAIinicial.Text = TextBoxAIinicial.Text & "-"
SendKeys "{End}", False
ElseIf Len(TextBoxAIinicial) = 4 Then
TextBoxAIinicial.Text = TextBoxAIinicial.Text & "-"
'ElseIf Len(TextBoxAIinicial) = 11 Then
' TextBoxAIinicial.Text = TextBoxAIinicial.Text & "-"
SendKeys "{End}", False
End If
'Case Else ' o resto é travado
' KeyAscii = 0
End Select
Select Case KeyAscii
Case Asc("A") To Asc("Z"), Asc("a") To Asc("z")
If Len(TextBoxAIinicial) >= 1 Then
MsgBox "Favor inserir(C-35-1234567)Faixa-Série-Números!", vbCritical, "Erro de validação"
KeyAscii = 0
End If
Case Asc("0") To Asc("9")
If Len(TextBoxAIinicial) < 2 Then
MsgBox "Favor inserir a Faixa!(C-35-1234567)", vbCritical, "Erro de validação"
KeyAscii = 0
End If
Case Else
MsgBox "Favor inserir a Faixa-Série-Números!", vbCritical, "Erro de validação"
KeyAscii = 0
End Select
If KeyAscii = 14 Then ' tecla BackSpace apaga o último caracter da TextBox, a partir do último digitado.
Dim x As Integer
x = Len(TextBoxAIinicial.Text) - Pos
For i = 1 To x
TextBoxAIinicial.Text = Left(TextBoxAIinicial.Text, Len(TextBoxAIinicial.Text) - 1) ' apaga o último caracter
TextBoxAIinicial.SelStart = Pos
Next
End If
TextBox2.Text = TextBoxAIinicial.Text
TextBox3.Text = TextBox2.Text
TextBox4.Text = TextBox3.Text
End Sub