重复任务,直到没有更多错误vba [英] repeat task until no more error vba

查看:59
本文介绍了重复任务,直到没有更多错误vba的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,以根据股票名称查找股票行情.自然,有时找不到公司的编写方式.原因是源通常会在最后添加一些不必要的位.我试图写我的脚本,以便它将重复地从末尾删除单词,直到获得报价器为止.如果行情自动收录器从未获得过,请继续选择下一个并选择下一行.有办法吗?

I'm writing a script to find tickers based on stock names. Naturally, sometimes companies are not found the way they are written. The reason for this is the source usually tacks on some unneccesary bit at the end. Im trying to write my script so that it will repeatedly remove words from the end until the ticker is gotten. If the ticker is never gotten, then to Resume Next and select the next row. Is there a way to do this?

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

推荐答案

对于您描述的问题(直到错误消失,一直循环),解决方案是:

For the issue you describe (loop until error is gone) the solution is:

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"表达式在没有 ever 的情况下进行编码.例如,代替"Goto 20",您可以添加 Exit Sub .

Also, try to code without ever using the "goto" expression. For example, instead of that "Goto 20" you could just add an Exit Sub instead.

这篇关于重复任务,直到没有更多错误vba的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