Error Handling - Introduction

Excel VBA for Beginners 11- Error Handling
9 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
€92.84
List Price:  €129.98
You save:  €37.14
£79.48
List Price:  £111.28
You save:  £31.79
CA$136.74
List Price:  CA$191.45
You save:  CA$54.70
A$150.88
List Price:  A$211.24
You save:  A$60.36
S$135.02
List Price:  S$189.03
You save:  S$54.01
HK$781.40
List Price:  HK$1,093.99
You save:  HK$312.59
CHF 90.57
List Price:  CHF 126.80
You save:  CHF 36.23
NOK kr1,084.75
List Price:  NOK kr1,518.70
You save:  NOK kr433.94
DKK kr692.49
List Price:  DKK kr969.51
You save:  DKK kr277.02
NZ$166.11
List Price:  NZ$232.56
You save:  NZ$66.45
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,956.06
List Price:  ৳15,338.92
You save:  ৳4,382.86
₹8,347.75
List Price:  ₹11,687.19
You save:  ₹3,339.43
RM473.85
List Price:  RM663.41
You save:  RM189.56
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,772.29
List Price:  ₨38,882.31
You save:  ₨11,110.02
฿3,673.43
List Price:  ฿5,142.95
You save:  ฿1,469.52
₺3,227.91
List Price:  ₺4,519.21
You save:  ₺1,291.29
B$507.22
List Price:  B$710.14
You save:  B$202.91
R1,843.50
List Price:  R2,580.97
You save:  R737.47
Лв181.60
List Price:  Лв254.25
You save:  Лв72.65
₩135,529.28
List Price:  ₩189,746.42
You save:  ₩54,217.13
₪374.14
List Price:  ₪523.82
You save:  ₪149.67
₱5,714.50
List Price:  ₱8,000.53
You save:  ₱2,286.03
¥15,376.28
List Price:  ¥21,527.41
You save:  ¥6,151.12
MX$1,692.97
List Price:  MX$2,370.23
You save:  MX$677.25
QR364.12
List Price:  QR509.78
You save:  QR145.66
P1,360.02
List Price:  P1,904.08
You save:  P544.06
KSh13,423.65
List Price:  KSh18,793.65
You save:  KSh5,370
E£4,794.44
List Price:  E£6,712.40
You save:  E£1,917.96
ብር5,730.37
List Price:  ብር8,022.75
You save:  ብር2,292.37
Kz83,526.97
List Price:  Kz116,941.11
You save:  Kz33,414.13
CLP$94,107.58
List Price:  CLP$131,754.38
You save:  CLP$37,646.80
CN¥707.87
List Price:  CN¥991.05
You save:  CN¥283.18
RD$5,800.92
List Price:  RD$8,121.52
You save:  RD$2,320.60
DA13,449.71
List Price:  DA18,830.13
You save:  DA5,380.42
FJ$224.39
List Price:  FJ$314.16
You save:  FJ$89.76
Q775.96
List Price:  Q1,086.38
You save:  Q310.41
GY$20,884.44
List Price:  GY$29,239.05
You save:  GY$8,354.61
ISK kr13,954.60
List Price:  ISK kr19,537
You save:  ISK kr5,582.40
DH1,006.90
List Price:  DH1,409.70
You save:  DH402.80
L1,768.33
List Price:  L2,475.74
You save:  L707.40
ден5,716.08
List Price:  ден8,002.75
You save:  ден2,286.66
MOP$803.50
List Price:  MOP$1,124.94
You save:  MOP$321.43
N$1,847.92
List Price:  N$2,587.16
You save:  N$739.24
C$3,674.94
List Price:  C$5,145.06
You save:  C$1,470.12
रु13,329.02
List Price:  रु18,661.17
You save:  रु5,332.14
S/372.66
List Price:  S/521.73
You save:  S/149.07
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,692.78
List Price:  ZK3,770.01
You save:  ZK1,077.22
L461.79
List Price:  L646.53
You save:  L184.73
Kč2,323.07
List Price:  Kč3,252.39
You save:  Kč929.32
Ft36,167.88
List Price:  Ft50,636.48
You save:  Ft14,468.60
SEK kr1,082.52
List Price:  SEK kr1,515.58
You save:  SEK kr433.05
ARS$87,865.40
List Price:  ARS$123,015.07
You save:  ARS$35,149.67
Bs691.31
List Price:  Bs967.87
You save:  Bs276.55
COP$388,509.43
List Price:  COP$543,928.75
You save:  COP$155,419.31
₡51,021.71
List Price:  ₡71,432.44
You save:  ₡20,410.72
L2,466.06
List Price:  L3,452.59
You save:  L986.52
₲747,341.53
List Price:  ₲1,046,308.05
You save:  ₲298,966.51
$U3,819.97
List Price:  $U5,348.11
You save:  $U1,528.14
zł401.45
List Price:  zł562.05
You save:  zł160.59
Already have an account? Log In

Transcript

