Using For Each to process the elements in a collection

for each

The Office applications such as Excel and MS Access are crammed with collections of their distinct element types, such as Workbooks, Sheets, Comments, Forms, Fields, etcetera

For Each sheet in a Workbook

Code VBA add-in makes it easy to insert For Each loops. The image on the right shows it included in the Workbook menu, for all other object classes and collections similar menus exists. When selected the code block below is inserted.

Dim wb As Workbook: Set wb = ActiveWorkbook
Dim ws As Worksheet
For Each ws In wb.Worksheets
Next ws

For Each control in an Access Form

Dim frm As Form: Set frm = Screen.ActiveForm
Dim ctl As Control
For Each ctl In frm.Controls
Next ctl