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