

To run the macro, position the insertion point in the line that reads "Sub WorksheetLoop2()," and press F5. ' This line displays the worksheet name in a message box. ' Loop through all of the worksheets in the active workbook. ' Declare Current as a worksheet object variable.
VISUAL BASIC LOOPING CODE
You can also loop through all of the worksheets in the workbook by using a 'For Each' loop.Įnter the following macro code into a new module sheet. Note that this macro will only display worksheet names it will not display the names of other types of sheets in the workbook.

The macro will loop through the workbook and display a message box with a different worksheet name each time it runs through the loop. To run the macro, position the insertion point in the line that reads "Sub WorksheetLoop()," and press F5. The Do.Loop executes a block of statements for as long as a condition is True, or until a condition becomes True. ' the loop by displaying the worksheet name in a dialog box.
VISUAL BASIC LOOPING HOW TO
' The following line shows how to reference a sheet within ' Set WS_Count equal to the number of worksheets in the active Type the following macro code into a new module sheet. To try the sample macro, follow these steps: Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This macro also displays the name of each worksheet. This article contains a Microsoft Visual Basic for Applications macro (Sub procedure) that loops through all the worksheets in the active workbook. It is possible to edit the value of the counter variable within a for.Visual Basic 2010 Express More. When X reaches the end of the range in the range (100 in the example above), the loop will cease to execute, and the program will continue to the code beyond the next statement. For instance, for X = 100 to 1 step -1 would decrease the value of X from 100 to 99 to 98, etc.) (Note that the step can be a negative value. When the step is not explicitly stated, 1 is used by default. For example, if you use a step 3, X will increase from 1 to 4, then to 7, 10, 13, and so on. To change the amount by which the counter variable increases on each iteration, simply change the value of the step. In this instance, X will be increased to 3 on the second iteration, 5 on the third, etc. Upon reaching the next statement, the program returns to the for statement and increases the value of the counter by the step. The program then executes any code between the for and next statements normally. When a For.Next loop is initialized, the counter is set to the first number in the range in this case, X is set to 1. There is no ambiguity in doing this if code is correctly indented.

The variable reference in the Next part of the statement is optional and it is common practice to leave this out.

In this example, X is the counter, "1 to 100" is the range, and "2" is
