VBA: Macro to Update all Worksheets in Workbook

August 14th, 2004 | Categories: VBA | Tags: , , , ,
-->

Macro’s 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.

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

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
Can't get the tutorial to work for you? Need help with your code?
Get answers right away at our AE Excel Support Forums!
No comments yet.