gpt4 book ai didi

excel - 当 'On Error Resume Next' 将先前的值留在变量中时,如何返回空白或零?

转载 作者:行者123 更新时间:2023-12-04 21:29:07 25 4
gpt4 key购买 nike

我有一个循环获取股票价格的 VBA 代码。

在此 API 源中找不到股票代码。这将导致这些股票出错。

我正在使用 On Error Resume Next所以 VBA 代码将继续到下一个符号。

我的问题是错误的符号将返回最后一个没有错误的股票代码的股票价格。

我想将错误的股票代码的结果设为空白或零。

当前结果 - 斜体符号是错误的。

股票代码 价格
BDO 158.00
ABS 15.80
绿色 1.87
阿尔希 1.87
土地 1.87
可编程逻辑 Controller 0.57
磅值 0.57
0.57

期望的结果 - 错误的斜体符号将导致或返回 0

股票代码 价格
BDO 158.00
ABS 15.80
绿色 1.87
0
土地 0
可编程逻辑 Controller 0.57
低密度脂蛋白 0
曾经 0

    Set myrequest = CreateObject("WinHttp.WinHttpRequest.5.1")
myrequest.Open "Get", "http://phisix-api.appspot.com/stocks/" & symbol & ".json"
myrequest.Send

Dim Json As Object
Set Json = JsonConverter.ParseJson(myrequest.ResponseText)

i = Json("stock")(1)("price")("amount")
ws.Range(Cells(n, 2), Cells(n, 2)) = i

n = n + 1

On Error Resume Next

Next X

ws.Columns("B").AutoFit
MsgBox ("Stock Quotes Refreshed.")

ws.Range("B4:B" & lastrow).HorizontalAlignment = xlGeneral
ws.Range("B4:B" & lastrow).NumberFormat = "#,##0.00"

Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

最佳答案

您的代码设置 On Error Resume Next在第一次循环结束时,从那时起忽略任何和所有错误。那很糟。

使用 OERN 的一般方法应该是

Other non error causing code
Optionally initialise variables in preparation for error trap
On Error Resume Next
Execute the instruction that might cause an error
On Error GoTo 0
If (test if instruction succeeded) Then
Process the result
Else
Optionally handle the error case
End If

关于excel - 当 'On Error Resume Next' 将先前的值留在变量中时,如何返回空白或零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59550283/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com