ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Adding a Leter to a Numerical Value on Exit (https://www.excelbanter.com/excel-programming/413623-adding-leter-numerical-value-exit.html)

Corey ....[_2_]

Adding a Leter to a Numerical Value on Exit
 
Private Sub TextBox1_Change()
TextBox1.MaxLength = "4"
If IsNumeric(TextBox1) = True Then TextBox1 = UCase(TextBox1) Else TextBox1
= ""
End Sub

The above code i am using to ensure only a 4 digit numerical value is placed
in the textbox.
I actually want on EXIT only that value entered, but with a "A" before it.
Like A4587 etc.

I tried using :
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Textbox1.MaxLength="5"
Textbox1.value = "A"+Textbox1.value
End Sub

But it clears the Textbox value

Is there a way around this?



Leith Ross[_2_]

Adding a Leter to a Numerical Value on Exit
 
On Jul 6, 6:54 pm, "Corey ...." wrote:
Private Sub TextBox1_Change()
TextBox1.MaxLength = "4"
If IsNumeric(TextBox1) = True Then TextBox1 = UCase(TextBox1) Else TextBox1
= ""
End Sub

The above code i am using to ensure only a 4 digit numerical value is placed
in the textbox.
I actually want on EXIT only that value entered, but with a "A" before it.
Like A4587 etc.

I tried using :
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Textbox1.MaxLength="5"
Textbox1.value = "A"+Textbox1.value
End Sub

But it clears the Textbox value

Is there a way around this?


Hello Corey,

This TextBox_Exit event code will add the "A" if the value is a number
and is equal to 4 characters in length. Otherwise, the entry is
cleared and the cursor is placed back at the beginning of the TextBox.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim TextVal As Variant

TextVal = TextBox1.Value

With TextBox1
If Len(.Value) = 4 And IsNumeric(TextVal) Then
.Value = "A" & .Value
Else
.Value = ""
Cancel = True
End If
End With

End Sub

Sincerely,
Leith Ross

Corey ....[_2_]

Adding a Leter to a Numerical Value on Exit
 
Leith,
Absolutely beautiful.

Thanks

Corey....

"Leith Ross" wrote in message
...
On Jul 6, 6:54 pm, "Corey ...." wrote:
Private Sub TextBox1_Change()
TextBox1.MaxLength = "4"
If IsNumeric(TextBox1) = True Then TextBox1 = UCase(TextBox1) Else
TextBox1
= ""
End Sub

The above code i am using to ensure only a 4 digit numerical value is
placed
in the textbox.
I actually want on EXIT only that value entered, but with a "A" before
it.
Like A4587 etc.

I tried using :
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Textbox1.MaxLength="5"
Textbox1.value = "A"+Textbox1.value
End Sub

But it clears the Textbox value

Is there a way around this?


Hello Corey,

This TextBox_Exit event code will add the "A" if the value is a number
and is equal to 4 characters in length. Otherwise, the entry is
cleared and the cursor is placed back at the beginning of the TextBox.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim TextVal As Variant

TextVal = TextBox1.Value

With TextBox1
If Len(.Value) = 4 And IsNumeric(TextVal) Then
.Value = "A" & .Value
Else
.Value = ""
Cancel = True
End If
End With

End Sub

Sincerely,
Leith Ross




Corey ....[_2_]

Adding a Leter to a Numerical Value on Exit
 
Just realised, if i enter the 4 digit number then exit, all is good.
But if i need to change the number, i am unable to enter the textbox again ?


"Corey ...." wrote in message
...
Leith,
Absolutely beautiful.

Thanks

Corey....

"Leith Ross" wrote in message
...
On Jul 6, 6:54 pm, "Corey ...." wrote:
Private Sub TextBox1_Change()
TextBox1.MaxLength = "4"
If IsNumeric(TextBox1) = True Then TextBox1 = UCase(TextBox1) Else
TextBox1
= ""
End Sub

The above code i am using to ensure only a 4 digit numerical value is
placed
in the textbox.
I actually want on EXIT only that value entered, but with a "A" before
it.
Like A4587 etc.

I tried using :
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Textbox1.MaxLength="5"
Textbox1.value = "A"+Textbox1.value
End Sub

But it clears the Textbox value

Is there a way around this?


Hello Corey,

This TextBox_Exit event code will add the "A" if the value is a number
and is equal to 4 characters in length. Otherwise, the entry is
cleared and the cursor is placed back at the beginning of the TextBox.

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Dim TextVal As Variant

TextVal = TextBox1.Value

With TextBox1
If Len(.Value) = 4 And IsNumeric(TextVal) Then
.Value = "A" & .Value
Else
.Value = ""
Cancel = True
End If
End With

End Sub

Sincerely,
Leith Ross







All times are GMT +1. The time now is 03:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com