Assignments Of Class 11: Introduction to Macros and VBA,
Assignment 1: Record and Run a Macro
Objective: Record a macro to format a range of cells and run it.
Instructions:
- Open a new Excel workbook.
- Record a macro to perform the following actions:
- Select the range A1 to A10.
- Change the font color to red.
- Change the font size to 14.
- Fill the background color with light green.
- Stop the macro recording.
- Run the recorded macro to apply the formatting.
Solution:
- Go to the View tab, click Macros, and select Record Macro.
- Name the macro FormatCells, assign a shortcut (e.g., Ctrl + Shift + F), and click OK.
- Select the range A1 to A10 and perform the formatting actions.
- Stop recording the macro.
- To run the macro, use the shortcut or go to View > Macros > View Macros, select FormatCells, and click Run.
Assignment 2: Create a Basic VBA Script
Objective: Write a VBA script to calculate the total of a specified range and display the result in a message box.
Instructions:
- Open the VBA Editor (press ALT + F11).
- Create a new module (Right-click on any existing module in the "Modules" section > Insert > Module).
- Write a script that:
- Sums the values in the range B1 to B10.
- Displays the total in a message box.
- Run the script to verify it works.
Solution:
Sub CalculateTotal()
Dim total As Double
total = Application.WorksheetFunction.Sum(Range("B1:B10"))
MsgBox "The total of the range B1 to B10 is: " & total
End Sub
- Run the script by pressing F5 in the VBA Editor or from Excel by going to View > Macros and selecting the CalculateTotal macro.
Assignment 3: Edit a Recorded Macro
Objective: Modify a previously recorded macro to include additional formatting.
Instructions:
- Record a macro that changes the font to italic and adds a border to the range C1 to C5.
- Open the VBA Editor and find the recorded macro.
- Edit the macro to also change the font color to blue and the fill color to light yellow.
- Save and run the edited macro.
Solution:
- Record the macro as FormatC1C5 by selecting the range C1 to C5 and applying italic font and borders.
- In the VBA Editor, the recorded macro might look like this:
Sub FormatC1C5()
Range("C1:C5").Font.Italic = True
Range("C1:C5").Borders.LineStyle = xlContinuous
End Sub
- Edit the macro as follows:
Sub FormatC1C5()
Range("C1:C5").Font.Italic = True
Range("C1:C5").Borders.LineStyle = xlContinuous
Range("C1:C5").Font.Color = RGB(0, 0, 255) ' Change font color to blue
Range("C1:C5").Interior.Color = RGB(255, 255, 153) ' Change fill color to light yellow
End Sub
- Save the changes and run the macro.
Assignment 4: Create a Macro with a Loop
Objective: Write a macro that applies formatting to a range of cells using a loop.
Instructions:
- Open the VBA Editor.
- Write a macro that:
- Loops through the range D1 to D10.
- Changes the background color of even-numbered rows to light gray and odd-numbered rows to white.
- Run the macro to see the formatting applied.
Solution:
Sub FormatRows()
Dim i As Integer
For i = 1 To 10
If i Mod 2 = 0 Then
Range("D" & i).Interior.Color = RGB(211, 211, 211) ' Light gray for even rows
Else
Range("D" & i).Interior.Color = RGB(255, 255, 255) ' White for odd rows
End If
Next i
End Sub
- Run the macro to apply the alternating colors to the rows in the range D1 to D10.