How to Loop in Excel

Macros are used to perform repetitive actions over and over automatically. A loop is an effortless and powerful macro. A cell or range of cells reference on a worksheet tells Excel where to look for the values or data you want to use in a formula. You can use the data that is contained in the different parts of a worksheet in a formula. You can also use the value from one cell in several formulas. You would have reasons wanting to loop through range of cells, such as to change cell color based on a condition, to create sets of data and to generate reports. There are multiple ways to loop through cell ranges. Here is the easiest way to loop, which is For Each Loop.

Instructions

    • 1

      Create a new workbook in Microsoft Excel.

    • 2

      Go to Tools > Macro > Visual Basic Editor, or press Alt + F11 to open the Visual Basic Editor.

    • 3

      Go to Insert > Module. A blank module window will appear inside the main window of the Visual Basic Editor.

    • 4

      Give your macro a name. Type: Sub My Macro () End Sub

    • 5

      Add the code below between the Sub line and the End Sub lines. ---------------------------------------------------- For Each MyCell in Selection If MyCell.Value Like “OK” Then MyCell.Font.Bold = True End If Next ---------------------------------------------------- This means A For Each… Next Loop makes the word “OK” darker than other text, everywhere in a selection. The total code will be: Sub My Macro () For Each MyCell in Selection If MyCell.Value Like “OK” Then MyCell.Font.Bold = True End If Next End Sub

    • 6

      Go to Tools > Macro > Record New Macro to record the macro you just created.

    • 7

      Edit the code by going to Tools > Macro > Macros to find your macro. Select it and click Edit to see the code.

Related Searches:

Comments

You May Also Like

  • Looping in Excel

    Looping in Microsoft Excel allows users to automate the entry of data into columns and rows. Learn how to create a data...

  • How to Create a For Next Loop in Excel VBA

    Create a For Next Loop in Excel VBA by typing in code that runs a series of operations as soon as it...

  • Loop Functions in VBA Excel

    Loop Functions in VBA Excel. Loops are statements that continue to execute until a condition is met. There are a few main...

  • Nested Loop Functions in Excel

    Nested Loop Functions in Excel. Microsoft Excel is a powerful application that offers many tools for analyzing and manipulating data. The formula...

  • Excel VBA Tutorial on Loops

    VBA is Microsoft's Visual Basic programming language. It is most commonly associated with macros used in Excel spreadsheets and sometimes even report...

  • Learning VBA for Excel

    Microsoft includes a simplified version of the Visual Basic programming language called Visual Basic for Applications (VBA) in some of its Office...

  • How to Use a Loop to Change Multiple Cells in Visual Basic

    The most powerful feature of a computer is its capacity to automate, and that is based on a simple programming concept called...

  • How to Nest Formulas in Excel

    Microsoft Office Excel is a spreadsheet software that allows the user to create workbooks that analyze information and perform calculations. The data...

Related Ads

Featured