2016-12-27 257 views
0
Private Sub btnsubmit_Click() 

Dim Msg As String 
Dim pos1 As Integer 
Dim pos2 As Integer 
Dim Count As Integer 
Dim flag As Integer 
Dim telphno  

Msg = TextBox1.Value 
pos1 = 1 
pos2 = 1 

flag = 0 

Do While pos1 < Len(Msg) 

    pos1 = InStr(pos1, Msg, "[") 
    If flag = 0 Then 
     pos2 = InStr(pos2, Msg, "]") 
    End If 

    If pos2 - pos1 < 5 Then 
     ActiveCell.Value = Mid(Msg, pos1 + 1, pos2 - pos1 - 1)   
     Count = 0 

     'Loop through the entire string 
     For i = pos2 To Len(Msg) 

      'Check to see if the character is a numeric one 
      If IsNumeric(Mid(Msg, i, 1)) Then 

       'Add it to the answer 
       telphno = telphno + Mid(Msg, i, 1) 
       Count = Count + 1      

       'Check to see if we have reached 10 digits 
       If Count = 10 Then Exit For       

      Else 
       telphno = "" 
       Count = 0     
      End If 

     Next i 

     ActiveCell.Offset(0, 1).Value = telphno 
     flag = 0 
     ActiveCell.Offset(1, 0).Select 
     pos1 = pos2 + 1 
     pos2 = pos1 
     telphno = ""   
    Else 
     flag = 1 
     pos1 = pos1 + 1    
    End If 

Loop 

End Sub 

I am getting error on using MID function highlighted in yellow.運行時間錯誤:5無效的過程調用或參數在VBA

我想在我的字符串來獲取「[」和「]」之間的字符,但是MID的功能是給我的錯誤。請幫我解決這個問題。

找到「[」和「]」之間的字符後,我正在尋找最近的10位數字。我正在通過TextBox(用戶輸入)獲取字符串。 並在Excel工作表上打印輸出。

爲前

[A22]1239163332bcfhds[B23]6453jhddf2784637281ajdnjda[C33]dksamkd1288776655 

對於上述字符串。它應該給 -

A22 1239163332 
B23 2784637281 
C33 1288776655 

輸入2 -

@fiJaeasafiGpaaaaod [A1] # 42, 5532 23156 「63’ 8:355 dedmaa @656 663336, {33538365. sail & as" 53666 wee—9008799499. @dfis.ée.maae06 [A2] S/O éegddad mweefi @dfiE aaseaefi mwaossae We" flees?) all 359% as" fidee, wee—9886557596. $036903: WI 29365 amass @aoflae mafia] (me) sail was?「 asllmairid’, aha—9945173528. @6066 60333305 2:056. [A4] £06333 messes $6503.91: 33:12:05 mews. 8.133133% a?" aiding as" mamas. aha-9886444737. [50%. memergfios, [A5] mew 89335365 mamas. mama. aan wage, as" eagdade, diam—9731742667. eaaiodfiaas. [A6] ge Wagfiegd mwaofisae mafia) £3966.mafia).sail mg"), as" mairifi, met—9986611558. @8396 30653236 [A7] fleas $839395 fleas# 4138/38, Sgéegd 33905:,623 finaSeag soomrf,ao.&.&.‘w’ea§ss,$3913.29. fideaas 6:312:36,anaemia.all dog as" macaw-36,mom—9448166197.mamas 8985305 [A8192 ageng mews sambaaadefiaefiéfieo.all adswsg, all warm,dam—[email protected] games [A9][email protected]&.dsaec56maisafid.aall $33.19 as" mamrid’,wag—9844644272.as.demam] 33mmadésada.Gall 836%62, as" $3668, wee:—8.839%:ngge [imam meme $36033「flaccid weave.mweafid.all 623; as" maiarid,dam—9481161243.agodaé.8.ao.129330556 agaossae53%;.a?" & as" 5365366,mez—[email protected]@[email protected]:330:36 @9033「we ems aim-.1353,swag.all 666663, as" maladdwag—8123565686.gang-56$60506 masses fiaofiaemafifleas mews,swag.all 8665383, as" mdwfid aha—9845781954.[A10][A11][A12][A13][A14]$63535 3:633:56 [A15]@663 aaaaaefi SawadJaeawe: aa£eas€ mews,{3368366.sail 6.9% all 0369966,dam—9945707587eaoadfi mesmereaa’cfiwsl6:36:38 agaoisaefiasfiewéédaall 303:3, as" 03653613,dam-9900436152.$830335eiedafi $885366 Qawaeeiapflsaecss,asasdne.aall wipe, as" 53566,Elma—9448218974.$69836 memergfios [A18]# 167,「&oa5555」15’ 2:336, 63:: @55,deg-353255 9:355 acme-3%,massarifi.aall 3.3.333 as」 nae-36386,WEE—[email protected]%§ mewaergfics [A19]# 794/8 oomfieo.modes mews,65366666.all 6333 as" mwwfi'fi,dam—9945434802.$830535 Meme®$cs [A20]@30335 masses Mathewsmandaoddwsg.sail fine]; as" 53538363,[email protected] memergaos903361103: $358365 Saaawsae3536333.Ball 36335623, as" $368966,Wei-9972675782[A17] [A21]

預期輸出繼電器 -

A1 9008799499
A2 9886557596
A4 9886444737
A5 9731742667
A6 9986611558
A7 9448166197
並以此類推,直到
A20 9980170633

+2

