Format TextBox to apply space within Post Code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,757
Office Version
  1. 2007
Platform
  1. Windows
On my userform in TextBox6 will have a post code entered.

Some users do not apply a space within it.
Example

BS292HH It should be BS29 2HH
&
BS79JN it should be BS7 9JN

How can vba format the incorrect post code to the correct post code when the user has typed wrong

Thanks
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
does this work for you ?
VBA Code:
Private Sub TextBox6_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox6) > 0 Then
        Me.TextBox6.Value = Format(Replace(Me.TextBox6.Value, " ", ""), "@@@@ @@@")
    End If
End Sub
 
Upvote 0
Solution
@NoSparks
Yesterday you advised a code for correcting a postcode that was typed incorrectly without the space.

I wish to use the same BUT for a telephone number

I have used the same code as shown below & changed the TextBox number BUT how do i take out the piece " ", ""),
The below works fine but looking to have the correct code for a later us.
Thanks

VBA Code:
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox2) > 0 Then
        Me.TextBox2.Value = Format(Replace(Me.TextBox2.Value, " ", ""), "@@@@@ @@@@@@")
    End If
End Sub
 
Upvote 0
This should be a new question
I don't know what would be or could be entered by the user and am not familiar with what resulting phone number should actually be in the UK

Locally (for me) phone numbers would be displayed as either
(250) 123-4567 or 250-123-4567 or 1-250-123-4567 if long distance
 
Upvote 0
does this work for you ?
VBA Code:
Private Sub TextBox6_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox6) > 0 Then
        Me.TextBox6.Value = Format(Replace(Me.TextBox6.Value, " ", ""), "@@@@ @@@")
    End If
End Sub
Should add trim function
VBA Code:
Private Sub TextBox6_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Me.TextBox6) > 0 Then
        Me.TextBox6.Value = Trim(Format(Replace(Me.TextBox6.Value, " ", ""), "@@@@ @@@"))
    End If
End Sub
 
Upvote 0
Thanks i would not have thought to go change this as it was on another form.

Thanks again now altered
 
Upvote 0

Forum statistics

Threads
1,224,111
Messages
6,176,429
Members
452,728
Latest member
mihael546

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top