这是再次停止和执行我的潜艇的正确方法吗?

时间:2017-12-12 06:14:24

标签: excel vba if-statement

我在下面运行我的代码时得到奇怪的调试信息,基本上在执行代码时遇到了条件,但是当它停止并且我调试它时,这些条件不满足且不应该停止。

我对我的代码中的第一个IF语句有疑问。基本上我正在检查条件是否满足(在我的json数据中收到“消息”,如果满足,我想再次停止并启动我的子(交易)。

这是正确的方法吗?

    Sub Trades()

Set MyRequest = CreateObject("WinHTTP.WinHTTPRequest.5.1")
MyRequest.Open "GET", "https://api.gdax.com/products/ETH-EUR/trades"
MyRequest.Send

Set ParsedTrades = JsonConverter.ParseJson(MyRequest.ResponseText)

'to try to fight intermitent bug
Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(19, "U") = MyRequest.ResponseText
WaitTime (2)

'check if did not receive error string from exchange, if so, start again (i should add logger here)
Dim pos As Integer
pos = InStr(MyRequest.ResponseText, "message")
If pos > 0 Then
Trades
End If

    'to speed up execution when looping
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False

For i = 1 To 100

        'check if new tradeid > last tradeid, if so, copy
        LastTradeId = Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(2, "B")
        If ParsedTrades(i)("trade_id") > LastTradeId Then

            Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 15) = ParsedTrades(i)("time")
            Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 16) = ParsedTrades(i)("trade_id")
            Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 17) = ParsedTrades(i)("price")
            Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 18) = ParsedTrades(i)("size")
            Workbooks("GDAX Order book and trades.xlsm").Sheets("trades").Cells(i + 1, 19) = ParsedTrades(i)("side")

        End If

1 个答案:

答案 0 :(得分:0)

也许是这样的:

Dim pos As Integer
pos = InStr(MyRequest.ResponseText, "message")
If pos > 0 Then
    'Error: wait a couple of seconds and then try again
    Application.OnTime Now + TimeValue("00:00:02"), "Trades"
    Exit Sub
End If