15. Final Project: Blinkit Maharashtra Monthly Report
15.6 Stage 5 – A Small Macro
Steps in Excel
- Save as Excel Macro-Enabled Workbook (*.xlsm).
- Developer › Code › Visual Basic (Alt + F11) › Insert › Module › paste the macro below.
- Insert › Illustrations › Shapes › draw a button "Refresh & Export PDF" › right-click › Assign Macro… ›
RefreshAndExport. - Test with Status filter/slicers cleared; check the PDF opens.
Sub RefreshAndExport()
Dim pdfPath As String
On Error GoTo ErrHandler
ThisWorkbook.RefreshAll
Application.CalculateUntilAsyncQueriesDone
pdfPath = ThisWorkbook.Path & Application.PathSeparator & _
"Blinkit_Dashboard_" & Format(Date, "dd-mm-yyyy") & ".pdf"
ThisWorkbook.Worksheets("Dashboard").ExportAsFixedFormat _
Type:=xlTypePDF, Filename:=pdfPath, Quality:=xlQualityStandard, _
OpenAfterPublish:=True
MsgBox "Dashboard refreshed and saved as:" & vbCrLf & pdfPath, vbInformation
Exit Sub
ErrHandler:
MsgBox "Could not finish: " & Err.Description, vbExclamation
End Sub
Worked example. Clicking the button on 30-09-2026 creates Blinkit_Dashboard_30-09-2026.pdf in the same folder as the workbook and opens it. If the workbook has never been saved, ThisWorkbook.Path is empty – save it first (the error handler reports the problem instead of crashing).
Ravindra Bagale's Tip
Many students save a macro in .xlsx and when they reopen the file the macro is gone. If there is a macro, always save as .xlsm, and put On Error GoTo in the code so the user gets a message they can understand. Keep a backup of the file before running a macro – Undo doesn't work after a macro.
Ravindra Bagale's Tip – मराठी
बरेच students macro .xlsx मध्ये save करतात आणि file पुन्हा उघडल्यावर macro गायब होतो. Macro असेल तर नेहमी .xlsm मध्ये save करा, आणि code मध्ये On Error GoTo ठेवून user ला समजेल असा message द्या. Macro चालवण्यापूर्वी एकदा file चा backup ठेवा – macro नंतर Undo काम करत नाही.
Ravindra Bagale's Tip – हिंदी
बहुत से students macro को .xlsx में save करते हैं और file दोबारा खोलने पर macro गायब होता है. Macro हो तो हमेशा .xlsm में save करो, और code में On Error GoTo रखकर user को समझ आने वाला message दो. Macro चलाने से पहले file का backup रख लो – macro के बाद Undo काम नहीं करता.
Practice task
Add the RefreshAndExport macro, assign it to a button on the Dashboard sheet, run it, and extend it so the PDF name also contains the text _AllCities.