REAL-TIME

VBA Projects

Full Access with Source Code

  • Designed and Developed by PNRao

  • Full Access with VBA Source Code

  • Well Commented Codes Lines

  • Creative and Professional Design

120+ PROFESSIONAL

Project Management Templates

120+ PM Templates Includes:
  • 50+ Excel Templates

  • 50+ PowerPoint Templates

  • 25+ Word Templates

Effortlessly Manage Your Projects

Seamlessly manage your projects with our powerful & multi-purpose templates for project management.

Share Post

VBA For…Next Statement is used to create a loop that repeats a set of instructions for a specified number of times. In this blog post, we will discuss the purpose and syntax of the For…Next statement, provide the examples of its usage, and highlight important notes and remarks. Let’s get started!

VBA For…Next Statement

Purpose and Syntax of VBA For…Next Statement

The main purpose of the For…Next statement is to automate repetitive tasks by executing a block of code a specific number of times. It allows you to create a loop that iterates through a set of data, making it a useful tool for data manipulation and analysis. The basic syntax of the For…Next statement is as follows:

For counter = start To end Step [increment]
    'insert code to be executed
Next counter

The ‘counter’ variable is used to keep track of the number of iterations, ‘start’ is the initial value of the counter, ‘end’ is the final value of the counter, and ‘increment’ is the value by which the counter is increased after each iteration. The ‘Step’ keyword can be omitted if the counter is incremented by 1.

Examples of For…Next Statement

Let’s look at the top 5 examples of using the For…Next statement in VBA:

Example 1: Counting Numbers

Suppose we want to print the numbers 1 to 10 in the first column of an Excel worksheet. We can use the For…Next statement to achieve this as shown in the code below:

Sub CountNumbers()
For i = 1 To 10
    Cells(i, 1) = i
Next i
End Sub

In this example, the counter starts at 1 and increments by 1 until it reaches 10. The loop will execute 10 times, and each time the value of ‘i’ is printed in the first column.

Example 2: Summing Numbers

The For…Next statement is also useful for performing calculations. Let’s say we have a column of numbers in an Excel worksheet and we want to find their sum. We can use the For…Next statement as shown in the code below:

Sub SumNumbers()
Dim total As Double
For i = 1 To 10
    total = total + Cells(i, 1)
Next i
Cells(11, 1) = total
End Sub

In this example, the value of ‘total’ is increased by the value in each cell in the first column, and the final sum is displayed in cell A11.

Example 3: Nested Loops

Nested loops are loops within loops and are often used when dealing with two-dimensional data. Let’s say we have a table with student names in column A and their test scores in column B. We want to calculate the average score for each student. We can use the For…Next statement as shown in the code below:

Sub AverageScore()
For i = 2 To 11
    total = 0
    For j = 2 To 5
        total = total + Cells(i, j)
    Next j
    Cells(i, 6) = total / 4
Next i
End Sub

In this example, the outer loop iterates through each row from 2 to 11, and the inner loop calculates the total score for each student by adding the values in columns B to E. The average score is then calculated and displayed in column F.

Example 4: Changing Cell Color

Using the For…Next statement, we can also change the formatting of cells in Excel. Let’s say we have a table with numbers in column A, and we want to highlight all the even numbers in red. We can achieve this using the For…Next statement as shown in the code below:

Sub HighlightEvenNumbers()
For i = 1 To 10
    If Cells(i, 1) Mod 2 = 0 Then
        Cells(i, 1).Font.Color = RGB(255, 0, 0) 'change font color to red
    End If
Next i
End Sub

In this example, the ‘If’ statement checks if the number in each cell is even. If it is, then the font color for that cell is changed to red.

Example 5: Looping Through an Array

Arrays are used to store multiple items in a single variable. We can use the For…Next statement to loop through each element in an array. Let’s say we have an array called ‘countries’ with the names of different countries. We want to print each country name in a new line using the For…Next statement as shown below:

Sub PrintCountries()
Dim countries(3) As String
countries(0) = "USA"
countries(1) = "Canada"
countries(2) = "Australia"
countries(3) = "Japan"
For i = 0 To UBound(countries)
    Debug.Print countries(i)
Next i
End Sub

In this example, the For…Next statement loops through the array, and each element is printed in the Immediate Window.

Important Notes & Remarks

  •  The For…Next statement may cause an infinite loop if the counter is not incremented properly. It is important to ensure that the counter reaches its final value to avoid this.
  • The ‘Step’ keyword can be negative, allowing the counter to decrease after each iteration.
  • The ‘Exit For’ statement can be used to end a For…Next loop prematurely.
  • The ‘For Each…Next’ statement is used to loop through a collection of objects, such as cells in a range or files in a folder.

In conclusion, the For…Next statement is a powerful tool that allows for repetitive tasks to be automated in VBA. It is easy to use and can be applied in a variety of situations. In this blog post, we discussed the purpose and syntax of the For…Next statement, provided examples of its usage, and highlighted important notes and remarks.

If you have any feedback or views on this post, we would love to hear from you. Are there any other examples of the For…Next statement that you would like to share? Let us know in the comments below. Thank you for reading!

Effortlessly Manage Your Projects and Resources
120+ Professional Project Management Templates!

A Powerful & Multi-purpose Templates for project management. Now seamlessly manage your projects, tasks, meetings, presentations, teams, customers, stakeholders and time. This page describes all the amazing new features and options that come with our premium templates.

Save Up to 85% LIMITED TIME OFFER
Project Management Templates

All-in-One Pack
120+ Project Management Templates

Essential Pack
50+ PM Templates

Excel Pack
50+ Excel PM Templates

PowerPoint Pack
50+ Excel PM Templates

MS Word Pack
25+ Word PM Templates

Ultimate Project
Management Template
Ultimate Resource
Management Template
Project Portfolio
Management Templates
Categories: VBA StatementsTags: , Last Updated: September 28, 2023

Leave A Comment