VBA Code to Print Narrow Margin in Excel

VBA code that you can use to set narrow margins and print a worksheet in Excel

VBA Code:

Sub PrintNarrowMargin()
    Dim ws As Worksheet
    
    ' Set the worksheet you want to print with narrow margins
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' Change "Sheet1" to the desired worksheet name
    
    ' Set the narrow margin size in inches (adjust as needed)
    Dim marginSize As Double
    marginSize = 0.5
    
    ' Set the page setup properties for narrow margins
    With ws.PageSetup
        .LeftMargin = Application.InchesToPoints(marginSize)
        .RightMargin = Application.InchesToPoints(marginSize)
        .TopMargin = Application.InchesToPoints(marginSize)
        .BottomMargin = Application.InchesToPoints(marginSize)
    End With
    
    ' Print the worksheet
    ws.PrintOut
    
    ' Reset the page setup properties to default
    With ws.PageSetup
        .LeftMargin = Application.InchesToPoints(0.75)
        .RightMargin = Application.InchesToPoints(0.75)
        .TopMargin = Application.InchesToPoints(1)
        .BottomMargin = Application.InchesToPoints(1)
    End With
    
    ' Inform the user that the worksheet has been printed with narrow margins
    MsgBox "The worksheet has been printed with narrow margins!"
End Sub


Check All VBA Codes

Join Our Telegram Group

Join the TechGuruPlus Telegram group

Join Our WhatsApp Group

Join the TechGuruPlus WhatsApp group
Nazim Khan, founder of TechGuruPlus.com

— Founder & Editor, TechGuruPlus.com
[MBA in Finance]

Nazim Khan has spent over ten years working with Excel, Tally and office documentation in accounts, finance and MIS roles. He has been running TechGuruPlus.com since 2016, where he publishes free editable templates for office work, and the YouTube channel Business Excel. He has trained more than 50,000 people online. Every template on this site is built and checked by him before it is published.

Contact  Â·  Excel Course

Leave a Comment