On error set a variable

yussi1870

Board Regular
Joined
Mar 18, 2002
Messages
139
Office Version
  1. 365
Platform
  1. Windows
In the following snippet of code I am setting the variable iTotalScore to a value from a website. If the value in the website doesn't exist I get a type mismatch, otherwise I get an integer which I store in my CBSImport sheet. Because I'm resuming next on error, when there is a type mismatch the iTotalScore integer remains the same from the prior loop. Instead, I'd like to set it to zero. I tried to do if(iserror(iTotalScore =.FindElementByXPath("/html/body/div[1]/div[4]/div//div[2]/div/input").Value) then iTotalScore = 0 else... But for some reason it doesn't catch it as an error there, it continues to the else and then errors out there as a type mismatch.

What I'd like to do is if there is a type mismatch then set iTotalScore to zero, store that in CBSImport and continue to the next iNameCount. Any suggestions?

For iNameCount = 1 To 8

iTotalScore = .FindElementByXPath("/html/body/div[1]/div[4]/div//div[2]/div/input").Value

On Error Resume Next

Sheets("CBSImport").Range("B" & iNameCount + 1) = iTotalScore

Next iNameCount
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Untested and assuming the rest of your code is working maybe:
VBA Code:
For iNameCount = 1 To 8
    On Error Resume Next
    iTotalScore = .FindElementByXPath("/html/body/div[1]/div[4]/div//div[2]/div/input").Value
    If Err.Number <> 0 Then iTotalScore = 0
    On Error GoTo 0
    Sheets("CBSImport").Range("B" & iNameCount + 1) = iTotalScore
Next iNameCount
 
Upvote 0
Solution

Forum statistics

Threads
1,215,537
Messages
6,125,386
Members
449,221
Latest member
DFCarter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top