0% found this document useful (0 votes)
75 views4 pages

To Fit All Column

This document provides code samples for formatting Excel worksheets and getting screen resolution information. It includes macros for: 1. Autofitting all columns on each worksheet in a workbook. 2. Setting page margins, headers/footers, and other print settings using a recorded macro. 3. Getting the screen resolution using API calls or VBA functions. 4. Setting row heights and column widths in millimeters using VBA macros. 5. An example macro that sets row height for row 3 and column width for column C to 3.5 cm.

Uploaded by

Jay Kay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views4 pages

To Fit All Column

This document provides code samples for formatting Excel worksheets and getting screen resolution information. It includes macros for: 1. Autofitting all columns on each worksheet in a workbook. 2. Setting page margins, headers/footers, and other print settings using a recorded macro. 3. Getting the screen resolution using API calls or VBA functions. 4. Setting row heights and column widths in millimeters using VBA macros. 5. An example macro that sets row height for row 3 and column width for column C to 3.5 cm.

Uploaded by

Jay Kay
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

TO FIT ALL COLUMN

For Each ws In Worksheets ws.Columns.Autofit Next ws

ANOTHER SOLUTION - RECORDING MACRO

Sub Macro5() With ActiveSheet.PageSetup .LeftHeader = "" .CenterHeader = "" .RightHeader = "" .LeftFooter = "" .CenterFooter = "" .RightFooter = "" .LeftMargin = Application.InchesToPoints(0) .RightMargin = Application.InchesToPoints(0) .TopMargin = Application.InchesToPoints(1) .BottomMargin = Application.InchesToPoints(1) .HeaderMargin = Application.InchesToPoints(0.5) .FooterMargin = Application.InchesToPoints(0.5) .PrintHeadings = False .PrintGridlines = False .PrintComments = xlPrintNoComments .PrintQuality = 600 .CenterHorizontally = False .CenterVertically = False .Orientation = xlLandscape .Draft = False .PaperSize = xlPaperLetter .FirstPageNumber = xlAutomatic .Order = xlDownThenOver .BlackAndWhite = False .Zoom = False .FitToPagesWide = 1 .FitToPagesTall = 3 .PrintErrors = xlPrintErrorsDisplayed End With End Sub

TO GET SCREEN RESOLUTION

'Add a module to your project (In the menu choose Project -> Add Module, Then click Open) 'Insert this code to the module : Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long Public Const SM_CXSCREEN = 0 Public Const SM_CYSCREEN = 1 'Insert this code to your form: Private Sub Form_Load() Dim Tmp As String Tmp = GetSystemMetrics(SM_CXSCREEN) & _ "x" & GetSystemMetrics(SM_CYSCREEN) MsgBox (Tmp) End Sub

ANOTHER CODE FOR GET SCREEN RESOLUTION

Public Function ScreenResolution(f As Form) As String Dim iWidth As Integer, iHeight As Integer iWidth = Screen.Width \ Screen.TwipsPerPixelX iHeight = Screen.Height \ Screen.TwipsPerPixelY ScreenRes = iWidth & " X " & iHeight End Function

TO SET ROW HEIGHT AND COLUMN WIDTH IN MILLIMETERS The macros below lets you set row heights and column widths using millimeters as a scale: Sub SetColumnWidthMM(ColNo As Long, mmWidth As Integer) ' changes the column width to mmWidth Dim w As Single If ColNo < 1 Or ColNo > 255 Then Exit Sub Application.ScreenUpdating = False w = Application.CentimetersToPoints(mmWidth / 10) While Columns(ColNo + 1).Left - Columns(ColNo).Left - 0.1 > Columns(ColNo).ColumnWidth = Columns(ColNo).ColumnWidth Wend While Columns(ColNo + 1).Left - Columns(ColNo).Left + 0.1 < Columns(ColNo).ColumnWidth = Columns(ColNo).ColumnWidth Wend End Sub Sub SetRowHeightMM(RowNo As ' changes the row height to If RowNo < 1 Or RowNo > Rows(RowNo).RowHeight = 10) End Sub

w - 0.1 w + 0.1

Long, mmHeight As Integer) mmHeight 65536 Then Exit Sub Application.CentimetersToPoints(mmHeight /

This example macro shows how you can set the row height for row 3 and the column width for column C to 3.5 cm: Sub ChangeWidthAndHeight() SetColumnWidthMM 3, 35 SetRowHeightMM 3, 35 End Sub

You might also like