2016-10-31 45 views
0

我不熟悉「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 

請,如果有人可以幫助和可能的解釋代碼徹底我將非常感謝。

謝謝

+1

使用正則表達式可能會更容易。 –

+1

以下是Microsoft文檔的鏈接:[VBA Like Operator](https://msdn.microsoft.com/en-us/library/office/gg251796.aspx)。 VBA文檔是一個信息和值得加書籤的金礦。 –

回答

1

像這樣使用VB腳本正則表達式

Function VALIDATE_PCODE(strPostCode As String) As Boolean 

Dim R As RegExp 

Set R = New RegExp 

' Validate AB## and AC# post codes 

With R 
    .MultiLine = False 
    .IgnoreCase = True 
    .Global = True 
    .Pattern = "^AB\d{1,2} |AC\d{1} " ' can use [0-9] here as \d 
End With 

VALIDATE_PCODE = R.Test(strPostCode) 

Set R = Nothing 

End Function 

編輯

使用你提供的東西,你可以建立的模式有點像下面。

Function Validate_PostCode(strPostCode As String) As Boolean 

Const cstSingleDigit As String = _ 
    "BR,FY,HA,HD,HG,HR,HS,HX,JE,LD,SM,SR,WC,WN,ZE" 
Const cstDoubleDigit As String = _ 
    "AB,LL,SO" 

Dim arrTemp() As String 
Dim strRegExPattern As String 
Dim intCounter As Integer 
Dim rgeRegExp As RegExp 

' Build the Reg Ex pattern 

strRegExPattern = "^" 

arrTemp = Split(cstSingleDigit, ",") 

For intCounter = 0 To UBound(arrTemp) 

    strRegExPattern = strRegExPattern & _ 
         IIf(intCounter > 0, "|", "") & _ 
         arrTemp(intCounter) & "\d{1} " 

Next intCounter 

Erase arrTemp 

arrTemp = Split(cstDoubleDigit, ",") 

For intCounter = 0 To UBound(arrTemp) 

    strRegExPattern = strRegExPattern & "|" & _ 
         arrTemp(intCounter) & "\d{2} " 

Next intCounter 

Set rgeRegExp = New RegExp 

rgeRegExp.Global = True 
rgeRegExp.IgnoreCase = True 
rgeRegExp.Pattern = strRegExPattern 

Validate_PostCode = rgeRegExp.Test(strPostCode) 

End Function 
+0

我假設這是英國 - [這個答案](http://stackoverflow.com/a/164994/4088852)給出了正式提供的正則表達式。 – Comintern

+0

大家好,謝謝。我們是否有政府提供的最新RegEx?我們知道一個...嗎?任何人?我們知道從哪裏得到? –

1

我一般都用這個註冊防爆驗證郵政編碼(圖案總能得到改善,但這並沒有讓我失望還) - 將返回郵政編碼或#VALUE錯誤。

'Returns the postcode or #VALUE error when used as worksheet function. 
'Use the 'OR alternatives if being called from within VBA - will return TRUE/FALSE on matches. 
Public Function ValidatePostCode(strData As String) As String 'OR Boolean 
    Dim RE As Object, REMatches As Object 

    Set RE = CreateObject("vbscript.regexp") 
    With RE 
     .MultiLine = False 
     .Global = False 
     .IgnoreCase = True 
     .Pattern = "^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$" 
    End With 

    Set REMatches = RE.Execute(strData) 

    ValidatePostCode = REMatches(0) 'OR REMatches.Count > 0 

End Function 
+0

對不起Darren,最後一行似乎返回一個運行時錯誤 - 無效的過程或參數?這看起來很奇怪? –

+1

這很奇怪 - 在我的工作完全....實際上,只是檢查,因爲我通常只是用它作爲工作表函數。似乎如果從VBA內部調用,它會在無效的郵政編碼上拋出錯誤:'?ValidatePostCode(「AB1 0AAz」)'。這是因爲'REMatches'在沒有匹配的情況下返回一個0的計數,所以你不能查看第一個匹配 - 將最後一行改爲'ValidatePostCode = REMatches.Count> 0',函數返回類型爲'Boolean' - 你會得到TRUE/FALSE的迴應。我已經使用VBA替代方案更新了我的代碼。 –

+0

有一些郵政編碼不符合這種模式(正確的郵政編碼,但上面說的不一樣),我發現的郵政編碼是:W1D 4RW,W1D 4SG,W1D 4SN,W1D 5DF,W1D 5EQ W1D 5ND ...我將如何調整模式以糾正這些問題。 –