我不熟悉「Like」符號,#和*他們在哪裏用過,我想在這裏進行驗證。這段代碼在網絡上,並想知道它是否應該如此。該代碼,加上一些我已經包括了一些意見:郵政編碼驗證和like notation
Function ValidPostCode(ByVal PostCode As String) As Boolean
'----------------------------------------------------------
' Deals with the postcodes of the form:
'AN NAA
'ANN NAA
'AAN NAA
'ANA NAA
'AANA NAA
'AANN NAA
'has issues with spaces and obscure entries like "England".
'Notes from Wiki::
'As all formats end with 9AA, the first part of a postcode can easily be extracted by ignoring the last three characters
'Areas with only single-digit districts: BR, FY, HA, HD, HG, HR, HS, HX, JE, LD, SM, SR, WC, WN, ZE (although WC is always subdivided by a further letter, e.g. WC1A).
'Areas with only double-digit districts: AB, LL, SO.
'Areas with a district '0' (zero): BL, BS, CM, CR, FY, HA, PR, SL, SS (BS is the only area to have both a district 0 and a district 10).
'The following central London single-digit districts have been further divided by inserting a letter after the digit and before the space: EC1–EC4 (but not EC50), SW1, W1, WC1, WC2, and part of E1 (E1W), N1 (N1C and N1P), NW1 (NW1W) and SE1 (SE1P).
'The letters QVX are not used in the first position.
'The letters IJZ are not used in the second position.
'The only letters to appear in the third position are ABCDEFGHJKPSTUW when the structure starts with A9A.
'The only letters to appear in the fourth position are ABEHMNPRVWXY when the structure starts with AA9A.
'The final two letters do not use the letters CIKMOV, so as not to resemble digits or each other when hand-written.
'Post code sectors are one of ten digits: 0 to 9 with 0 only used once 9 has been used in a post town, save for Croydon and Newport (see above).
'-----------------------------------------------------------
Dim Parts() As String
PostCode = UCase$(PostCode)
Parts = Split(PostCode)
If PostCode = "GIR 0AA" Or PostCode = "SAN TA1" Or _
(Parts(1) Like "#[A-Z][A-Z]" And _
(Parts(0) Like "[A-Z]#" Or Parts(0) Like "[A-Z]#[0-9ABCDEFGHJKSTUW]" Or _
Parts(0) Like "[A-Z][A-Z]#" Or Parts(0) Like "[A-Z][A-Z]#[0-9ABEHMNPRVWXY]")) Then
ValidPostCode = ((Parts(0) Like "[BEGLMSW]#*" Or _
Parts(0) Like "A[BL]#*" Or _
Parts(0) Like "B[ABDHLNRST]#*" Or _
Parts(0) Like "C[ABFHMORTVW]#*" Or _
Parts(0) Like "D[ADEGHLNTY]#*" Or _
Parts(0) Like "E[CHNX]#[AMNRVY]" Or _
Parts(0) Like "F[KY]#*" Or _
Parts(0) Like "G[LU]#*" Or _
Parts(0) Like "H[ADGPRSUX]#*" Or _
Parts(0) Like "I[GPV]#*" Or _
Parts(0) Like "K[ATWY]#*" Or _
Parts(0) Like "L[ADELNSU]#*" Or _
Parts(0) Like "M[EKL]#*" Or _
Parts(0) Like "N[EGNPRW]#*" Or _
Parts(0) Like "O[LX]#*" Or _
Parts(0) Like "P[AEHLOR]#*" Or _
Parts(0) Like "R[GHM]#*" Or _
Parts(0) Like "S[AEGKLMNOPRSTWY]#*" Or _
Parts(0) Like "T[ADFNQRSW]#*" Or _
Parts(0) Like "W[ACDFNRSV]#*" Or _
Parts(0) Like "UB#*" Or _
Parts(0) Like "YO#*" Or _
Parts(0) Like "ZE#*") And _
Parts(1) Like "*#[!CIKMOV][!CIKMOV]")
Else
ValidPostCode = False
End If
End Function
請,如果有人可以幫助和可能的解釋代碼徹底我將非常感謝。
謝謝
使用正則表達式可能會更容易。 –
以下是Microsoft文檔的鏈接:[VBA Like Operator](https://msdn.microsoft.com/en-us/library/office/gg251796.aspx)。 VBA文檔是一個信息和值得加書籤的金礦。 –