Ravindra BagaleCourses & study guides

15. Final Project: Blinkit Maharashtra Monthly Report

15.6 Stage 5 – A Small Macro

Steps in Excel

  1. Save as Excel Macro-Enabled Workbook (*.xlsm).
  2. Developer › Code › Visual Basic (Alt + F11) › Insert › Module › paste the macro below.
  3. Insert › Illustrations › Shapes › draw a button "Refresh & Export PDF" › right-click › Assign Macro… › RefreshAndExport.
  4. 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.

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.