12.5 Sub Procedures
A macro is a Sub procedure: it starts with Sub Name() and ends with End Sub. Comments start with an apostrophe '.
Option Explicit
Sub HelloBlinkit()
' My first macro
Worksheets("Report").Range("A1").Value = "Blinkit Maharashtra - Daily Report"
Worksheets("Report").Range("A2").Value = Date
Worksheets("Report").Range("A2").NumberFormat = "dd-mm-yyyy"
MsgBox "Report header ready!", vbInformation, "Done"
End Sub
- Run: put the cursor inside the Sub and press F5.
- One Sub can call another:
Call FormatHeaderor justFormatHeader. Private Subhides a macro from the Macros dialog (useful for helper procedures).- Line continuation: end a line with a space and underscore
_to continue on the next line.
Ravindra Bagale's Tip
Macro run kelyavar Undo (Ctrl + Z) chalat nahi – khup students ekda chukicha macro chalvtat aani data kharab hota. Navin macro nehmi file chi copy (backup) karun test kara. Ani Sub che nav arthapurna theva (FormatDailyReport), Macro1 nako.
Practice task
Write a Sub StampReport that writes "Prepared by Ravindra Bagale" in Report!A3 and the current date-time in A4 formatted dd-mm-yyyy hh:mm.