So I understand there are probably far better solutions to this but I am attempting to teach myself VBA from scratch with no prior programming experience so am trying to find my own solutions to problems until I get stuck e.g. now.

I am trying to get a textbox on a userform to check if a postcode is in the correct format for a UK postcode. If it is then no action is needed. If it is correct but missing the space then a space should be added, and if it is wrong completely then a message box should prompt the user to check it.

The following are the rules for a UK postcode.

FORMAT ---------EXAMPLE
AN NAA---------- M1 1AA
ANN NAA--------- M60 1NW
AAN NAA ---------CR2 6XH
AANN NAA-------- DN55 1PT
ANA NAA ----------W1A 1HQ
AANA NAA ---------EC1A 1BB


-The letters Q, V and X are not used in the first position
-The letters I,J and Z are not used in the second position.
-The only letters to appear in the third position are A, B, C, D, E, F, G, H, J, K, S, T, U and W.
- The second half of the postcode is always consistent numeric, alpha, alpha format and the letters C, I, K, M, O and V are never used.
Postcodes should always be in BLOCK CAPITALS as the last line of an address. Do not underline the postcode or use any punctuation. Leave a clear space of one character between the two parts of the postcode and do not join the characters in any way.

The code I have used so far is below and has several problems.
1) I have no idea if the ? character will work or not.
2) I have another Sub_Change Event which is just 1 line ensuring all text is uppercase.
3) I just get an error when opening the userform "Compile Error: Procedure declaration does not match description of event or procedure having the same name". - I have no idea what this means.
4) Probably several other issues but I have not been able to test it so far since I just get the error.

I have tried various things, making this part of the change event mentioned above, making this a 'Lost Focus' event, altering the wording of various parts of the code etc. But, as I am not sure what the error is referring to I don't really know where to start in terms of trying to fix it. Anyway, here is what I have.


  

Private Sub TextBox8_Exit()
    
    If TextBox8.Text = Len(0) Then Exit Sub
    
    End If
    
    Dim PC As String
    
    PC = Trim(TextBox8.Text)
    
    'Case 1 Correct     AN NAA     M1 1AA
    If PC Like "[A-PR-U-WY-Z]#Chr(32)#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    Exit Sub
    
    'Case 2 Correct     ANN NAA     M60 1NW
    ElseIf PC Like "[A-PR-U-WY-Z]##Chr(32)#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    Exit Sub
    
    'Case 3 Correct     AAN NAA     CR2 6XH
    ElseIf PC Like "[A-PR-U-WY-Z][A-HK-Y]#Chr(32)#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    Exit Sub
         
    'Case 4 Correct     AANN NAA     DN55 1PT
    ElseIf PC Like "[A-PR-U-WY-Z][A-HK-Y]##Chr(32)#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    Exit Sub
    
    'Case 5 Correct      ANA NAA     W1A 1HQ
    ElseIf PC Like "[A-PR-U-WY-Z]#[A-HJ,K,S-U,W]Chr(32)#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    Exit Sub
    
    'Case 6 Correct     AANA NAA     EC1A 1BB
    ElseIf PC Like "[A-PR-U-WY-Z][A-HK-Y]#[A-Z]Chr(32)#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    Exit Sub
    
    
    
    'Case 1 Incorrect      AN NAA     M1 1AA
    
    ElseIf PC Like "[A-PR-U-WY-Z]##[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    TextBox8.Text = Format(PC, "??" & " " & "???")
    
    
    'Case 2 Incorrect     ANN NAA     M60 1NW
    ElseIf PC Like "[A-PR-U-WY-Z]###[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    TextBox8.Text = Format(PC, "???" & " " & "???")
    
    
    'Case 3 Incorrect      AAN NAA     CR2 6XH
    ElseIf PC Like "[A-PR-U-WY-Z][A-HK-Y]##[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    TextBox8.Text = Format(PC, "???" & " " & "???")
    
       
    'Case 4 Incorrect     AANN NAA     DN55 1PT
    ElseIf PC Like "[A-PR-U-WY-Z][A-HK-Y]###[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    TextBox8.Text = Format(PC, "????" & " " & "???")
    
    
    'Case 5 Incorrect     ANA NAA     W1A 1HQ
    ElseIf PC Like "[A-PR-U-WY-Z]#[A-HJ,K,S-U,W]#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    TextBox8.Text = Format(PC.Text, "???" & " " & "???")
    
    
    'Case 6 Incorrect     AANA NAA     EC1A 1BB
    ElseIf PC Like "[A-PR-U-WY-Z][A-HK-Y]#[A-Z]#[A-BD-H,J,L,N,P-UW-Z][A-BD-H,J,L,N,P-UW-Z]" Then
    
    TextBox8.Text = Format(PC.Text, "????" & " " & "???")
    
    Else: MsgBox "Invalid format for UK Postcode entered." & vbCrLf & _
    "Please check and try again.", vbOKOnly + vbExclamation, "Invalid Format!"
    
    End If
   
       
End Sub