-
#11
Ok so I know I’m resurrecting a severely dead thread here, but since its the top hit on Google for «VBA continue keyword» I might as add the actual answer along with my 2 cents.
Here is the second result on Google:
http://msdn.microsoft.com/en-us/library/801hyx6f(v=vs.80).aspx
Continue [ Do / While / For ] is the syntax yeekhoon asked for.
pcg’s info is certainly right about programming practices, but continue is a very common loop feature and does not necessarily imply badly structured code. As you might know that loops and ifs use jmps (in ASM), which are more or less the same as GoTos for those who do not know. Obviously this is only ok because the compiler resolves the addresses/etc (ensuring you don’t break it unless you’re trying to) but it is a tad silly to reject the concept when we rely on this all the time Also, I seem to remember that the continue keyword is mainly used to implement this whilst allowing the compiler to maintain the actual jump. Tbf though, you’d have to be fairly careful using it if you have an under-structured loop, and it could have nasty implementation specifics that turn up bugs later (VB probably does). I was under the impression that if you want this form of behaviour from a loop without continue, you’d do something analogous to using a bool to pass over ifs.
What really got me about this thread, though, is that Stormseed (banned.. lol.. wonder why…) essentially said that comparing languages is not useful. Programming languages, even when part of vastly different paradigms, have significant similarities in syntax, usage, etc. Comparison of a language gives you a place to start from and looking at where the differences are defines the language [in an abstract form]. I learnt Java in about 20 minutes because of my C++ knowledge (granted they are very similar, and easy), but if I were to ignore my experience it would have taken much longer, and probably some very boring reading. What I’m getting at is this: knowing a programming language isn’t enough, understanding it and others, similar and different, solidify your knowledge. Any programming university course will try and drill this into your head.
Sorry for the mini-rant, but this is worth pointing out imo.
Hopefully now when someone Googles this they’ll get their answer and learn something
NF
Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
-
#12
Don’t appear to be able to edit, so sorry for the bump.
I tried to use the syntax I referenced for my work, turns out they changed it. If you’re handling an error you can use Resume Next. I wasn’t so I rearranged my while.
-
#13
Hello and welcome to the forum Neofish!
yeekhoon’s question was about VBA, no VB. So while there is a Continue command in VB (as you linked to), there isn’t one in VBA. Continue [ Do / While / For ] is the syntax yeekhoon asked for, if they were programming in VB, it’s not going to help them in VBA (as Norie pointed out early on in the post).
So now we can put it to rest that when someone googles «»VBA continue keyword», they won’t be confused by your VB answer when looking for a valid VBA statement.
-
#14
Yeah thats why I was obliged to correct myself. I’m actually fairly new to VBA (what I’m using — hence finding it to not work), but finding accurate info is scarily hard sometimes. Like the valid options for TableDef.Attributes (or why it won’t let you use them in CreateTableDef :P).
Anyways, hopefully people will read the second page when googling
RoryA
MrExcel MVP, Moderator
-
#15
Tabledef.attributes is not a VBA issue per se, it’s a DAO object model question, and should be addressed in the DAO help file.
-
#16
Tabledef.attributes is not a VBA issue per se, it’s a DAO object model question, and should be addressed in the DAO help file.
The real question for this, I think, is: In a For loop how can one jump/continue back to the top loop, with the counter incremented and do the for loop again. In my case I have two scenarios where the one works and the other scenario is exactly the question that is asking here. (according to my understanding)
1. The first scenario is where you have the For loop and in the For loop an if statement is matched and you want to EXit the for loop. For this you use Exit For. 2. The second scenario you do not want to exit, but continue/jump to the top if a if statement is matched and you do not want to perform the rest of the code in the loop, thus not exit but continue with the loop incremented.
1. Example for first scenario:
For row_num 1 to n
some code statements and then an if statement follows.
If xx = AA then (some condition is true)
Exit for (you completely want to exit the For loop)
else
do some code
end if
next row_num
2. Example second scenarion:
For row_num 1 to n
some code statements and then an if statement follows.
If xx = AA then (some condition is true)
Continue For (the if statement is not matched and you just do not want the else statements to execute, but still want to continue witht loop)
else
do some code
end if
next row_num
As far as I know in vb the Continue For is an available option, but not in vba. Thus the question, how to continue/jump to top with row_num incremented and loop again in vba, for the, For loop ?
……..
RoryA
MrExcel MVP, Moderator
-
#17
The second one is how you do it. The else part does not get processed if the If condition is met.
-
#18
Yes, you are correct.
1. In the following code, see the red comments, see Code2. I would not like to use the else.
2. In some cases I would like to, direct after the For loop use the continue for as: see Code1.
Code1:
For row_num = 6 To sheet_1_aantallyne
if xx = ?? then
continue for
end if
some other code to follow with other multimultiple if statements. Not using the else part will make it easier because it is always difficult to match up the IFs with End IFs for lenghtly lines of code with nested if statements.
next row_num
Code2:
For row_num = 6 To sheet_1_aantallyne
Current_A_RowNumber$ = sheet_1_matchKol + RTrim(LTrim(Str$(row_num)))
soekvir = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
If soekvir = «» Then
Exit For
End If
voort = 0
Current_A_RowNumber$ = «T» + RTrim(LTrim(Str$(row_num)))
soekvirFile = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
Current_A_RowNumber$ = «P» + RTrim(LTrim(Str$(row_num)))
soekvirNoBom = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
If soekvirFile <> «» Then
voort = 1
End If
If soekvirNoBom <> «» Then
voort = 1
End If
If voort = 1 Then
niks = 1 ‘ HERE I would like to to use a the Continue For
Else
Current_A_RowNumber$ = «A» + RTrim(LTrim(Str$(row_num + 1)))
soekvirFlat = LTrim(RTrim(Sheets(sheet_1_naam).Range(Current_A_RowNumber$).value))
If soekvirFlat = «» Then
lot other other nested if statements follows. …
…….
if xxxxxx …
….
RoryA
MrExcel MVP, Moderator
-
#19
There is no such command. The closest thing would be to put a label before then Next row_num and then use a Goto statement. Used judiciously (i.e. sparingly) that’s OK but not a great habit to get into in my opinion as it often leads to spaghetti code.
-
#20
Hi rorya,
This works perfectly for me. I tested it.
I know it is not good practice to use Goto’s (I never use them anyway) but in this case, perfectly !
Thank you. Great work !
-
12-02-2014, 05:15 PM
#1
‘Continue For’ gives error (does not work)
I am working on MS Word 2010. When I use ‘Continue For’, it gives error («Expected: Expression«).
I am on Word 2010, VBA 7.0
I get a red error on the ‘Continue For’; what possibility could be wrong with this?
I’ve never used a ‘Continue For’ before.
(I’ve got the .Find.Found properly associated; so don’t worry about that; I only show the above streamlined so to zero in on the ‘Continue For’ part)Last edited by sauerj; 12-02-2014 at 09:13 PM.
-
12-02-2014, 07:59 PM
#2
Re: ‘Continue For’ gives error (does not work)
There is no continue function in VBA.
Also you have an End With without a With inside the loop.
-
12-02-2014, 08:23 PM
#3
Re: ‘Continue For’ gives error (does not work)
As mehmetcik indicates, there is no ‘Continue For’ in Word VBA.
It’s not clear what you’re trying to achieve, but perhaps the following macros will give you some pointers. The first one bolds the found text wherever it is found in a table. The second one bolds it only if it’s in column 2 in a table.
Cheers,
Paul Edstein
[Fmr MS MVP — Word]
-
12-02-2014, 09:18 PM
#4
Re: ‘Continue For’ gives error (does not work)
Thanks guys! Prior to my first post, I did a google search and got led astray because I saw an example using the ‘Continue For’ command, but upon re-looking (more carefully this time), I now see that that example was for «Visual Studio», not VBA. … Thanks for taking the time to set me straight.
Contents
- Loops in VBA
- The For Loop in VBA
- Syntax:
- Simple Examples of a For Loop
- Print a Mathematical Table for a Number
- Program to Print Contents of an Array
- Program to Write and Read Array Contents
- Program That Uses the [step] Value
- The Nested “For” Loop
- Structure
- A Few Examples of Nested For Loops
- Program to Format Cells with a Specific Word
- Program with 2D Array and “NESTED FOR” Loop
- The “Continue” Statement in Visual Basic: Skip a Part of an Iteration
- Continue
- Continue Statement is Not Available in VBA
- An Alternative to the Continue Statement
- Conclusion
Loops in VBA
Loops are an essential part of any programming language because they help with the repeated running of any code snippet. Just like any other language, VBA offers the following loops to enable efficient programming:
- Do until
- Do while
- For
- Foreach
In this article we will dive dep into the “for” loop which iterates only for “x” number of times where “x” is a known value before the loop’s runtime.
“X” can also be a predictable number at runtime, for example when you determine the Lbound and Ubound values of arrays.
The For Loop in VBA
In VBA, “for loop” is easy to use. It follows a simple and understandable syntax.
Syntax:
For <counter> = <start> to <end> [ <step value> ]
[ <code block to execute> ]
Next [ <counter> ]
where
<counter>
is the iterator variable which keeps incrementing by the <step value>
after every iteration.
<step value>
is optional. If this value is not provided, the default incremental value is “1.”
<Code block to execute>
is also optional. It is the snippet of code to be executed during every iteration.
The “Next” statement marks the end of the “for” loop. It indicates to move to the next iteration of this loop with the incremented counter value.
Simple Examples of a For Loop
Print a Mathematical Table for a Number
Sub forloop_demo() ' declare all the required variables Dim counter, input_number As Integer ' Receive input from user input_number = InputBox(" Which tables do you want to print ? ") ' Print the mathematical table of the input number upto 12 count For counter = 1 To 12 Debug.Print (counter &amp;amp; " X " &amp;amp; input_number &amp;amp; " = " &amp;amp; counter * input_number) Next End Sub
This program receives a number as an input from the user. Then, using a “for” loop, it is multiplied with each number from 1 to 12 to print a “times table” table of that input number.
A sample output of this program if “3” is input by the user is seen in the image below.
Program to Print Contents of an Array
This program defines an array with its size. All elements of the array are assigned values one by one. Next they are printed as we iterate through a loop. It is noteworthy to say that the iterator variable (i) of the “for” loop is also used as the array index inside the loop (this is not mandatory but makes it easy to read/understand/maintain).
Sub array_cars() 'declaring variable while defining its size Dim arr_my_cars1(5) As Variant 'initialize array elements for one array arr_my_cars1(0) = "Benz" arr_my_cars1(1) = "Maruthi" arr_my_cars1(2) = "Hyundai" arr_my_cars1(3) = "Ford" arr_my_cars1(4) = "Nissan" 'print all elements of the array For i = LBound(arr_my_cars1) To UBound(arr_my_cars1) Debug.Print arr_my_cars1(i) Next End Sub
Output of the Program:
Benz
Maruthi
Hyundai
Ford
Nissan
Program to Write and Read Array Contents
This program slightly varies from the one above. Here you will create an array and iterate through it using its indices.
Values are given to each array item using a “for” loop initially. (This was not the case in the previous program.)
Then, all the data stored in each element of the same array are read/printed back using another “for” loop.
Two loops are used here one below the other:
- One to write data to array
- Another one to read data from the same array.
Sub array_check_demo1() ' Declaration of array variable Dim arr1(11) ' Assign values to array elements For i = LBound(arr1) To UBound(arr1) 'Allocate data for each array item through a loop arr1(i) = Cells(i + 2, 1).Value Next ' Print values of the array For i = LBound(arr1) To UBound(arr1) 'Read data of each array item through a loop Debug.Print arr1(i) Next End Sub
Input is taken from the table below:
Principal amount | No of yrs | Age of customer |
10000 | 5 | 67 |
340600 | 6 | 45 |
457800 | 8 | 34 |
23400 | 3 | 54 |
12000 | 4 | 23 |
23545 | 4 | 56 |
345243 | 2 | 55 |
34543 | 3 | 24 |
23223 | 2 | 19 |
3656 | 1 | 65 |
Output of the Program
Program That Uses the [step] Value
Check out this program and try to find out why the numbers are not continuous in the output:
Sub step_demo() ' declare a counter Dim p As Integer 'loop through 10 numbers and print them For p = 1 To 10 step 3 Debug.Print p Next End Sub
Output of the Program
1
4
7
10
Yes, you might have found that the iterator variable increments by “3” instead of “1” in every iteration. Hence, the numbers printed are skip counted by “3.”
Try the same with different values in the place of [step] to understand it better if you are still confused.
The Nested “For” Loop
A loop within a loop is called a nested loop.
Structure
Let me explain this structure with colors:
- The for loop structure in yellow color is the outermost loop.
- The for loop structure/block in green color is the inner loop for the outermost (yellow color) loop. But it also acts as the outer loop for the “for loop” structure in turquoise color.
- The for loop structure in turquoise color is the innermost loop.
In this way , we can have any number of nested loops.
But there are ample chances for you to get confused when you see your own code after several days. You will wonder inside which loop is a specific line and what role it plays in the whole code.
To avoid this, follow the tips below while you code your logic:
- Maintain the alignment in your code with proper tab spaces as shown in the structure above. This can help you find the block of code/loop in which your statement is.
If possible, use the iterator variable next to the “next” keyword. It can help you indicate which “next” statement marks the closure of which loop. This is also marked in the colored structure for reference.
A Few Examples of Nested For Loops
Program to Format Cells with a Specific Word
This program iterates through all the cells (every column of every row in this case). If the cell content has a word “India,” then the specified formatting is applied to it.
Sub format_cell_with() For i = 1 To 15 For j = 1 To 5 cellcontent = Cells(i, j).Value If InStr(cellcontent, "India") &amp;gt; 0 Then With Cells(i, j).Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.399975585192419 .PatternTintAndShade = 0 End With End If Next Next
Input sheet:
ICC Men’s Cricket World Cup Winners – 50 Overs | ||||
Year | World Cup Winner | Runners Up | Host | Finals Venue |
1975 | West Indies | Australia | England | Lord’s Cricket Ground, London |
1979 | West Indies | England | England | Lord’s Cricket Ground, London |
1983 | India | West Indies | England | Lord’s Cricket Ground, London |
1987 | Australia | England | India & Pakistan | Eden Gardens, Kolkata |
1992 | Pakistan | England | Australia & New Zealand | Melbourne Cricket Ground, Melbourne |
1996 | Sri Lanka | Australia | India, Pakistan & Sri Lanka | Gaddafi Stadium, Lahore |
1999 | Australia | Pakistan | England | Lord’s Cricket Ground, London |
2003 | Australia | India | Australia | Wanderers, Johannesburg |
2007 | Australia | Sri Lanka | West Indies | Kensington Oval, Bridgetown |
2011 | India | Sri Lanka | India | Wankhede Stadium, Mumbai |
2015 | Australia | New Zealand | Australia | Melbourne Cricket Ground |
2019 | England | New Zealand | England | Lord’s, London |
2023 | — | — | India | — |
Output sheet – after the program is run:
Program with 2D Array and “NESTED FOR” Loop
This program declares a two dimensional array and initializes data in all its elements.
The first dimension holds the students’ names, and the second dimension holds the “exam results” of the students in the first dimension. These are written back to an Excel sheet.
Warning: As the code does not select any sheet in specific, please ensure that you keep a blank Excel sheet selected. This can prevent any damage to your existing data.
In case you wish to learn more about multidimensional arrays, please check out this article.
Sub Nested_for_demo2() 'declaring and defining size of an array '3 means 4 rows starting from 0 to 3 and 1 means 2 columns starting from 0 to 1 Dim arr_stu(1 To 5, 1 To 2) As Variant 'initialize array elements arr_stu(1, 1) = "Dave" arr_stu(1, 2) = "Fail" arr_stu(2, 1) = "Trumpo" arr_stu(2, 2) = "Pass" arr_stu(3, 1) = "Vincent" arr_stu(3, 2) = "Pass" arr_stu(4, 1) = "Rose Mary" arr_stu(4, 2) = "Pass" arr_stu(5, 1) = "Eliza" arr_stu(5, 2) = "Fail" ' print all elements to the open excel sheet. For i = 1 To 5 For j = 1 To 2 Cells(i, j) = arr_stu(i, j) Next j Next End Sub
Output of the Program on the Active Excel Sheet: Here
- The rows indicate the “i” value of the array (first dimension) and the iterator variable of the outer “for” loop.
- The columns indicate the “j” value of the array (Col A – first dimension , Col B – second dimension) and the iterator variable of the inner “for” loop.
The “Continue” Statement in Visual Basic: Skip a Part of an Iteration
When you encounter a situation where you wish to skip running the rest of the code inside the loop for a specific iteration based on a condition, the “continue” statement will come to your rescue in Visual Basic Programming.
Continue
Syntax:
Continue { Do | For | While }
Where { Do | For | While }
are the respective loops in which it has to be used.
A Simple Example
Sub continue_demo() ' declare a counter Dim i As Integer 'loop through 10 numbers and print them For i = 1 To 10 ' we will skip printing "4" If i = 4 Then Continue For End Debug.Print i Next End Sub
Your output will not have “4” in this case. As the condition is met in the 4th iteration, the continue statement is hit and the rest of the code is skipped for that iteration. The control then moves to the “next” statement of the loop (inner loop in case of nested loops).
Note: The Next
statement is the closure of a loop, as you may be aware.
Output of the above program
1
2
3
5
6
7
8
9
10
Continue Statement is Not Available in VBA
VBA does not offer the “continue statement” because well-built logic in a loop can help us avoid using a “continue” statement. For example, rearranging statement blocks like nested loops and if conditions can help avoid the use of “continue” statements.
An Alternative to the Continue Statement
In unavoidable situations, the Goto statement can be used.
This article can provide you with more insights on how to properly use the “Goto” statement in VBA.
Conclusion
In my experience, I would say that the “for” loop is my favorite compared to the “do while “/“Do until”/“ For each” loops. The reason is the comfort of defining it completely in one line. Even during run-time, you can find how many iterations are completed and how many are yet to come.
I have also been able to use this loop in situations where I do not know the number of iterations. I manage it using conditions inside the loop. It also comes handy when I want to wait for a page load during automation. Once you start using this loop, you will even start playing around nested “for loops” with much more confidence and fewer of mistakes.
So, what are you waiting for ? Why not give it a try? 😊
У меня есть цикл for по массиву. Что я хочу сделать, так это проверить определенное условие в цикле и перейти к следующей итерации, если это правда:
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
If (Schedule(i, 1) < ReferenceDate) Then
PrevCouponIndex = i
Continue '*** THIS LINE DOESN'T COMPILE, nor does "Next"
End If
DF = Application.Run("SomeFunction"....)
PV = PV + (DF * Coupon / CouponFrequency)
Next
Я знаю, что могу:
If (Schedule(i, 1) < ReferenceDate) Then Continue For
но я хочу иметь возможность записать последнее значение i в переменную PrevCouponIndex.
Есть идеи?
Спасибо
6 ответы
VBA не имеет Continue
или любое другое эквивалентное ключевое слово для немедленного перехода к следующей итерации цикла. Я бы посоветовал разумно использовать Goto
в качестве обходного пути, особенно если это всего лишь надуманный пример, а ваш реальный код более сложен:
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
If (Schedule(i, 1) < ReferenceDate) Then
PrevCouponIndex = i
Goto NextIteration
End If
DF = Application.Run("SomeFunction"....)
PV = PV + (DF * Coupon / CouponFrequency)
'....'
'a whole bunch of other code you are not showing us'
'....'
NextIteration:
Next
Однако, если это действительно весь ваш код, @Brian абсолютно прав. Просто поставьте Else
пункт в вашем If
заявление и покончить с этим.
ответ дан 01 окт ’15, 00:10
Вы можете использовать своего рода continue
с помощью вложенного Do ... Loop While False
:
'This sample will output 1 and 3 only
Dim i As Integer
For i = 1 To 3: Do
If i = 2 Then Exit Do 'Exit Do is the Continue
Debug.Print i
Loop While False: Next i
Создан 26 фев.
Не могли бы вы просто сделать что-нибудь такое простое?
For i = LBound(Schedule, 1) To UBound(Schedule, 1)
If (Schedule(i, 1) < ReferenceDate) Then
PrevCouponIndex = i
Else
DF = Application.Run("SomeFunction"....)
PV = PV + (DF * Coupon / CouponFrequency)
End If
Next
ответ дан 30 дек ’11, 15:12
Continue For
недействителен в VBA или VB6.
от эта страница MSDN похоже, что он был введен в VB.Net в VS 2005./Net 2.
Как говорили другие, на самом деле нет другого варианта, кроме как использовать Goto
или Else
.
ответ дан 30 дек ’11, 15:12
Привет, я тоже столкнулся с этой проблемой, и я решаю ее, используя приведенный ниже пример кода
For j = 1 To MyTemplte.Sheets.Count
If MyTemplte.Sheets(j).Visible = 0 Then
GoTo DoNothing
End If
'process for this for loop
DoNothing:
Next j
ответ дан 05 мая ’16, 05:05
Возможно, попробуйте поставить все это в конце, если и используйте else, чтобы пропустить код, это сделает так, что вы не сможете использовать GoTo.
If 6 - ((Int_height(Int_Column - 1) - 1) + Int_direction(e, 1)) = 7 Or (Int_Column - 1) + Int_direction(e, 0) = -1 Or (Int_Column - 1) + Int_direction(e, 0) = 7 Then
Else
If Grid((Int_Column - 1) + Int_direction(e, 0), 6 - ((Int_height(Int_Column - 1) - 1) + Int_direction(e, 1))) = "_" Then
Console.ReadLine()
End If
End If
Создан 30 янв.
Не тот ответ, который вы ищете? Просмотрите другие вопросы с метками
vba
conditional
loops
or задайте свой вопрос.
No matter how experienced you’re with VBA coding, errors are always going to be a part of it.
The difference between a novice and an expert VBA programmer is that the expert programmers know how to effectively handle and use errors.
In this tutorial, I will show you various ways you can use to handle errors effectively in Excel VBA.
Before we get into VBA error handling, let’s first understand the different types of errors you are likely to encounter when programming in Excel VBA.
Types of VBA Errors in Excel
There are four types of errors in Excel VBA:
- Syntax errors
- Compilation errors
- Runtime errors
- Logical Errors
Let’s quickly understand what these errors are and when you’re likely to encounter these.
Syntax Error
A syntax error, as the name suggests, occurs when VBA finds something wrong with the syntax in the code.
For example, if you forget a part of the statement/syntax that is needed, then you will see the compile error.
In the below code, as soon as I hit enter after the second line, I see a compile error. This is because the IF statement needs to have the ‘Then‘ command, which is missing in the below code.
Note: When you are typing a code in Excel VBA, it checks for each sentence as soon as you hit enter. If VBA finds something missing in the syntax, it instantly shows a message with some text that can help you understand the missing part.
To make sure you see the syntax error whenever there is something missing, you need to make sure Autosyntax check is enabled. To do this, click on ‘Tools’ and then click on ‘Options’. In the options dialog box, make sure that the ‘Auto Syntax Check’ option is enabled.
If the ‘Auto Syntax Check’ option is disabled, VBA will still highlight the line with the syntax error in red, but it will not show the error dialog box.
Compile Error
Compile errors occur when something is missing that is needed for the code to run.
For example, in the below code, as soon as I try to run the code, it will show the following error. This happens as I have used the IF Then statement without closing it with the mandatory ‘End If’.
A syntax error is also a type of compile error. A syntax error occurs as soon as you hit enter and VBA identifies that something is missing. A compilation error can also occur when VBA doesn’t find anything missing while typing the code, but it does when the code is compiled or executed.
VBA checks each line as you’re typing the code and highlights the syntax error as soon as the line is incorrect and you hit enter. Compile errors, on the other hand, are only identified when the entire code is analyzed by VBA.
Below are some scenarios where you’ll encounter the compile error:
- Using an IF Statement without the End IF
- Using For statement with the Next
- Using Select statement without using the End Select
- Not declaring the variable (this works only when Option Explicit is enabled)
- Calling a Sub/Function that does not exist (or with wrong parameters)
Note about ‘Option Explicit’: When you add ‘Option Explicit’, you will be required to declare all the variables before running the code. If there is any variable that has not been declared, VBA would show an error. This is a good practice as it shows an error in case you have a misspelled variable. You can read more about Option Explicit here.
Run Time Errors
Runtime errors are those that occur when the code is running.
Run time errors will occur only when all the syntax and compile errors are being taken care of.
For example, if you run code that is supposed to open an Excel workbook, but that workbook is unavailable (either deleted or name changed), your code would give you a runtime error.
When a runtime error occurs, it will stop the code and show you the error dialog box.
The message in the Run-time error dialog box is a little more helpful. It tries to explain the problem that can help you correct it.
If you click on the Debug button, it will highlight the part of the code that is leading to the error.
If you have corrected the error, you can click on the Run button in the toolbar (or press F5) to continue running the code from where it left.
Or you can also click on the End button to come out of the code.
Important: In case you click the End button in the dialog box, it will stop the code at the line at which is encountered. However, all the lines of code before that would have been executed.
Logical Errors
Logical errors would not make your code stop but can lead to wrong results. These could also be the most difficult types of errors to troubleshoot.
These errors are not highlighted by the compiler and need to be manually tackled.
One example of logical error (that I often find myself stuck with) is running into an endless loop.
Another example could be when it gives a result which is wrong. For example, you may end up using a wrong variable in the code or add two variables where one is incorrect.
There are a few ways I use to tackle logical errors:
- Insert Message Box at some place in the code and highlight values/data that can help understand if eberything is going as expected.
- Instead of running the code at one go, go through each line one by one. To do this, click anywhere in the code and press F8. you would notice that each time you press F8, one line gets executed. This allows you to go through the code one line at a time and identify the logical errors.
Using Debug to Find Compile/Syntax Errors
Once you’re done with the code, it’s a good practice to first compile it before running.
To compile a code, click on the Debug option in the toolbar and click on Compile VBAProject.
When you compile a VBA project, it goes through the code and identifies errors (if any).
In case it finds an error, it will show you a dialog box with the error. It finds errors one by one. So if it finds an error and you have corrected it, you need to run compile again to find other errors (if there are).
When you’re code is free of errors, the Compile VBAProject option will be greyed out.
Note that Compiling will only find ‘Syntax’ errors and ‘Compile’ errors. It will NOT find the run-time errors.
When you’re writing VBA code, you don’t want the errors to crop up. To avoid this, there are many error-handling methods you can use.
In the next few sections of this article, I will be covering the methods you can use for VBA error handling in Excel.
Configure Error Settings (Handled Vs Unhandled Errors)
Before you start working with your code, you need to check for one setting in Excel VBA.
Go to the VBA toolbar and click on Tools and then click on Options.
In the Options dialog box, click on the General tab and make sure that within the ‘Error Trapping’ group, ‘Break on Unhandled Errors’ is checked.
Let me explain the three options:
- Break on All Errors: This will stop your code on all types of errors, even when you have used the techniques to handle these errors.
- Break in Class Module: This will stop your code on all unhandled errors, and at the same time, if you’re using objects such as Userforms, it will also break within those objects and highlight the exact line causing the error.
- Break on Unhandled Errors: This will stop your code only for those errors that are not handled. This is the default setting as it ensures any unhandled errors are brought to your notice. If you’re using objects such as Userforms, this will not highlight the line causing the error in the object, but will only highlight the line that’s referring to that object.
Note: If you work with objects such as Userforms, you can change this setting to ‘Break on Class Modules’. The difference between #2 and #3 is that when you use Break in Class Module, it will take you to the specific line in the object that is causing the error. You can also choose to go with this instead of ‘Break on Unhandled Errors’.
So in a nutshell – if you’re just starting with Excel VBA, ensure ‘Break on Unhandled Errors’ is checked.
VBA Error Handling with ‘On Error’ Statements
When your code encounters an error, there are a few things you can do:
- Ignore the error and let the code continue
- Have an error handling code in place and run it when an error occurs
Both of these error handling methods ensures that the end user will not get to see an error.
There are a few ‘On Error’ statements that you can use to get these done.
On Error Resume Next
When you use ‘On Error Resume Next’ in your code, any encountered error will be ignored and the code will continue to run.
This error handling method is used quite often, but you need to be cautious when using it. Since it completely ignores any error that may occur, you may not be able to identify the errors that need to be corrected.
For example, if the below code is run, it will return an error.
Sub AssignValues() x = 20 / 4 y = 30 / 0 End Sub
This happens because you can not divide a number by zero.
But if I use the ‘On Error Resume Next’ statement in this code (as shown below), it will ignore the error and I will not know that there is an issue that needs to be corrected.
Sub AssignValues() On Error Resume Next x = 20 / 4 y = 30 / 0 End Sub
On Error Resume Next should be used only when you clearly know the kind of errors your VBA code is expected to throw and it’s alright to ignore it.
For example, below is the VBA event code that would instantly add the date and time value in cell A1 of a newly inserted sheet (this code is added in the worksheet and not in a module).
Private Sub Workbook_NewSheet(ByVal Sh As Object) Sh.Range("A1") = Format(Now, "dd-mmm-yyyy hh:mm:ss") End Sub
While this works great in most cases, it would show an error if I add a chart sheet instead of a worksheet. Since a chart sheet does not have cells, the code would throw an error.
So, if I use the ‘On Error Resume Next’ statement in this code, it will work as expected with worksheets and do nothing with chart sheets.
Private Sub Workbook_NewSheet(ByVal Sh As Object) On Error Resume Next Sh.Range("A1") = Format(Now, "dd-mmm-yyyy hh:mm:ss") End Sub
Note: On Error Resume Next Statement is best used when you know what kind of errors you’re likely to encounter. And then if you think it’s safe to ignore these errors, you can use it.
You can take this code to the next level by analyzing if there was an error, and displaying a relevant message for it.
The below code would show a message box that would inform the user that a worksheet has not been inserted.
Private Sub Workbook_NewSheet(ByVal Sh As Object) On Error Resume Next Sh.Range("A1") = Format(Now, "dd-mmm-yyyy hh:mm:ss") If Err.Number <> 0 Then MsgBox "Looks like you inserted a chart sheet" & vbCrLf & "Error - " & Err.Description End If End Sub
‘Err.Number’ is used to get the error number and ‘Err.Description’ is used to get the error description. These will be covered later in this tutorial.
On Error GoTo 0
‘On Error GoTo 0’ will stop the code on the line that causes the error and shows a message box that describes the error.
In simple terms, it enables the default error checking behavior and shows the default error message.
Then why even use it?
Normally, you don’t need to use ‘On Error Goto 0’, but it can be useful when you use it in conjunction with ‘On Error Resume Next’
Let me explain!
The below code would select all the blank cells in the selection.
Sub SelectFormulaCells() Selection.SpecialCells(xlCellTypeBlanks).Select End Sub
But it would show an error when there are no blank cells in the selected cells.
So to avoid showing the error, you can use On Error Resume next’
Now, it will also show any error when you run the below code:
Sub SelectFormulaCells() On Error Resume Next Selection.SpecialCells(xlCellTypeBlanks).Select End Sub
So far, so good!
The problem arises when there is a part of the code where error can occur, and since you’re using ‘On Error Resume Next’, the code would simply ignore it and move to the next line.
For example, in the below code, there would no error prompt:
Sub SelectFormulaCells() On Error Resume Next Selection.SpecialCells(xlCellTypeBlanks).Select ' .. more code that can contain error End Sub
In the above code, there are two places where an error can occur. The first place is where we are selecting all blank cells (using Selection.SpecialCells) and the second is in the remaining code.
While the first error is expected, any error after that is not.
This is where On Error Goto 0 comes to rescue.
When you use it, you reset the error setting to default, where it will start showing errors when it encounters it.
For example, in the below code, there would be no error in case there are no blank cells, but there would be an error prompt because of ’10/0′
Sub SelectFormulaCells() On Error Resume Next Selection.SpecialCells(xlCellTypeBlanks).Select On Error GoTo 0 ' .. more code that can contain error End Sub
On Error Goto [Label]
The above two methods – ‘On Error Resume Next’ and ‘On Error Goto 0’ – doesn’t allow us to truly handle the error. One makes the code ignore the error and the second one resume error checking.
On Error Go [Label] is a way with which you can specify what you want to do in case your code has an error.
Below is the code structure that uses this error handler:
Sub Test() On Error GoTo Label: X = 10 / 0 'this line causes an error ' ....your remaining code goes here Exit Sub Label: ' code to handle the error End Sub
Note that before the Error handling ‘Label’, there is an Exit Sub. This ensures that in case there are no errors, the sub is exited and the ‘Label’ code is not executed. In case you don’t use Exit Sub, it will always execute the ‘Label’ code.
In the example code below, when an error occurs, the code jumps and executes the code in the handler section (and shows a message box).
Sub Errorhandler() On Error GoTo ErrMsg X = 12 Y = 20 / 0 Z = 30 Exit Sub ErrMsg: MsgBox "There seems to be an error" & vbCrLf & Err.Description End Sub
Note that when an error occurs, the code has already run and executed the lines before the line causing the error. In the above example, the code sets the value of X as 12, but since the error occurs in the next line, it doesn’t set the values for Y and Z.
Once the code jumps to the error handler code (ErrMsg in this example), it will continue to execute all the lines in and below the error handler code and the exit the sub.
On Error Goto -1
This one is a bit complicated, and in most cases, you’re unlikely to use this.
But I will still cover this as I have faced a situation where this was needed (feel free to ignore and jump to the next section if you’re only looking for basics).
Before I get into the mechanics of it, let me try and explain where can it be useful.
Suppose you have a code where an error is encountered. But all is good as you have one error handler in place. But what happens when there is another error in the error handler code (yeah.. somewhat like the inception movie).
In such a case, you can not use the second handler as the first error has not been cleared. So while you have handled the first error, in VBA’s memory it still exists. And the VBA memory only has a place for one error – not two or more than that.
In this scenario, you can use On Error Goto -1.
It clears the error and frees up VBA memory to handle the next error.
Enough talk!
Let’s me explain now by using examples.
Suppose I have the below code. This will throw an error as there is division by zero.
Sub Errorhandler() X = 12 Y = 20 / 0 Z = 30 End Sub
So to handle it, I use an error handler code (with the name ErrMsg) as shown below:
Sub Errorhandler() On Error GoTo ErrMsg X = 12 Y = 20 / 0 Z = 30 Exit Sub ErrMsg: MsgBox "There seems to be an error" & vbCrLf & Err.Description End Sub
All is good now again. As soon as the error occurs, the error handler is used and shows a message box as shown below.
Now, I expand the code so that I have more code in or after the error handler.
Sub Errorhandler() On Error GoTo ErrMsg X = 12 Y = 20 / 0 Z = 30 Exit Sub ErrMsg: MsgBox "There seems to be an error" & vbCrLf & Err.Description A = 10 / 2 B = 35 / 0 End Sub
Since the first error has been handled but the second has not been, I again see an error as shown below.
Still all good. The code is behaving in the way we expected it to.
So to handle the second error, I use another error handler (ErrMsg2).
Sub Errorhandler() On Error GoTo ErrMsg X = 12 Y = 20 / 0 Z = 30 Exit Sub ErrMsg: MsgBox "There seems to be an error" & vbCrLf & Err.Description On Error GoTo ErrMsg2 A = 10 / 2 B = 35 / 0 Exit Sub ErrMsg2: MsgBox "There seems to be an error again" & vbCrLf & Err.Description End Sub
And this is where it doesn’t work as expected.
If you run the above code, it will still give you a run-time error, even after having the second error handler in place.
This happens as we didn’t clear the first error from VBA’s memory.
Yes, we handled it! But it still remains in the memory.
And when VBA encounters another error, it’s still stuck with the first error, and hence the second error handler is not used. The code stops at the line that caused the error and shows the error prompt.
To clear VBA’s memory and clear the previous error, you need to use the ‘On Error Goto -1’.
So if you add this line in the below code and run it, it will work as expected.
Sub Errorhandler() On Error GoTo ErrMsg X = 12 Y = 20 / 0 Z = 30 Exit Sub ErrMsg: MsgBox "There seems to be an error" & vbCrLf & Err.Description On Error GoTo -1 On Error GoTo ErrMsg2 A = 10 / 2 B = 35 / 0 Exit Sub ErrMsg2: MsgBox "There seems to be an error again" & vbCrLf & Err.Description End Sub
Note: The error automatically gets cleared when a subroutine ends. So, ‘On Error Goto -1’ can be useful when you’re getting two or more than two errors in the same subroutine.
The Err Object
Whenever an error occurs with a code, it’s the Err object that is used to get the details about the error (such as the error number or the description).
Err Object Properties
The Err Object has the following properties:
Property | Description |
Number | A number that represents the type of error. When there is no error, this value is 0 |
Description | A short description of the error |
Source | Project name in which the error has occurred |
HelpContext | The help context id for the error in the help file |
HelpFile | A string that represents the folder location and the file name of the help file |
While in most cases you don’t need to use Err object, it can sometimes be useful while handling errors in Excel.
For example, suppose you have a dataset as shown below and for each number, in the selection, you want to calculate the square root in the adjacent cell.
The below code can do it, but since there is a text string in cell A5, it shows an error as soon as this occurs.
Sub FindSqrRoot() Dim rng As Range Set rng = Selection For Each cell In rng cell.Offset(0, 1).Value = Sqr(cell.Value) Next cell End Sub
The problem with this type of error message is that it gives you nothing about what has gone wrong and where the issue occurred.
You can use the Err object to make these error messages more meaningful.
For example, if I now use the below VBA code, it will stop the code as soon as the error occurs and show a message box with the cell address of the cell where there is an issue.
Sub FindSqrRoot() Dim rng As Range Set rng = Selection For Each cell In rng On Error GoTo ErrHandler cell.Offset(0, 1).Value = Sqr(cell.Value) Next cell ErrHandler: MsgBox "Error Number:" & Err.Number & vbCrLf & _ "Error Description: " & Err.Description & vbCrLf & _ "Error at: " & cell.Address End Sub
The above code would give you a lot more information than the simple ‘Type Mismatch’, especially the cell address so that you know where the error occurred.
You can further refine this code to make sure your code runs until the end (instead of breaking at each error) and then gives you a list of cell address where the error occurs.
The below code would do this:
Sub FindSqrRoot2() Dim ErrorCells As String Dim rng As Range On Error Resume Next Set rng = Selection For Each cell In rng cell.Offset(0, 1).Value = Sqr(cell.Value) If Err.Number <> 0 Then ErrorCells = ErrorCells & vbCrLf & cell.Address On Error GoTo -1 End If Next cell MsgBox "Error in the following cells" & ErrorCells Exit Sub End Sub
The above code runs until the end and gives the square root of all the cells that have numbers in it (in the adjacent column). It then shows a message that lists all the cells where there was an error (as shown below):
Err Object Methods
While the Err properties are useful to show useful information about the errors, there are two Err methods as well that can help you with error handling.
Method | Description |
Clear | Clears all the property settings of the Err object |
Raise | Generates a run-time error |
Let’s quickly learn what these are and how/why to use these with VBA in Excel.
Err Clear Method
Suppose you have a dataset as shown below and you want to get the square root of all these numbers in the adjacent column.
The following code will get the square roots of all the numbers in the adjacent column and show a message that an error occurred for cell A5 and A9 (as these have text in it).
Sub FindSqrRoot2() Dim ErrorCells As String Dim rng As Range On Error Resume Next Set rng = Selection For Each cell In rng cell.Offset(0, 1).Value = Sqr(cell.Value) If Err.Number <> 0 Then ErrorCells = ErrorCells & vbCrLf & cell.Address Err.Clear End If Next cell MsgBox "Error in the following cells" & ErrorCells End Sub
Note that I have used the Err.Clear method within the If Then statement.
Once an error has occurred and trapped by the If condition, Err.Clear method resets the error number back to 0. This ensures that IF condition only trap the errors for cells where it is raised.
Had I not used the Err.Clear method, once the error occurs, it would always be true in the IF condition, and the error number has not been reset.
Another way of making this work is by using the On Error Goto -1, which resets the error completely.
Note: Err.Clear is different from On Error Goto -1. Err.Clear only clears the error description and the error number. it doesn’t completely reset it. This means that if there is another instance of error in the same code, you won’t be able to handle it before resetting it (which can be done with ‘On Error Goto -1’ and not by ‘Err.Clear’).
Err Raise Method
The Err.Raise method allows you to raise a run-time error.
Below is the syntax of using the Err.Raise method:
Err.Raise [number], [source], [description], [helpfile], [helpcontext]
All these arguments are optional and you can use these to make your error message more meaningful.
But why would you ever want to raise an error yourself?
Good question!
You can use this method when there is an instance of an error (which means that there is going to an error anyway) and then you use this method to tell the user more about the error (instead of the less helpful error message that VBA shows by default).
For example, suppose you have a dataset as shown below and you want all the cells to have numeric values only.
Sub RaiseError() Dim rng As Range Set rng = Selection On Error GoTo ErrHandler For Each Cell In rng If Not (IsNumeric(Cell.Value)) Then Err.Raise vbObjectError + 513, Cell.Address, "Not a number", "Test.html" End If Next Cell ErrHandler: MsgBox Err.Description & vbCrLf & Err.HelpFile End Sub
The above code would show an error message that has the specified description and the context file.
Personally, I have never used Err.Raise as I mostly work with Excel only. But for someone who uses VBA to work with Excel along with other applications such as Outlook, Word or PowerPoint, this can be useful.
Here is a detailed article on Err.Raise method in case you want to learn more.
VBA Error Handling Best Practices
No matter how skilled you get a writing VBA code, errors are always going to be a part of it. The best coders are those who have the skills to handle these errors properly.
Here are some best practices you can use when it comes to error handling in Excel VBA.
- Use ‘On Error Go [Label]’ at the beginning of the code. This will make sure any error that can happen from there is handled.
- Use ‘On Error Resume Next’ ONLY when you’re sure about the errors that can occur. Use it with expected error only. In case you use it with unexpected errors, it will simply ignore it and move forward. You can use ‘On Error Resume Next’ with ‘Err.Raise’ if you want to ignore a certain type of error and catch the rest.
- When using error handlers, make sure you’re using Exit Sub before the handlers. This will ensure that the error handler code is executed only when there is an error (else it will always be executed).
- Use multiple error handlers to trap different kinds of errors. Having multiple error handler ensures that an error is properly addressed. For example, you would want to handle a ‘type mismatch’ error differently than a ‘Division by 0’ run-time error.
Hope you found this Excel article useful!
Here are some more Excel VBA Tutorials that you may like:
- Excel VBA Data Types – A Complete Guide
- Excel VBA Loops – For Next, Do While, Do Until, For Each
- Excel VBA Events – An Easy (and Complete) Guide
- Excel Visual Basic Editor – How to Open and Use it in Excel
Содержание
- Error Types (Visual Basic)
- Syntax Errors
- Run-Time Errors
- Logic Errors
- Синтаксическая ошибка
- Поддержка и обратная связь
- Syntax Error VBA
- Warning Messages for Syntax Errors
- Common Syntax Errors
- VBA Coding Made Easy
- VBA Code Examples Add-in
- Типы ошибок в VBA
- Ошибки компиляции
- Ошибки выполнения
- Перехват ошибок выполнения
- Логические ошибки
- Excel VBA Error Handling – All You Need to Know!
- Types of VBA Errors in Excel
- Syntax Error
- Compile Error
- Run Time Errors
- Logical Errors
- Using Debug to Find Compile/Syntax Errors
- Configure Error Settings (Handled Vs Unhandled Errors)
- VBA Error Handling with ‘On Error’ Statements
- On Error Resume Next
- On Error GoTo 0
- On Error Goto [Label]
- On Error Goto -1
- The Err Object
- Err Object Properties
- Err Object Methods
- Err Clear Method
- Err Raise Method
- VBA Error Handling Best Practices
Error Types (Visual Basic)
In Visual Basic, errors fall into one of three categories: syntax errors, run-time errors, and logic errors.
Syntax Errors
Syntax errors are those that appear while you write code. If you’re using Visual Studio, Visual Basic checks your code as you type it in the Code Editor window and alerts you if you make a mistake, such as misspelling a word or using a language element improperly. If you compile from the command line, Visual Basic displays a compiler error with information about the syntax error. Syntax errors are the most common type of errors. You can fix them easily in the coding environment as soon as they occur.
The Option Explicit statement is one means of avoiding syntax errors. It forces you to declare, in advance, all the variables to be used in the application. Therefore, when those variables are used in the code, any typographic errors are caught immediately and can be fixed.
Run-Time Errors
Run-time errors are those that appear only after you compile and run your code. These involve code that may appear to be correct in that it has no syntax errors, but that will not execute. For example, you might correctly write a line of code to open a file. But if the file does not exist, the application cannot open the file, and it throws an exception. You can fix most run-time errors by rewriting the faulty code or by using exception handling, and then recompiling and rerunning it.
Logic Errors
Logic errors are those that appear once the application is in use. They are most often faulty assumptions made by the developer, or unwanted or unexpected results in response to user actions. For example, a mistyped key might provide incorrect information to a method, or you may assume that a valid value is always supplied to a method when that is not the case. Although logic errors can be handled by using exception handling (for example, by testing whether an argument is Nothing and throwing an ArgumentNullException), most commonly they should be addressed by correcting the error in logic and recompiling the application.
Источник
Синтаксическая ошибка
Visual Basic не может определить действие, которое требуется выполнить. Эта ошибка имеет следующие причину и решение:
Написание ключевых слов и именованных аргументов должно в точности совпадать с определенным в спецификации синтаксиса. Уточните правильное написание в интерактивной справке и при необходимости исправьте его.
Неверное сочетание знаков препинания. Например, если вы опускаете необязательный позиционно расположенный аргумент, необходимо заменить запятую (,), которая выступает как заполнитель для аргумента.
Проверьте правильность написания имени процедуры.
Попытка одновременно указать аргументы Optional и ParamArray в объявлении процедуры. Аргумент ParamArray не может иметь тип Optional. Удалите один из этих аргументов.
Попытка определить процедуру события с параметром Optional или ParamArray. Удалите ключевое слово Optional или ParamArray из спецификации параметра.
Попытка использовать именованный аргумент в операторе RaiseEvent. События не поддерживают названные аргументы.
Для получения дополнительной информации выберите необходимый элемент и нажмите клавишу F1 (для Windows) или HELP (для Macintosh).
Поддержка и обратная связь
Есть вопросы или отзывы, касающиеся Office VBA или этой статьи? Руководство по другим способам получения поддержки и отправки отзывов см. в статье Поддержка Office VBA и обратная связь.
Источник
Syntax Error VBA
This tutorial will explain what a VBA Syntax Error means and how it occurs.
VBA code has to be constructed in a certain way in order to run. A syntax error occurs in VBA when the code is not constructed correctly. These errors are shown in the VBE Editor in red which makes them easy to identify.
(See our Error Handling Guide for more information about VBA Errors)
Warning Messages for Syntax Errors
If you have the option for Syntax Check switch on in your VBE, then a message box will appear when you make a syntax error.
If a message box does not appear, the incorrect code will still show up in red as in the graphic above but a warning message will not be displayed. It is up to you whether or not you want these warning message to appear as you can switch this option
To switch on this option, in the Menu, select Tools > Options. In the Editor Tab, make sure that the option Auto Syntax Check is ticked.
Common Syntax Errors
Syntax errors occur due to misspelling or missing code – like the omission of a Then when we are writing the first line of an If statement.
Some other common error are listed below.
For without a To :
Do Until without specifying the condition:
With without specifying what is included in the with:
Not Closing Brackets or Leaving out Punctuation:
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro — A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!
VBA Code Examples Add-in
Easily access all of the code examples found on our site.
Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.
Источник
Типы ошибок в VBA
При выполнении макросов Excel могут возникнуть ошибки, которые в VBA делят на три категории:
Далее мы поговорим о каждом из трёх типов ошибок VBA подробно.
Ошибки компиляции
Компилятор VBA рассматривает ошибки компиляции как недопустимые и выделяет их в коде ещё до того, как дело дойдёт до запуска макроса.
Если при написании кода допущена синтаксическая ошибка, то редактор VBA сигнализирует об этом немедленно: либо при помощи окна с сообщением, либо выделяя ошибку красным цветом, в зависимости от статуса режима Auto Syntax Check.
Примечание: При включённом режиме Auto Syntax Check каждый раз, при появлении в редакторе Visual Basic во введённом коде синтаксической ошибки, будет показано соответствующее сообщение. Если же этот режим выключен, то редактор VBA продолжит сообщать о синтаксических ошибках, просто выделяя их красным цветом. Опцию Auto Syntax Check можно включить/выключить в меню Tools > Options редактора Visual Basic.
В некоторых случаях ошибка компиляции может быть обнаружена при выполнении компиляции кода, непосредственно перед тем, как макрос будет выполнен. Обычно ошибку компиляции несложно обнаружить и исправить, потому что компилятор VBA даёт информацию о характере и причине ошибки.
Например, сообщение «Compile error: Variable not defined» при попытке запустить выполнение кода VBA говорит о том, что происходит попытка использовать или обратиться к переменной, которая не была объявлена для текущей области (такая ошибка может возникнуть только если используется Option Explicit).
Ошибки выполнения
Ошибки выполнения возникают в процессе выполнения кода и приводят к остановке выполнения программы. Этот тип ошибок VBA, как правило, также не сложно обнаружить и исправить, так как сообщается информация о характере ошибки и место в коде, где произошла остановка.
Примером такой ошибки может служить попытка выполнить деление на ноль. В результате будет показано сообщение «Run-time error ’11’: Division by zero«.
В зависимости от структуры проекта VBA, может быть предложено выполнить отладку кода (как показано на рисунке ниже). В этом случае при нажатии на кнопку Debug (в окне сообщения о необходимости отладки) будет выделена цветом строка кода, которая стала причиной ошибки VBA.
Получив такое сообщение и видя выделенную строку кода, как в приведённом выше примере, обнаружить причину ошибки будет совсем не сложно.
В случае если код сложнее, чем в нашем примере, то, чтобы получить больше информации о причине возникновения ошибки VBA, можно проверить значения используемых переменных. В редакторе VBA для этого достаточно навести указатель мыши на имя переменной, или можно открыть окно отслеживания локальных переменных (в меню редактора View > Locals Window).
Коды различных ошибок выполнения расшифрованы на сайте Microsoft Support (на английском). Наиболее часто встречающиеся ошибки VBA перечислены в этой таблице:
5 | Недопустимый вызов процедуры (Invalid procedure call) |
7 | Недостаточно памяти (Out of memory) |
9 | Индекс вне заданного диапазона (Subscript out of range)
Эта ошибка возникает при попытке обратиться к элементу массива за пределами заданного размера массива – например, если объявлен массив с индексами от 1 до 10, а мы пытаемся обратиться к элементу этого же массива с индексом 11. |
11 | Деление на ноль (Division by zero) |
13 | Несоответствие типа (Type mismatch)
Эта ошибка возникает при попытке присвоить переменной значение не соответствующего типа – например, объявлена переменная i типа Integer, и происходит попытка присвоить ей значение строкового типа. |
53 | Файл не найден (File not found)
Иногда возникает при попытке открыть не существующий файл. |
Перехват ошибок выполнения
Не все ошибки выполнения бывают вызваны недочётами в коде. Например, ошибки VBA не удастся избежать, если для работы макроса необходимо открыть файл с данными, а этого файла не существует. В таких случаях признаком профессионализма будет перехват ошибок и написание кода VBA, который будет выполняться при их возникновении. Таким образом, вместо неприятных сбоев будет происходить изящное завершение работы макроса.
Для того, чтобы помочь справиться с возникающими ошибками, VBA предоставляет разработчику операторы On Error и Resume. Эти операторы отслеживают ошибки и направляют выполнение макроса в специальный раздел кода VBA, в котором происходит обработка ошибки. После выполнения кода обработки ошибки, работа программы может быть продолжена с того места, где возникла ошибка, или макрос может быть остановлен полностью. Далее это показано на примере.
В этом коде производится попытка открыть файл Excel с именем Data. Если файл не найден, то пользователю будет предложено поместить этот файл в нужную папку. После того, как пользователь сделает это и нажмёт ОК, выполнение кода продолжится, и попытка открыть этот файл повторится. При желании вместо попытки открыть нужный файл, выполнение процедуры Sub может быть прервано в этом месте при помощи команды Exit Sub.
Логические ошибки
Логические ошибки (или баги) возникают в процессе выполнения кода VBA, но позволяют ему выполняться до самого завершения. Правда в результате могут выполняться не те действия, которые ожидалось, и может быть получен неверный результат. Такие ошибки обнаружить и исправить труднее всего, так как компилятор VBA их не распознаёт и не может указать на них так, как это происходит с ошибками компиляции и выполнения.
Например, при создании макроса в процедуре случайно были просуммированы не те переменные, которые требовалось просуммировать. Результат будет ошибочным, но макрос будет продолжать выполняться до завершения.
Редактор Excel VBA предоставляет набор инструментов отладки, которые помогут найти и исправить логические ошибки в коде VBA. В данной статье мы не будем рассматривать подробно эти инструменты. Любознательный пользователь может найти обзор инструментов отладки VBA на сайте Microsoft Help & Support (на английском).
Источник
Excel VBA Error Handling – All You Need to Know!
No matter how experienced you’re with VBA coding, errors are always going to be a part of it.
The difference between a novice and an expert VBA programmer is that the expert programmers know how to effectively handle and use errors.
In this tutorial, I will show you various ways you can use to handle errors effectively in Excel VBA.
Before we get into VBA error handling, let’s first understand the different types of errors you are likely to encounter when programming in Excel VBA.
This Tutorial Covers:
Types of VBA Errors in Excel
There are four types of errors in Excel VBA:
- Syntax errors
- Compilation errors
- Runtime errors
- Logical Errors
Let’s quickly understand what these errors are and when you’re likely to encounter these.
Syntax Error
A syntax error, as the name suggests, occurs when VBA finds something wrong with the syntax in the code.
For example, if you forget a part of the statement/syntax that is needed, then you will see the compile error.
In the below code, as soon as I hit enter after the second line, I see a compile error. This is because the IF statement needs to have the ‘Then‘ command, which is missing in the below code.
To make sure you see the syntax error whenever there is something missing, you need to make sure Autosyntax check is enabled. To do this, click on ‘Tools’ and then click on ‘Options’. In the options dialog box, make sure that the ‘Auto Syntax Check’ option is enabled.
If the ‘Auto Syntax Check’ option is disabled, VBA will still highlight the line with the syntax error in red, but it will not show the error dialog box.
Compile Error
Compile errors occur when something is missing that is needed for the code to run.
For example, in the below code, as soon as I try to run the code, it will show the following error. This happens as I have used the IF Then statement without closing it with the mandatory ‘End If’.
VBA checks each line as you’re typing the code and highlights the syntax error as soon as the line is incorrect and you hit enter. Compile errors, on the other hand, are only identified when the entire code is analyzed by VBA.
Below are some scenarios where you’ll encounter the compile error:
- Using an IF Statement without the End IF
- Using For statement with the Next
- Using Select statement without using the End Select
- Not declaring the variable (this works only when Option Explicit is enabled)
- Calling a Sub/Function that does not exist (or with wrong parameters)
Run Time Errors
Runtime errors are those that occur when the code is running.
Run time errors will occur only when all the syntax and compile errors are being taken care of.
For example, if you run code that is supposed to open an Excel workbook, but that workbook is unavailable (either deleted or name changed), your code would give you a runtime error.
When a runtime error occurs, it will stop the code and show you the error dialog box.
The message in the Run-time error dialog box is a little more helpful. It tries to explain the problem that can help you correct it.
If you click on the Debug button, it will highlight the part of the code that is leading to the error.
If you have corrected the error, you can click on the Run button in the toolbar (or press F5) to continue running the code from where it left.
Or you can also click on the End button to come out of the code.
Logical Errors
Logical errors would not make your code stop but can lead to wrong results. These could also be the most difficult types of errors to troubleshoot.
These errors are not highlighted by the compiler and need to be manually tackled.
One example of logical error (that I often find myself stuck with) is running into an endless loop.
Another example could be when it gives a result which is wrong. For example, you may end up using a wrong variable in the code or add two variables where one is incorrect.
There are a few ways I use to tackle logical errors:
- Insert Message Box at some place in the code and highlight values/data that can help understand if eberything is going as expected.
- Instead of running the code at one go, go through each line one by one. To do this, click anywhere in the code and press F8. you would notice that each time you press F8, one line gets executed. This allows you to go through the code one line at a time and identify the logical errors.
Using Debug to Find Compile/Syntax Errors
Once you’re done with the code, it’s a good practice to first compile it before running.
To compile a code, click on the Debug option in the toolbar and click on Compile VBAProject.
When you compile a VBA project, it goes through the code and identifies errors (if any).
In case it finds an error, it will show you a dialog box with the error. It finds errors one by one. So if it finds an error and you have corrected it, you need to run compile again to find other errors (if there are).
When you’re code is free of errors, the Compile VBAProject option will be greyed out.
Note that Compiling will only find ‘Syntax’ errors and ‘Compile’ errors. It will NOT find the run-time errors.
When you’re writing VBA code, you don’t want the errors to crop up. To avoid this, there are many error-handling methods you can use.
In the next few sections of this article, I will be covering the methods you can use for VBA error handling in Excel.
Configure Error Settings (Handled Vs Unhandled Errors)
Before you start working with your code, you need to check for one setting in Excel VBA.
Go to the VBA toolbar and click on Tools and then click on Options.
In the Options dialog box, click on the General tab and make sure that within the ‘Error Trapping’ group, ‘Break on Unhandled Errors’ is checked.
Let me explain the three options:
- Break on All Errors: This will stop your code on all types of errors, even when you have used the techniques to handle these errors.
- Break in Class Module: This will stop your code on all unhandled errors, and at the same time, if you’re using objects such as Userforms, it will also break within those objects and highlight the exact line causing the error.
- Break on Unhandled Errors: This will stop your code only for those errors that are not handled. This is the default setting as it ensures any unhandled errors are brought to your notice. If you’re using objects such as Userforms, this will not highlight the line causing the error in the object, but will only highlight the line that’s referring to that object.
So in a nutshell – if you’re just starting with Excel VBA, ensure ‘Break on Unhandled Errors’ is checked.
VBA Error Handling with ‘On Error’ Statements
When your code encounters an error, there are a few things you can do:
- Ignore the error and let the code continue
- Have an error handling code in place and run it when an error occurs
Both of these error handling methods ensures that the end user will not get to see an error.
There are a few ‘On Error’ statements that you can use to get these done.
On Error Resume Next
When you use ‘On Error Resume Next’ in your code, any encountered error will be ignored and the code will continue to run.
This error handling method is used quite often, but you need to be cautious when using it. Since it completely ignores any error that may occur, you may not be able to identify the errors that need to be corrected.
For example, if the below code is run, it will return an error.
This happens because you can not divide a number by zero.
But if I use the ‘On Error Resume Next’ statement in this code (as shown below), it will ignore the error and I will not know that there is an issue that needs to be corrected.
On Error Resume Next should be used only when you clearly know the kind of errors your VBA code is expected to throw and it’s alright to ignore it.
For example, below is the VBA event code that would instantly add the date and time value in cell A1 of a newly inserted sheet (this code is added in the worksheet and not in a module).
While this works great in most cases, it would show an error if I add a chart sheet instead of a worksheet. Since a chart sheet does not have cells, the code would throw an error.
So, if I use the ‘On Error Resume Next’ statement in this code, it will work as expected with worksheets and do nothing with chart sheets.
Note: On Error Resume Next Statement is best used when you know what kind of errors you’re likely to encounter. And then if you think it’s safe to ignore these errors, you can use it.
You can take this code to the next level by analyzing if there was an error, and displaying a relevant message for it.
The below code would show a message box that would inform the user that a worksheet has not been inserted.
‘Err.Number’ is used to get the error number and ‘Err.Description’ is used to get the error description. These will be covered later in this tutorial.
On Error GoTo 0
‘On Error GoTo 0’ will stop the code on the line that causes the error and shows a message box that describes the error.
In simple terms, it enables the default error checking behavior and shows the default error message.
Then why even use it?
Normally, you don’t need to use ‘On Error Goto 0’, but it can be useful when you use it in conjunction with ‘On Error Resume Next’
The below code would select all the blank cells in the selection.
But it would show an error when there are no blank cells in the selected cells.
So to avoid showing the error, you can use On Error Resume next’
Now, it will also show any error when you run the below code:
The problem arises when there is a part of the code where error can occur, and since you’re using ‘On Error Resume Next’, the code would simply ignore it and move to the next line.
For example, in the below code, there would no error prompt:
In the above code, there are two places where an error can occur. The first place is where we are selecting all blank cells (using Selection.SpecialCells) and the second is in the remaining code.
While the first error is expected, any error after that is not.
This is where On Error Goto 0 comes to rescue.
When you use it, you reset the error setting to default, where it will start showing errors when it encounters it.
For example, in the below code, there would be no error in case there are no blank cells, but there would be an error prompt because of ’10/0′
On Error Goto [Label]
The above two methods – ‘On Error Resume Next’ and ‘On Error Goto 0’ – doesn’t allow us to truly handle the error. One makes the code ignore the error and the second one resume error checking.
On Error Go [Label] is a way with which you can specify what you want to do in case your code has an error.
Below is the code structure that uses this error handler:
Note that before the Error handling ‘Label’, there is an Exit Sub. This ensures that in case there are no errors, the sub is exited and the ‘Label’ code is not executed. In case you don’t use Exit Sub, it will always execute the ‘Label’ code.
In the example code below, when an error occurs, the code jumps and executes the code in the handler section (and shows a message box).
Note that when an error occurs, the code has already run and executed the lines before the line causing the error. In the above example, the code sets the value of X as 12, but since the error occurs in the next line, it doesn’t set the values for Y and Z.
Once the code jumps to the error handler code (ErrMsg in this example), it will continue to execute all the lines in and below the error handler code and the exit the sub.
On Error Goto -1
This one is a bit complicated, and in most cases, you’re unlikely to use this.
But I will still cover this as I have faced a situation where this was needed (feel free to ignore and jump to the next section if you’re only looking for basics).
Before I get into the mechanics of it, let me try and explain where can it be useful.
Suppose you have a code where an error is encountered. But all is good as you have one error handler in place. But what happens when there is another error in the error handler code (yeah.. somewhat like the inception movie).
In such a case, you can not use the second handler as the first error has not been cleared. So while you have handled the first error, in VBA’s memory it still exists. And the VBA memory only has a place for one error – not two or more than that.
In this scenario, you can use On Error Goto -1.
It clears the error and frees up VBA memory to handle the next error.
Let’s me explain now by using examples.
Suppose I have the below code. This will throw an error as there is division by zero.
So to handle it, I use an error handler code (with the name ErrMsg) as shown below:
All is good now again. As soon as the error occurs, the error handler is used and shows a message box as shown below.
Now, I expand the code so that I have more code in or after the error handler.
Since the first error has been handled but the second has not been, I again see an error as shown below.
Still all good. The code is behaving in the way we expected it to.
So to handle the second error, I use another error handler (ErrMsg2).
And this is where it doesn’t work as expected.
If you run the above code, it will still give you a run-time error, even after having the second error handler in place.
This happens as we didn’t clear the first error from VBA’s memory.
Yes, we handled it! But it still remains in the memory.
And when VBA encounters another error, it’s still stuck with the first error, and hence the second error handler is not used. The code stops at the line that caused the error and shows the error prompt.
To clear VBA’s memory and clear the previous error, you need to use the ‘On Error Goto -1’.
So if you add this line in the below code and run it, it will work as expected.
The Err Object
Whenever an error occurs with a code, it’s the Err object that is used to get the details about the error (such as the error number or the description).
Err Object Properties
The Err Object has the following properties:
Property | Description |
Number | A number that represents the type of error. When there is no error, this value is 0 |
Description | A short description of the error |
Source | Project name in which the error has occurred |
HelpContext | The help context id for the error in the help file |
HelpFile | A string that represents the folder location and the file name of the help file |
While in most cases you don’t need to use Err object, it can sometimes be useful while handling errors in Excel.
For example, suppose you have a dataset as shown below and for each number, in the selection, you want to calculate the square root in the adjacent cell.
The below code can do it, but since there is a text string in cell A5, it shows an error as soon as this occurs.
The problem with this type of error message is that it gives you nothing about what has gone wrong and where the issue occurred.
You can use the Err object to make these error messages more meaningful.
For example, if I now use the below VBA code, it will stop the code as soon as the error occurs and show a message box with the cell address of the cell where there is an issue.
The above code would give you a lot more information than the simple ‘Type Mismatch’, especially the cell address so that you know where the error occurred.
You can further refine this code to make sure your code runs until the end (instead of breaking at each error) and then gives you a list of cell address where the error occurs.
The below code would do this:
The above code runs until the end and gives the square root of all the cells that have numbers in it (in the adjacent column). It then shows a message that lists all the cells where there was an error (as shown below):
Err Object Methods
While the Err properties are useful to show useful information about the errors, there are two Err methods as well that can help you with error handling.
Method | Description |
Clear | Clears all the property settings of the Err object |
Raise | Generates a run-time error |
Let’s quickly learn what these are and how/why to use these with VBA in Excel.
Err Clear Method
Suppose you have a dataset as shown below and you want to get the square root of all these numbers in the adjacent column.
The following code will get the square roots of all the numbers in the adjacent column and show a message that an error occurred for cell A5 and A9 (as these have text in it).
Note that I have used the Err.Clear method within the If Then statement.
Once an error has occurred and trapped by the If condition, Err.Clear method resets the error number back to 0. This ensures that IF condition only trap the errors for cells where it is raised.
Had I not used the Err.Clear method, once the error occurs, it would always be true in the IF condition, and the error number has not been reset.
Another way of making this work is by using the On Error Goto -1, which resets the error completely.
Err Raise Method
The Err.Raise method allows you to raise a run-time error.
Below is the syntax of using the Err.Raise method:
Err.Raise [number], [source], [description], [helpfile], [helpcontext]
All these arguments are optional and you can use these to make your error message more meaningful.
But why would you ever want to raise an error yourself?
You can use this method when there is an instance of an error (which means that there is going to an error anyway) and then you use this method to tell the user more about the error (instead of the less helpful error message that VBA shows by default).
For example, suppose you have a dataset as shown below and you want all the cells to have numeric values only.
The above code would show an error message that has the specified description and the context file.
Personally, I have never used Err.Raise as I mostly work with Excel only. But for someone who uses VBA to work with Excel along with other applications such as Outlook, Word or PowerPoint, this can be useful.
Here is a detailed article on Err.Raise method in case you want to learn more.
VBA Error Handling Best Practices
No matter how skilled you get a writing VBA code, errors are always going to be a part of it. The best coders are those who have the skills to handle these errors properly.
Here are some best practices you can use when it comes to error handling in Excel VBA.
- Use ‘On Error Go [Label]’ at the beginning of the code. This will make sure any error that can happen from there is handled.
- Use ‘On Error Resume Next’ ONLY when you’re sure about the errors that can occur. Use it with expected error only. In case you use it with unexpected errors, it will simply ignore it and move forward. You can use ‘On Error Resume Next’ with ‘Err.Raise’ if you want to ignore a certain type of error and catch the rest.
- When using error handlers, make sure you’re using Exit Sub before the handlers. This will ensure that the error handler code is executed only when there is an error (else it will always be executed).
- Use multiple error handlers to trap different kinds of errors. Having multiple error handler ensures that an error is properly addressed. For example, you would want to handle a ‘type mismatch’ error differently than a ‘Division by 0’ run-time error.
Hope you found this Excel article useful!
Here are some more Excel VBA Tutorials that you may like:
Источник
In visual basic, the Continue statement is useful to transfer the control immediately to the next iteration of loops such as For, While, Do-While from the specified position by skipping the remaining code.
In the previous section, we learned the Exit statement in vb. The main difference between the Exit statement and Continue
statement is, the Exit statement will completely terminate the loop or statement execution but the Continue
statement will transfer the control immediately to the next iteration of the loop.
Visual Basic Continue Statement Syntax
Following is the syntax of defining the Continue
statement in the visual basic programming language.
Continue { Do | For | While }
In our applications, we can use Continue
statement whenever we want to skip the code execution from the particular position and send back the control to the next iteration of the loop based on our requirements.
Visual Basic Continue Statement Flow Chart
Following is the pictorial representation of Continue
statement process flow in a visual basic programming language.
Now, we will see how to use the Continue statement in For, While, Do-While statements with examples in the visual basic programming language.
Visual Basic For Loop with Continue Statement
In visual basic, by using Continue
keyword, we can skip further code execution and send back the control to the next iteration of For loop statement based on our requirements.
Following is the example of using Continue
statement with For loop in a visual basic programming language.
Module Module1
Sub Main()
For i As Integer = 1 To 4
If i = 3 Then Continue For
Console.WriteLine(«i value: {0}», i)
Next
Console.WriteLine(«Press Enter Key to Exit..»)
Console.ReadLine()
End Sub
End Module
If you observe the above code, we used Continue
statement to pass the control back to the next iteration of For loop whenever the variable i value equals to 3.
When we execute the above visual basic program, we will get the result as shown below.
Suppose you observe the above result whenever the variable i value equals 3. In that case, it skips the further execution of statements and passes the control back to the next iteration of For loop.
This is how we can use Continue
statement in For loop to skip the further execution of statements and send back the control to further iteration of For loop based on our requirements.
Visual Basic While Loop with Continue Statement
In visual basic, we can stop the execution of further statements from the specified position and send back the control to the further iteration of the While loop immediately.
Following is the example of using Continue
keyword in the While loop to pass the control to the next loop iteration in a visual basic programming language.
Module Module1
Sub Main()
Dim i As Integer = 0
While i < 4
i += 1
If i = 2 Then Continue While
Console.WriteLine(«i value: {0}», i)
End While
Console.WriteLine(«Press Enter Key to Exit..»)
Console.ReadLine()
End Sub
End Module
If you observe the above example, whenever the variable (i) value become 2, we are skipping the further execution of statements and passing the control back to the further iteration of the While loop using Continue
statement.
When we execute the above visual basic program, we will get the result as shown below.
This is how we can use the Continue
statement with While loop to pass the control back to the further iteration of loop based on our requirements.
Visual Basic Do-While Loop with Continue Statement
Following is the example of using Continue
keyword in the Do-While loop to pass the control to the next loop iteration in the visual basic programming language.
Module Module1
Sub Main()
Dim i As Integer = 1
Do
Console.WriteLine(«i value: {0}», i)
i += 1
If i = 2 Then Continue Do
Loop While i < 4
Console.WriteLine(«Press Enter Key to Exit..»)
Console.ReadLine()
End Sub
End Module
If you observe the above example, whenever the variable (i) value become 2, we are skipping the further execution of statements and passing the control back to the further iteration of a loop using Continue
statement.
When we execute the above visual basic program, we will get the result as shown below.
i value: 1
i value: 2
i value: 3
Press Enter Key to Exit..
This is how we can use the Continue
statement with Do-While loop to pass the control back to the further iteration of loop based on our requirements.