Ravindra BagaleCourses & study guides मराठी Track your progress

12. Macros and VBA

Come friends, today we teach Excel to do the work for us – Macros and VBA (Visual Basic for Applications). The same formatting, the same trimming, the same copy-paste every morning? Build a macro once and finish the job with one button! Recording macros, the VBA Editor, variables (a named place that stores a changing value), loops (doing the same action again and again), practical macros, user-defined functions and debugging – step by step. Do not worry: code looks hard the first time, but by the end of this module you will write your own macros.

What you will learn in this module

  • The Developer tab, Trust Center macro security and the .xlsm format
  • Recording macros with absolute and relative references; running them from a button or shortcut
  • The VBA Editor: modules, Project Explorer, Immediate window
  • Sub procedures, variables and data types, Range/Cells/Worksheets, MsgBox/InputBox
  • If, Select Case, For, For Each and Do loops, and finding the last row
  • Practical macros on our data, a delivery-fee user-defined function, On Error handling, debugging and the Personal Macro Workbook

Live story: The same clean-up every morning

Suppose every morning Rani trims spaces, formats the Amount column as rupees and splits the orders into one sheet per city – about 25 minutes of clicking. One day she forgets the trim and the Pune total is short. A macro does the same steps in the same order in two seconds. This chapter records macros, then writes VBA with variables, loops, a delivery-fee function and error handling.

All numbers in this story are fictional practice data.

Macro safety

Macros can run any code on your computer. Enable macros only in files you trust. Microsoft 365 blocks macros by default in files downloaded from the internet or received as e-mail attachments (the Mark of the Web). Practise on your own files. All code in this module is plain VBA for Excel on Windows; some features (Personal Macro Workbook path, ActiveX controls) differ on Mac.

Concepts in this chapter

  1. 12.1Developer Tab, Macro Security and .xlsm
  2. 12.2Recording a Macro: Absolute vs Relative
  3. 12.3Running Macros: Button, Shortcut and the Macros Dialog
  4. 12.4The VBA Editor (VBE)
  5. 12.5Sub Procedures
  6. 12.6Variables and Data Types
  7. 12.7Range, Cells and Worksheets
  8. 12.8MsgBox and InputBox
  9. 12.9If and Select Case
  10. 12.10Loops: For, For Each and Do
  11. 12.11Finding the Last Row (and Column)
  12. 12.12Practical Macros on Our Data
  13. 12.13User-Defined Functions: a Delivery Fee Function
  14. 12.14Error Handling with On Error
  15. 12.15Debugging: F8, Breakpoints, Immediate and Locals Windows
  16. 12.16The Personal Macro Workbook
  17. 12.17Office Scripts: What They Are and Who Can Use Them
  18. 12.18Recording an Office Script and Reading the Code
  19. 12.19Writing a Small Office Script: Colour the Late Orders
  20. 12.20Running a Script from Power Automate: On a Schedule or When a File Arrives
  21. 12.21VBA vs Office Scripts: Which One When?

Review and practice

  1. ★Practice and Answer Key: An Office Script that Summarises Orders by Status
  2. ★Practice and Answer Key: A Macro that Tags Late Orders

The chapter recap is at the end of the last concept page.

Quick revision cards

Quick revision

Revise this chapter in two minutes. Save the cards on your phone or download them as PNG files. All names and numbers are made up for practice.

Revision card: Macros: saving, recording and the last row. Example data table with Find the last filled row in column A: 5; Save a workbook with a macro: .xlsm; Relative recording: acts on A3.
Card 1 of 2: Macros: saving, recording and the last row · Download PNG
Revision card: VBA loops: For, Do While, If. Example data table with For loop doubles each price: 200, 400, 600; Do While stops at a blank: 3 rows done; If for one choice: High.
Card 2 of 2: VBA loops: For, Do While, If · Download PNG

More cards for every chapter: Ravi Sir's Cards.