Excel VBA Loop

Excel loops can help you perform repetitive tasks with a simple excel vba macro. This keeps your excel vba code simple and at the same time your vba code runs more efficiently. Excel loops are used when you need to run same functions or calculations in a range of cells. Instead of repeating the same operation as many times as you have cells in your data you can use one simple loop statement.

Loops are also useful when your ranges are dynamic so the number of cells in your range changes. You can use loops to perform the required action to the number of cells you currently have in your excel worksheet.

There are many different ways to create excel vba loops. The following example demonstrates an approach to create your excel vba loops:

Sub MyLoop()

Related: Excel


       Dim i As Integer

       For i = 1 To Application.CountA(Range(“A:A”))

              ActiveCell.FormulaR1C1 = “=RC[-1]*10”

              ActiveCell.Offset(1, 0).Select

       Next i

End Sub

This excel vba code multiplies each cell value in column A with 10 and enters the results in column B next to each cell from column A. The data starts in cell A1 and the excel vba loop counts the number of cells in Column A and based on hte number of cells it performs the required action in Column B respectivelly. So regardless of the number of cells in your range (column A) the macro will always work based on your data.

This excel macro select the B1 cell and starts working in this cell by moving down one cell at a time and performs the calculation for as many times as there are non-blank cells in column A. This is defined by the value of i in the MyLoop macro.