In this Article
Macros applied to an entire workbook can be handy for extracting data from multiple sheets to a new workbook, applying advanced formatting to all sheets, or many other reasons. Here is the basic syntax and a working code example for VBA that works on every sheet in a workbook.
Loop Through Every Sheet in Workbook
The basic syntax for looping through every sheet in a workbook and applying VBA code to it is
For Each ws In Worksheets 'Update or do something here Next
Update Every Worksheet in Workbook Using VBA
For a functioning example, copy and paste the following code into a module and run it. The result is text placed in cell A1 of every sheet in your workbook.
Public Sub DoToAll() 'Declare our variable Dim ws As Worksheet For Each ws In Worksheets 'place code between the For and Next 'for what you would like to do to 'each sheet ws.Range("A1") = "AutomateExcel.com" Next End Sub
VBA Coding Made Easy
Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!