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

Khup students macro .xlsx madhe save kartat aani file punha ughadlyavar macro gayab hoto. Macro asel tar nehmi .xlsm madhe save kara, aani code madhe On Error GoTo thevun user la samjel asa message dya. Macro chalvnyapurvi ekda file cha backup theva – Undo macro nantar kaam karat nahi.

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.