EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Logical Functions VBA Break for Loop
Secondary Sidebar
VBA Logical Functions
  • VBA Logical
    • VBA Do While Loop
    • VBA IF Statements
    • VBA Loops
    • VBA Select Case
    • VBA Else If
    • VBA While Loop
    • VBA Select Cell
    • VBA Break for Loop
    • VBA IF Not
    • VBA Do Until Loop
    • VBA OR
    • VBA Boolean
    • VBA Like
    • VBA For Each Loop
    • VBA Operators
    • VBA Selection
    • VBA DoEvents
    • VBA Do Loop
    • VBA Not
    • VBA With
    • VBA AND

VBA Break for Loop

By Ashwani JaiswalAshwani Jaiswal

VBA Break for loop

Excel VBA Break For Loop

VBA Break is used when we want to exit or break the continuous loop which has certain fixed criteria. For loop is within the scope range defines the statements which get executed repeatedly for a fixed number of time. Sometimes when we use any loop condition in VBA, it often happens that the code keeps on running without an end or break. To avoid this kind of situations it is recommended to use some breaks or exit functions by which we can exit from the loop with the measurable output from the macro.

How to Use Excel VBA Break For Loop?

We will learn how to use a VBA Break for Loop with few examples in Excel.

You can download this VBA Break For Loop Excel Template here – VBA Break For Loop Excel Template

Example #1 – VBA Break For Loop

We will see an example where when using For loop the criteria arrange will not be broken. But to fix this we will use the If-End loop. For this, we need a Module where we will write this code.

Follow the below steps to use Break for Loop in Excel VBA.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,258 ratings)

Step 1: Open a new module go to the VBA window and in that select Module from the Insert menu option.

New Module

Step 2: This will take us to a new module in VBA. Now in that module write Subcategory in the name of VBA Break For Loop or in any other name which defines its meaning.

Code:

Sub VBABreak1()

End Sub

Break loop example 1

Step 3: Now define a DIM as any alphabet or word. This gives it an identity. Here we are using the alphabet “A” below.

Code:

Sub VBABreak1()

   Dim A 

End Sub

Break loop example 2

Step 4: Now assign it as Integer which is used for numbers only as shown below.

Code:

Sub VBABreak1()

   Dim A As Integer

End Sub

Break loop example 3

Step 5: Now assign a numeric value to Integer A. Let’s say it is 10 as shown below.

Code:

Sub VBABreak1()

   Dim A As Integer
   A = 10

End Sub

Break loop example 4

Step 6: Now start a For loop and consider any starting value of A. It is better to take this value as Zero. This becomes easy for calculation.

Code:

Sub VBABreak1()

   Dim A As Integer
   A = 10

   For A = 0 To A Step 2

End Sub

Break loop example 5

Step 7: And to print the value stored in A, we will use a message box that will fetch its value. You can use any form of a sentence as well to make the message box more meaningful which is optional. We have used it on as shown below.

Code:

Sub VBABreak1()

   Dim A As Integer
   A = 10

   For A = 0 To A Step 2
     MsgBox ("The value is A is : " & A)
   Next

End Sub

Break loop example 6

Step 8: Now compile the code step-by-step by using functional key F8 and after that run the code by clicking on the Play button as shown below. We will see the output in every step will be from 0 to 10 in the step gap of 2 as shown below.

break vba 1

Step 9: Now at last close For Loop and also use Next at the end. The “Next” is used when we are using numbers in the code.

Code:

Sub VBABreak1()

Dim A As Integer
   A = 10

   For A = 0 To A Step 2
      MsgBox ("The value is A is : " & A)

      Exit For
   Next

End Sub

Break loop example 7

Step 10: This shows that the loop is not complete and we need to fill the exit criteria as well to exit from For loop. And for that, we will use If-End Loop condition. So consider writing If loop for same Integer A. Here choose the limit till you want to run the loop. We have considered it as 4.

Code:

Sub VBABreak1()

Dim A As Integer
   A = 10

   For A = 0 To A Step 2
      MsgBox ("The value is A is : " & A)
   If A = 4 Then

      Exit For
   Next

End Sub

VBA IF condition

