2016-05-15 67 views
0

株価に基づいてティッカーを見つけるためのスクリプトを書いています。当然のことながら、企業は書かれた方法で見つからないこともあります。この理由は、通常、ソースが最後に不必要なビットをつかむことです。私のスクリプトを書こうとしているので、テロップが終わるまで単語の末尾から繰り返し削除されます。ティッカーが決して得られない場合は、次を再開し、次の行を選択します。これを行う方法はありますか? (エラーがなくなるまでループを)あなたが記述問題についてはエラーがなくなるまでタスクを繰り返すvba

Sub TickerLookup() 

cell = ActiveCell.Value 

10 If Not IsEmpty(ActiveCell.Value) Then 
    GoTo GetTicker 
    Else 
    GoTo 20 
End If 
Catch: 
    For Each c In Selection 
     If Not IsEmpty(c.Offset(, -1)) Then 
      cell = Left(cell, InStrRev(cell, " ") - 1) 
      MsgBox cell 
     Else 
      MsgBox "Please clear all adjacent cells" 
     End If 
Next 
Resume Next 
Selection.Offset(1, 0).Select 
GoTo 10 

GetTicker: 

    cell = ActiveCell.Value 
    'remInc = Replace(cell, "INC", "") 
    'remCos = Replace(remInc, "COS", "") 
    cmpnyName = cell 

    ticker = "http://dev.markitondemand.com/MODApis/Api/v2/Lookup/json?input=" & cmpnyName 

    Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1") 
     MyRequest.Open "GET", ticker 
     MyRequest.Send 
    Dim Json As Object 
     Set Json = JsonConverter.ParseJson(MyRequest.ResponseText) 


    On Error GoTo Catch 
    ActiveCell.Offset(, -1).Value = Json(1)("Symbol") 

    Selection.Offset(1, 0).Select 
    GoTo 10 

20 End Sub 
+2

「Goto' ??本当に?それはsoooo 80年代です... – trincot

+0

これはちょっとしたXY問題のようですが、あなたは 'Err'オブジェクトをテストしてそれに応じて行動する必要があります。代わりにデータをテストする方がはるかに良い方法があると思います。 –

答えて

0

解決策は以下のとおりです。また、

Do 
    On Error Resume Next 'reset Err.obj. 
    'do things you want here 
    If (BoolExpressionYoWantIsTrue) Then 'the test you mention: the string is OK 
     'do what you want 
     Exit Do 'quit loop 
    End If 
Loop Until (Err.Number = 0) 
On Error Goto 0 'turn off error sinking 

、「後藤」という表現を使って、これまでせずにコードにしてみてください。たとえば、「Goto 20」の代わりに、代わりにExit Subを追加することもできます。

関連する問題