postcode format Netherlands

wimven

New Member
Joined
Feb 1, 2010
Messages
14
Please,

Can someone help me to the VBA lines for the entry field txtPostcd in a userform which should give the format:

4 digits, space, 2 letters
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
still no luck but i feel that this should be able to work.

On the worksheet i have a button which launches the macro "newclient":

Sub NewClient()
FrmNewClient.Show
End Sub

The form:
Private Sub UserForm_Initialize()
Dim maxvalrange, maxval
Set maxvalrange = Range("Clients!q2:q10000")
maxval = Application.WorksheetFunction.Max(maxvalrange)

TxtSurname = ""
TxtAttn = ""
TxtStreet = ""
TxtStrNr = ""
TxtPostcd = ""
TxtPlace = ""

Me.TxtSurname.SetFocus
With FrmNewClient
.Move 400, 10

End With

End Sub


So my question is, where to put the new program lines? Till now, i get invalid
 
Upvote 0
You put the code in the UserForm, which must have a CommandButton named CommandButton1. To test it enter something in the TextBox and press the CommandButton.
 
Upvote 0
Thank you guys. Here's what it became with your help.
If the postcode is valid must not be determined by a button or in the end but whenever you push enter to go forward to the next field.

Private Sub TxtPostcd_AfterUpdate()

Pc = TxtPostcd.Text
TxtPostcd = Left(Pc, 4) & " " & UCase(Right(Pc, 2))
If Not TxtPostcd.Text Like "[0-9][0-9][0-9][0-9] [A-Z][A-Z]" Then
MsgBox "Invalid code, please retry"
End If
End Sub
 
Upvote 0
Hi guys,

Here's what it became finally. Take notice of the bOk lines thanks to a Dutch helper. If the code is OK it tells the cursor to move on, if not the cursor remains in the field.

Option Explicit
Public bOk As Boolean

Private Sub TxtPostcd_AfterUpdate()
Dim pc As String
bOk = True
If TxtPostcd.Text = "" Then Exit Sub
pc = UCase(Replace(TxtPostcd.Text, " ", ""))
If Not pc Like "####[A-Z][A-Z]" Or Len(pc) <> 6 Then 'check op lengte en op opmaak
bOk = False
MsgBox "Ongeldige invoer" & vbCrLf & "" & vbCrLf & "Probeer opnieuw", vbOKOnly


TxtPostcd.Text = ""
Exit Sub
End If
pc = Left(pc, 4) & " " & Right(pc, 2)
If pc <> TxtPostcd.Text Then TxtPostcd.Text = pc
End Sub

Private Sub txtPostcd_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not bOk Then Cancel = True
End Sub

Private Sub userform_click()
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,638
Latest member
Oluwabukunmi

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