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 techguruplus telegram group Join Our WhatsApp Group techguruplus whatsapp group
Nazim Khan - Author Image

Nazim Khan (Author) 📞 +91 9536250020
[MBA in Finance]

Nazim Khan is an expert in Microsoft Excel. He teaches people how to use it better. He has been doing this for more than ten years. He is running this website (TechGuruPlus.com) and a YouTube channel called "Business Excel" since 2016. He shares useful tips from his own experiences to help others improve their Excel skills and careers.

Leave a Comment