Practice by topic / Python & code / Excel VBA Macros

Excel VBA Macros Exercises

VBA macros automate repetitive Excel work: formatting, copying, looping over rows. These exercises have you write real Sub procedures. These 3 exercises build up step by step, starting with your first sub, loop through numbers, set a cell's value.

3 of 3 free · no sign-up needed · instant feedback

1

Your First Sub

Free

Write a VBA Sub procedure named Greet that shows a message box saying "Hello".

Show the solution

Structure: Sub Greet() MsgBox "Hello" End Sub

Right — Sub Greet() ... MsgBox "Hello" ... End Sub defines a callable macro that pops up a message box.

Try it in the live editor →
2

Loop Through Numbers

Free

Write a VBA For loop that goes from 1 to 10 and prints each number to the Immediate window using Debug.Print.

Show the solution

Structure: For i = 1 To 10 Debug.Print i Next i

Nice — "For i = 1 To 10 ... Debug.Print i ... Next i" loops through each number and logs it.

Try it in the live editor →
3

Set a Cell's Value

Free

Write a VBA line that sets the value of cell A1 on Sheet1 to "Done".

Show the solution

Use: Sheet1.Range("A1").Value = "Done"

Good — Sheet1.Range("A1").Value = "Done" directly addresses the sheet and cell you want to update.

Try it in the live editor →