How to use two methods to loop through worksheets automatically using VBA. When we wish to copy or format data in multiple worksheets, we need to access them first. Watch the video below:
Here’s the complete VBA code to access all or multiple worksheets in a workbook to perform further actions:
Sub LoopWorksheet1()
Dim worksheetcount As Long
Dim i As Long
‘set worksheetcount equal to the number of worksheets in our active workbook
worksheetcount = ActiveWorkbook.Worksheets.Count
For i = 1 To worksheetcount
‘insert relevant code to make a range bold
‘we display the worksheet name via a message box
MsgBox ActiveWorkbook.Worksheets(i).Name
Next i
End Sub
Sub LoopWorksheets2()
‘declare worksheet object variable
Dim ws As Worksheet
‘looping process starts
For Each ws In Worksheets
‘insert code to do some action
‘display each sheet name
MsgBox ws.Name
Next
End Sub
Further reading:
Merge Data from Worksheets into Master Worksheet
Apply Autofilter Across Multiple Excel Worksheets
Watch this video on YouTube.