Tag Archives: Loop

VBA Macro: How to add and rename worksheets

In following demonstration video I will implement Excel Visual Basic macro that reads worksheet names from the cells A1, A2, A3, … in sheet “Sheet1” and creates new worksheets with correct names.

For simplicity there is no error handling etc and following simple code does the trick:

Sub createsheets()
    Dim newsheet As Worksheet
    Dim r As Integer
    r = 1
    Do While Sheets("Sheet1").Cells(r, 1).Value <> ""
        Set newsheet = Sheets.Add
        newsheet.Name = Sheets("Sheet1").Cells(r, 1).Value
        r = r + 1
    Loop
End Sub

Excel VBA macro: How to loop through selection

In following video I will show an Excel VBA macro example about how to loop through each selected cells and write location information to cells.

We can use Excel’s Selection object (Range type containing selected cells of the active worksheet) and cycle through selected cells by using For-Each-Next loop:

For Each c in Selection
    ...
    c.Value = ...
    ...
Next

c stands for single cell (Range type).

Following video clarifies this.