Home » excel » excel – How to return blank or zero when 'On Error Resume Next' leaves previous value in variable?

excel – How to return blank or zero when 'On Error Resume Next' leaves previous value in variable?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have VBA code that gets the stock price in a loop.

There are stock symbols not found in this API source. It will result to error for those stocks.

I’m using On Error Resume Next so the VBA code will continue to the next symbol.

My problem is the symbol in error will return a stock price of the last stock symbol not in error.

I would like to make the result blank or zero for the stock symbols in error.

Current Result – the italicized symbols are those that are in error.

Stock Symbol Price
BDO 158.00
ABS 15.80
GREEN 1.87
ALHI 1.87
LAND 1.87
PLC 0.57
LBC 0.57
EVER 0.57

Desired Result – the italicized symbols those that are in error will result or will give return of 0

Stock Symbol Price
BDO 158.00
ABS 15.80
GREEN 1.87
ALHI 0
LAND 0
PLC 0.57
LBC 0
EVER 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
How to&Answers:

You can always put a conditional check statement based on the error you receive for invalid stocks, if you are getting empty value in myrequest on invalid stock. You can write your logic like below and update price value to 0.

If myrequest is Nothing Then
    'For Invalid Stocks
End

or

If myrequest.ResponseText = "" Then
    'For Invalid Stocks
End

Let me know if it helps. Otherwise share the JSON response for both valid and invalid stocks.

Update:
Based on the value of myrequest.ResponseStatus for invalid response, update the <add-condition> condition in If statement according to your requirement.

For Each X In rng

    Dim Json As Object
    Dim i
    symbol = X

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

    Debug.Print myrequest.ResponseStatus

    If <add-condition> Then
        Set Json = JsonConverter.ParseJson(myrequest.ResponseText)
        i = Json("stock")(1)("price")("amount")
    Else
        i = 0
    End If

    ws.Range(Cells(n, 2), Cells(n, 2)) = i
    n = n + 1

Next X

Answer:

Your code as it stands sets On Error Resume Next at the end of the first time through the loop, and from that point on ignores any and all errors. That’s bad.

The general method of using OERN should be

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