Dear Reader,
In this article, I am going to write a very small trick about Excel Macro. In Excel Macro, you might have come across the situation where you need to Traverse all the Sheets of any Workbook one by one. You might have seen the below two methods to do so. As part of this article we will discuss what exactly the difference is between both the below methods:
1.
Sub TraverseWorkSheet()
Dim ws As Worksheet
For Each Worksheet In Worksheets
MsgBox (Worksheet.Name)
Next
End Sub
2.
Sub TraverseSheet()
For Each Sheet In Sheets
MsgBox (Sheet.Name)
Next
End Sub
Difference:
If you are using the KeyWord Sheet in Sheets then it will traverse all the WorkSheets of that Workbook, including Charts, Macro Sheet, International Macro Sheets etc..
See Example 2, above
In other hand if you use Keyword WorkSheet in WorkSheets then it will traverse only the WorkSheets. It will ignore all the Other Sheets like Charts, Macro Sheet, International Macro Sheets etc.
Very good tip.thanks