Resetting the error handling status - ON Error GO TO 0

Excel VBA for Beginners 11- Error Handling
4 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€86.76
List Price:  €121.48
You save:  €34.71
£74.67
List Price:  £104.54
You save:  £29.87
CA$138.09
List Price:  CA$193.33
You save:  CA$55.24
A$155.63
List Price:  A$217.90
You save:  A$62.26
S$130.40
List Price:  S$182.57
You save:  S$52.16
HK$775.99
List Price:  HK$1,086.42
You save:  HK$310.43
CHF 80.70
List Price:  CHF 112.99
You save:  CHF 32.28
NOK kr1,041.35
List Price:  NOK kr1,457.94
You save:  NOK kr416.58
DKK kr647.86
List Price:  DKK kr907.02
You save:  DKK kr259.16
NZ$166.43
List Price:  NZ$233.01
You save:  NZ$66.58
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳12,143.18
List Price:  ৳17,000.94
You save:  ৳4,857.76
₹8,513.11
List Price:  ₹11,918.70
You save:  ₹3,405.58
RM436.95
List Price:  RM611.75
You save:  RM174.80
₦160,437.95
List Price:  ₦224,619.55
You save:  ₦64,181.60
₨28,143.44
List Price:  ₨39,401.94
You save:  ₨11,258.50
฿3,308.36
List Price:  ฿4,631.84
You save:  ฿1,323.48
₺3,819.79
List Price:  ₺5,347.87
You save:  ₺1,528.07
B$593.14
List Price:  B$830.42
You save:  B$237.28
R1,868.36
List Price:  R2,615.78
You save:  R747.42
Лв172.12
List Price:  Лв240.97
You save:  Лв68.85
₩141,905.31
List Price:  ₩198,673.11
You save:  ₩56,767.80
₪368.88
List Price:  ₪516.44
You save:  ₪147.56
₱5,656.38
List Price:  ₱7,919.16
You save:  ₱2,262.78
¥14,097.29
List Price:  ¥19,736.77
You save:  ¥5,639.48
MX$1,965.48
List Price:  MX$2,751.75
You save:  MX$786.27
QR364.29
List Price:  QR510.02
You save:  QR145.73
P1,377.36
List Price:  P1,928.36
You save:  P551
KSh12,961.55
List Price:  KSh18,146.69
You save:  KSh5,185.13
E£5,044.66
List Price:  E£7,062.73
You save:  E£2,018.06
ብር13,323.32
List Price:  ብር18,653.18
You save:  ብር5,329.86
Kz91,190.88
List Price:  Kz127,670.88
You save:  Kz36,480
CLP$96,756.32
List Price:  CLP$135,462.72
You save:  CLP$38,706.40
CN¥728.52
List Price:  CN¥1,019.96
You save:  CN¥291.44
RD$6,027.39
List Price:  RD$8,438.59
You save:  RD$2,411.20
DA13,263.54
List Price:  DA18,569.48
You save:  DA5,305.94
FJ$228.55
List Price:  FJ$319.98
You save:  FJ$91.43
Q770.07
List Price:  Q1,078.13
You save:  Q308.06
GY$20,943.20
List Price:  GY$29,321.32
You save:  GY$8,378.12
ISK kr12,590.74
List Price:  ISK kr17,627.54
You save:  ISK kr5,036.80
DH924.24
List Price:  DH1,293.98
You save:  DH369.73
L1,729.79
List Price:  L2,421.77
You save:  L691.98
ден5,365.75
List Price:  ден7,512.27
You save:  ден2,146.51
MOP$799.27
List Price:  MOP$1,119.02
You save:  MOP$319.74
N$1,868.61
List Price:  N$2,616.13
You save:  N$747.52
C$3,677.16
List Price:  C$5,148.17
You save:  C$1,471.01
रु13,625.88
List Price:  रु19,076.78
You save:  रु5,450.89
S/374.14
List Price:  S/523.81
You save:  S/149.67
K413.25
List Price:  K578.58
You save:  K165.32
SAR375.18
List Price:  SAR525.27
You save:  SAR150.08
ZK2,850.34
List Price:  ZK3,990.59
You save:  ZK1,140.25
L431.61
List Price:  L604.28
You save:  L172.66
Kč2,173.88
List Price:  Kč3,043.52
You save:  Kč869.64
Ft35,338.69
List Price:  Ft49,475.58
You save:  Ft14,136.89
SEK kr951.25
List Price:  SEK kr1,331.79
You save:  SEK kr380.53
ARS$112,282.22
List Price:  ARS$157,199.61
You save:  ARS$44,917.38
Bs691.72
List Price:  Bs968.44
You save:  Bs276.71
COP$424,576.35
List Price:  COP$594,423.88
You save:  COP$169,847.52
₡50,228.06
List Price:  ₡70,321.30
You save:  ₡20,093.23
L2,586.88
List Price:  L3,621.73
You save:  L1,034.85
₲799,721.01
List Price:  ₲1,119,641.41
You save:  ₲319,920.40
$U4,234.41
List Price:  $U5,928.35
You save:  $U1,693.93
zł371.12
List Price:  zł519.58
You save:  zł148.46
Already have an account? Log In