On this section of the course, we're going to speak about error handling in Excel VBA. So on the process of writing code in Excel VBA and developing applications, you are bound to get errors no matter what your skill level is as a developer. And on this video, we're going to have an introduction to error handling and excel vba. And we're going to learn about the different error types in Excel VBA. So in Excel VBA, we've got three main types of errors that you might encounter. We've got syntax errors, and we've got compile errors, and we've got runtime errors.

And to understand the difference between them, we need to understand how VBA checks our code when we write our code. So VBA does three types of checks when we write our code. The first type is syntax checks. And the second type is compile checks. And the third type is runtime checks. So first of all, starting with syntax checks, these syntax checks are basically like spelling and grammar checks if we relate that to English language.

So when VBA checks your code on this type of checks on syntax checks, it asks the question, does your code have any spelling or grammar mistakes, so it basically searches for any grammar or spelling mistakes on your code. So let's have an example. For example here, if you write workbooks, and then my workbook dot open, you will immediately if you write it this way, you will immediately get your code turn into red color. So why did our code turn into red color here? It's because we're having a spelling mistake, which Just basically not putting a second double quote, after the word, my workbook. So the word, my workbook needs to be enclosed in two double quotes, we've got only one double quote here on the left who don't have a closing double quote.

So this is in VBA. His book is basically a spelling mistake. And this is a syntax error. And you will get notified about this error by having your code highlighted in red. And this is because syntax checks are done in real time as you write your VBA code. So if you make any errors, you will get notified immediately by having your code turning into red.

The second type of checks is basically compiled checks. So compile checks, ask the question, does your code Make sense? So this is actually kind of the question that VBA asks about your code. Does your code make sense and this is done before Attempting to run your code. So before your code starts to run, and just after you hit the Run button, this check is done. And if you have an error, you will have an error that says compile error and you will have the error and your code will not start to run.

The third type of checks that get done on your code are basically runtime checks. One time checks, ask the question Can what you need to do be done and this is done when your code is running while executing each line of code. Okay, so let's have an example for each type of errors in Excel VBA. So starting with syntax errors, syntax errors are similar to spelling or punctuation errors in English language. So if you write in English, I love chocolate and you write the spelling of chocolate incorrectly as I did here, you're basically making a spelling mistake and this is similar to writing workbooks, my workbook without a closing double quote here, open, you made a spelling mistake, and your code will turn into red. Okay, so these are syntax errors.

Compilers are basically errors where you have your spelling and punctuation correct. However, the context that you're using the words in does not make sense. Remember, on compile check, we ask the question, does your code Make sense? So I'm going to have an example here in the English language. So if somebody tells you, I fly my car to work every day, okay, so this sentence, I fly my car to work every day does not have any spelling errors, and it does not have any punctuation errors. And it's grammatically correct.

However, this sentence does not make sense. Why. So what's the error? What's the problem? Well, the problem is cars cannot fly. Right?

At least not your average. car I mean, your average car does not fly, you probably could have seen a science fiction movie where cars fly. But for now, at least your average car for now does not fly, they could fly in the future. But for now, cars cannot fly. So we try to relate that to VBA. This error can happen when you write a word that exists in the VBA dictionary, however, using it in the wrong context.

So for example, if you write workbook, monk workbook dot open, so where is the problem here, so this would generate a compile error. Why? Because having the word workbook is not correct in this context, the word workbook does exist in the VBA dictionary. So VBA recognizes the word workbook and you will not get a syntax error. However, using it in this context is incorrect, because you should use work books not workbook. So this will really results in a compile error.

So how would you fix that, you need to make sure that you're using words in the right context. runtime errors result from something that can be done normally. So usually, this thing that you're trying to do can be done normally. But in this particular case, it could not be done. So this can result in a runtime error. And runtime errors result when you start running your code.

So actually, you will have your runtime error on a certain line of code that has a problem. Okay, so you will have it after your code starts to run. So to relate that we're not going to relate it to English language, because runtime errors are more about actions, not just words and languages will relate it to normal life or real life. So for example, if you try to drive your car with an empty gas tank, so driving up car is something that can be done normally. However, in this case, only, you cannot do it because you did not have any gas in the tank. So trying to do a valid action driving your car, it's a valid action.

It's something that can normally be done. However, it could not be done on this particular time, or in this particular occasion, because you did not have any gas in the tank. So in VBA, this can happen for example, if you're trying to select a worksheet that does not exist in your workbook. So for example, if you're right worksheets, my worksheet dot select, however, my worksheet does not exist in your workbook, you do not have a worksheet called my worksheet. So you will not get the error until you attempt to run this particular line of code that says worksheets my worksheet dot select so selecting the worksheet is a valid action, you just could not do it in this particular case, because the worksheet is not available in your workbook, then you will get a runtime error. So how can you fix that you need to make sure that what you're trying to do can be done plus apply error handling in case it cannot be done.

That is, if the worksheet does not exist, then write some code to create the worksheet before running that line of code that is going to make you select the worksheet for example. And this is what we're going to learn actually in this part of the course error handling, how to deal with errors, how to make sure that errors do not occur on your code. Okay, so this guy's was a quick introduction to error handling in Excel VBA. On the upcoming videos, we're going to dig deeper into error handling things Very much 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.