2017-12-01 4 views
0

私はいくつかのウェブサイトでいくつかのアイデンティティを特定するためにvbaにいくつかのコードを書いています。コードが正しい順序であれば、コードはうまくいきます。リンクが有効であれば、名前はaタグと一致し、最終的に正規表現はIDを見つけることができます。 3つのうちの3つまたは3つのいずれかが不良検索である場合、スクリプトはエラーをスローします。私はすでに下のスクリプトでエラーが発生する位置を指定しました。誤った結果を防ぐためにエラーを効率的に処理するにはどうすればよいですか?

私はあなたにどのような解決策を提供し、すべてのリンクが使い果たされるまで私のスクリプトをどのように続けることができるか、私にはあなたの専門家から期待しています。

私はVBAに関する多くの知識を持っていないので、私はOn error resume nextでエラーをスキップしようとしました。しかし、結果を見てみると、明らかに混乱することがわかります。私はOn error resume nextを使用したときに得られるものの大まかな例を貼り付けています。私は何を得るOn error resume nextを使用する際

Sub Identity_Finder() 

    Dim http As New XMLHTTP60, html As New HTMLDocument 
    Dim post As Object, link As Variant, refined_links As String 
    Dim rxp As New RegExp, identity As Object 

    For Each link In [{"http://spltech.in/","http://www.unifrostindia.com/","http://www.unitfrostindia.com/","http://www.greenplanet.in/"}] 
     With http 
      .Open "GET", link, False 
      .send     '''throws here the first error if the link is invalid 
      html.body.innerHTML = .responseText 
     End With 

     For Each post In html.getElementsByTagName("a") 
      If InStr(post.innerText, "certain_name") > 0 Then refined_links = post.href: Exit For 
     Next post 

     With http 
      .Open "GET", refined_links, False 
      .send       ''throws another error here if no such link is found 
     End With 

     With rxp 
      .Pattern = "some_regex" 
      .Global = True 
      Set identity = .Execute(http.responseText) 
     End With 

     r = r + 1: Cells(r, 1) = link 
     Cells(r, 2) = identity(0) ''''throws another error here if no such identity is noticed 

    Next link 
End Sub 

John executive 
Mac lawyer 
lulu lawyer 
Robin lawyer 
Cathy student 

予想される出力:

John executive 
Mac lawyer 
lulu 
Robin 
Cathy student 

空のフィールド(彼らが発見されていない)以前の値で埋めなっています私はOn error resume nextを使用します。この誤解を招く結果を回避するにはどうすればよいですか?前もって感謝します。

+0

ロジックを実行する場所を確認するためにコードをステップバイステップで実行しましたか? 'F8'を押してコードを起動し、もう一度コードの次の行に移動すると...など。ロジックの問題をデバッグするのに最適です:) – Maldred

+0

はい何度もデバッグしても問題はありませんリンクは無効ではなく、名前は 'a'タグと一致し、regexは自分の投稿ですでに説明したアイデンティティーを見つけました。しかし、3つのうちのいずれかが検索と一致しないときにエラーが発生し、エラーが発生したスクリプト内の位置をすでにマークしています。ありがとう。 – SIM

答えて

0

VBAでerror trapに最も効率的な方法は、実際にはカスタムメイドの機能または介してそれらのいずれかを実行する前に、入力/結果をテスト

1)にある内蔵の概念、またはその両方の組み合わせを符号化します。

2)使用VBA組み込みエラー処理絶対例えば

例1

が必要な場合。このステートメントをカスタム関数でラップして、URLが有効かどうかをテストできます。

With http 
    .Open "GET", link, False 
    .send     '''throws here the first error if the link is invalid 
    html.body.innerHTML = .responseText 
End With 

If ValidURL Then 

    With http 
     .Open "GET", link, False 
     .send  
     html.body.innerHTML = .responseText 
    End With 

End If 
ValidURLのように定義された関数である

Function ValidURL(URL as String) as Boolean 

    Dim result as Boolean 
    'I don't know how you would specify a valid link in your specific case 
    'but that code goes here 
    'a dummy example follows 
    result = Left(URL,7) = "http://" 
    ValidURL = result 'True or False 

End Function 

例2

私はこの文で仮定:

With http 
     .Open "GET", refined_links, False 
     .send       ''throws another error here if no such link is found 
    End With 

特定のエラー数があります(コード)が生成されます。その番号を発見し、このコードをバイパスする。

With http 
    .Open "GET", refined_links, False 
    On Error Resume Next 
    .Send 
    On Error GoTo 0 
End With 

If err.Number <> 9999 'replace with correct number 

    'continue with regex test 

End If 

が最後にOn Error Resume Nextの最小限の使用と、すべて一緒にあなたがそうのように構築することができることを入れていないと何もGoTo文ALL TOGETHERそれを入れ

For Each link In [{"http://spltech.in/","http://www.unifrostindia.com/","http://www.unitfrostindia.com/","http://www.greenplanet.in/"}] 

    If ValidURL(link) Then 

     With http 
      .Open "GET", link, False 
      .send  
      html.body.innerHTML = .responseText 
     End With 

     For Each post In html.getElementsByTagName("a") 
      If InStr(post.innerText, "certain_name") > 0 Then refined_links = post.href: Exit For 
     Next post 

     With http 
      .Open "GET", refined_links, False 
      On Error Resume Next 
      .Send 
      On Error GoTo 0 
     End With 

     If err.Number <> 9999 'replace with correct number 

      With rxp 
       .Pattern = "some_regex" 
       .Global = True 
       Set identity = .Execute(http.responseText) 
      End With 

      'i will leave it to you on how to account for no pattern match 
      r = r + 1: Cells(r, 1) = link 
      Cells(r, 2) = identity(0) ''''throws another error here if no such identity is noticed 

     End If 

    End If 

Next link 
+0

あなたが '.Send'を呼び出すまで、リンクが有効かどうかを判断する方法がない場合は、例2と同じ方法でエラーをトラップすることができます。 –

+0

あなたのコードは外観がすごく素敵です。私はチェックを済ませたらあなたに戻ってきます。あなたの時間と努力のおかげで@Scott Holtzmanに感謝します。 – SIM

関連する問題