Transcript

Hey guys, welcome back. So in this video, we're going to discuss the on error go to zero statement. So the on error go to zero, what it does is that it resets the error handling status in your VBA code to the default status, which is basically throwing an error. So in case that you do not do any error handling, what's going to happen is that when VBA encounters an error, it's just going to throw the runtime error on the message box and you're going to see a message box that says, runtime error. This is the default error handling status in Excel VBA. And what this is useful for is if we have different statements that can potentially cause errors on our code, and we want to treat each statement differently or handle the error that could potentially be caused by each statement differently.

So for example here on this example, we've got the line of code worksheets my worksheet, select. This could potentially throw an error. However, if we run it right now, while the worksheet that is called my worksheet is created, it will not throw an error. And we've got the statement here on error go to create my worksheet. But what if I put another line of code that will create an error, so I put range a one equals one divided by zero, so this will create a division by zero error. Now watch what happens when we run this line of code.

So let's start running the macros step by step. So this line of code will not cause an error for activating the worksheet called my worksheet because it already exists. However, when we run this line of code, it will actually go to the create my worksheet, because remember, guys when I told you that when we write an on our statement We'll actually start a status of error handling for any statement that follows this statement. So as you can see here, if we run this line of code, and it will cause an error, it will actually go to create my worksheet and it will create a worksheet that is called my worksheet while we do actually have a worksheet already, so this will throw an error. So actually, this error was handled using the same way as the error that would potentially be occurring from this statement. And this is something that we do not want to do because this is division by zero error.

It's not related to creating a worksheet, so we would want to handle it differently. So if we add a statement here on error, go to zero. This statement resets the error handling status in your VBA code. So now we will go to the default error handling status which is actually that if we encounter an error Here we're going to have a division by zero error and then we can actually put another type of error handling so on our go to message box, for example, and then we would have here a message box placeholder here and we would have a message box, you cannot divide by zero. And it is better as well to put an on error go to zero statement here so that any statement after that one here would not be handled the same way unless you have more statements that can be handled the same way.

Or that can take the same error handling guys on our go to message box so you can put them here in between the on error go to message box on the on error go to zero. So as you can see here, we've used the honor go to zero statement here to reset the error handling status so as to go back to the default status and start at different status for handling this different error. So as you can see here, if we run the code right now worksheets my worksheet does so like so this will not cause an error and then we go to the default error handling status and we start a new error handling status which is to go to the message box. So now this statement causing an error will go to the message box you cannot divide by zero and then we will reset our handling status again and exit the sub so we use the honor go to zero statement to reset our error handling status because not all errors should be handled the same way.

So that's it guys for the on our go to zero statement. Thank you very much for watching this video and I'll see you on the next one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.