上傳你的代碼的代碼,所以我們可以用它(而不是作爲圖像) –

+0

我猜想,'pos2'爲零時墜毀 - 發表您的代碼,我們可以幫助你弄清楚什麼是錯誤的。 – YowE3K

+0

也可以縮短調試時間,如果您讓我們知道在出現此錯誤時您在「TextBox1」中輸入了什麼值 –

回答

1

您的代碼使用RegEx「尖叫」,請參閱下面的代碼(代碼註釋內容)。

子btnSubmit按鈕代碼

Option Explicit 

Private Sub btnsubmit_Click() 

Dim Msg As String 
Dim pos1 As Integer 
Dim pos2 As Integer 
Dim posDelta As Integer 
Dim telphno 
Dim i As Integer 

' added these 2 variables 
Dim insideBrackets As String 
Dim telphnoPos As Integer 

TextBox1.Value = Sheets("sheet2").Range("H5").Value 
Msg = TextBox1.Value 

pos1 = 1 
pos2 = 1 

' loop while Msg still no emptied out 
Do While (Msg) <> "" 
    TextBox1.Value = Msg 
    Debug.Print Len(Msg) 

    pos1 = InStr(Msg, "[") 
    pos2 = InStr(Msg, "]") 

    ' find number of characters between "[" and "]" 
    posDelta = pos2 - pos1 

    Select Case posDelta 
     Case Is < 0 ' only "]" found , and no "[" 
      Msg = Right(Msg, Len(Msg) - pos2) 

     Case 3, 4 ' could be A# , or A## 
      insideBrackets = Mid(Msg, pos1 + 1, pos2 - pos1 - 1) 

      telphno = "" ' reset value 
      telphnoPos = 0 

      Msg = Right(Msg, Len(Msg) - pos2) 

      ' call function with Regex to find first 10 digits in string 
      telphno = GetFirstTenDigits(Msg) 

      ' find position of first 10 digits inside the string 
      If telphno <> "" Then telphnoPos = InStr(Msg, telphno) 

      ' successfult 10-digit resulted from RegEx 
      If telphnoPos > 0 Then 
       ActiveCell.Value = insideBrackets 
       ActiveCell.Offset(0, 1).Value = telphno 

       ' remove characters from string that were extracted to the cells 
       Msg = Right(Msg, Len(Msg) - (telphnoPos + 10 - 1)) 
       insideBrackets = "" ' reset value 

       ' advance 1 row 
       ActiveCell.Offset(1, 0).Select 
      Else 
       Msg = "" ' no 10 digits ccurrences left 
      End If 

     Case Is > 4 
      Msg = Right(Msg, Len(Msg) - pos1) 

    End Select 
Loop 

End Sub 

***Function GetFirstTenDigits* Code** (uses the `Regex` object) 

Function GetFirstTenDigits(byMixedString As String) As String 

' this function uses the RegEx to find all numeric characters insde the passed string 
' then it searches for the first occorunce that the number of digits = 10 , 
' and returns it to the calling Sub 

    Dim RegEx As Object, Matches As Object, Match As Object 

    Set RegEx = CreateObject("vbscript.regexp") 
    With RegEx 
     .MultiLine = False 
     .Global = True 
     .IgnoreCase = True 
     .Pattern = "(\d+)" ' Match any set of digits 
    End With 

    Set Matches = RegEx.Execute(byMixedString) 

    For Each Match In Matches 
     If Len(Match) = 10 Then 
      ' return the first match of 10 digits 
      GetFirstTenDigits = Match 
      Exit Function 
     End If 
    Next Match 

End Function 
+0

下面是什麼?你想測試什麼輸入?我沒有包含在我的代碼錯誤hanlding中,有錯誤處理的infinte選項。 –

+0

爲此輸入獲取相同的錯誤 - ésaeasmeme。 [A671 8353698個媽媽們AO音響米 FL OCKS馬廄,03338366. 爲 「= 3:一個爲」 maisedd, AHA-9731432004。 fi saeas 33.8)。 [A681 aesofnge imam Egao fi sae eadrboa。 球domais爲 「年齡3, 凌晨-9449343829。 6353 $三十三萬五千三百六十六AO FL我們[A69] 35393389655網絡OE科幻克, 8:55 50135主題, 檯面。 爲」 壩, AHA-9448640505 。 [A70] 533氣體agaoisae&aeaoisae 6355306 $ 3665,853333933, 65365366. 一個35:33的」 mwad音響, AHA-8495819232。 8535模因536 [A71] 音響ANCE 92%9時33分:33, TM 853ech 可以577217 爲」梅斯音響d, AHA-9141629909。 –

+0

問題是您的代碼正在解決該輸入的問題,但不是針對上述輸入。在同一行上獲取相同的運行時錯誤。 –

-1

時,有沒有更多的[...]對剩餘你的問題發生,因爲你沒有退出循環。

如果更改

pos1 = InStr(pos1, Msg, "[") 

立即有下面的代碼後,我覺得你的問題就會迎刃而解:

If pos1 = 0 Then 
    Exit Do 
End If 

你的pos2計算也應該從改變

pos2 = InStr(pos2, Msg, "]") 

pos2 = InStr(pos1, Msg, "]") 

這將確保你在當前[後拿起第一]的位置。


這兩個改變後,你的代碼正確處理兩者現在在你的問題給出的例子...除了用於寫出記錄後在第二個例子[A17][A21] - 我不知道是否你真的希望他們被忽略,或者你的第二個例子是否在源數據的中途被截斷。

相關問題