Step 11: Now let’s add one more criterion in which we will use how many times it would get multiplied by any number. Let’s say it is 10. You can choose any other number instead of 10. But using 10 makes easy to understand the multiplication loop.

Code:

Sub VBABreak1()

Dim A As Integer
   A = 10

   For A = 0 To A Step 2
      MsgBox ("The value is A is : " & A)
   If A = 4 Then
   A = A * 10

      Exit For
   Next

End Sub

VBA Break

Step 12: Now again use name message box as we have used for For loop and at lastly close the If Loop with End if statement as shown below.

Code:

Sub VBABreak1()

Dim A As Integer
   A = 10

   For A = 0 To A Step 2
      MsgBox ("The value is A is : " & A)
   If A = 4 Then
   A = A * 10
   MsgBox ("The value is A is : " & A)

      Exit For
      End If
   Next

End Sub

MsgBox 2

Step 13: Again compile the code and run it. For Loop will start from showing a message with value 0 and then followed by 2, 4 and ending it 40. Which means For Loop got broken when at 3rd iteration and took the last value as 40.

break vba 2

Example #2 – VBA Break For Loop

In this example, we will apply For loop and will see how to break the loop once it satisfies the criteria.

Step 1: Open a new module from the Insert menu option and give it a Subcategory in any name or better in a sequential manner as shown below.

Code:

Sub VBABreak2()

End Sub

VBA Break loop example 1

Step 2: Now define a DIM A as Integer. Using Integer for test allow us to implement the code faster and debug easy.

Code:

Sub VBABreak2()

  Dim A As Integer

End Sub

Dim Integer

Step 3: Now start a For loop for defined integer and give it a range from any row count starting from 1. We have chosen till 20.

Code:

Sub VBABreak2()

  Dim A As Integer
   For A = 1 To 20

End Sub

For condition

Step 4: Now give a range or location from where the numbers are getting printed in sequence. We used ThisWorkBook to select current opened file and Worksheets(1) for selecting the first sheet of an opened workbook as shown below. Now to close the For loop write Next at the end as shown below.

Code:

Sub VBABreak2()

 Dim A As Integer
  For A = 1 To 20

ThisWorkbook.Worksheets(1).Cells(A, 1) = A

Next

End Sub

Worksheet

Step 5: Run the above code manually or using the shortcut key F5. We can see that the code has given number count from 1 to 20.

Break loop Out

Step 6: Now to break the loop in between from 0 to 20, we will use If-End if loop condition where we will give the criteria to fall in as shown below.

Code:

Sub VBABreak2()

  Dim A As Integer
   For A = 1 To 20

   If

   End If

  ThisWorkbook.Worksheets(1).Cells(A, 1) = A

Next

End Sub

New If End Condition

Step 7: Now write the criteria, If A is greater than 9 (Or any number but less than 20) then Exit For as shown below. We suppose For Loop should get broken when loop reaches 9.

Code:

Sub VBABreak2()

 Dim A As Integer
  For A = 1 To 20

  If A > 9 Then
  Exit For

  End If

 ThisWorkbook.Worksheets(1).Cells(A, 1) = A

Next

End Sub

Exit For

Step 8: Now compile the code to find the error. If no error found then run it. We will see for integer A values from 1 to 20 have been filled till the code exited or broken at position 9.

break vba 3

Pros of VBA Break For Loop

  • Exit or Break can be used not only for For loop but in many other loops as well such as Do-While.
  • This is most frequently used among all the loop conditions available in VBA.
  • For Loop is quite easy and can work even without Exit criteria.

Things to Remember

  • For loop can work without giving the Exit criteria. It can work fine until the criteria without Exit is breaking the loop to end.
  • It is important to Break any loop which doesn’t satisfy the exit criteria by using Exit in the loop.
  • After writing the code, save the file as Macro Enable Excel so that written code will be preserved.

Recommended Articles

This has been a guide to VBA Break For Loop. Here we discuss how to use Excel VBA Break For Loop along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA InStr
  2. VBA Integer
  3. VBA Select Cell
  4. VBA Transpose
Popular Course in this category
All in One Excel VBA Bundle (120+ Courses)
  120+ Online Courses |  500+ Hours |  Verifiable Certificates |  Lifetime Access
4.9
Price

View Course
3 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more