Last active
February 1, 2019 12:38
-
-
Save RichPollock/5f15a1bbc76b65a907cb to your computer and use it in GitHub Desktop.
Change Excel column widths by column number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' For example: Call SetColumnWidths("Sheet1", Array(2, 4, 6, 8), 0) would collapse columns B, D, F and H on Sheet1 | |
Sub SetColumnWidths(sheet As String, columnNumbers As Variant, width As Integer) | |
For i = 0 To UBound(columnNumbers) | |
Worksheets(sheet).Cells(1, columnNumbers(i)).EntireColumn.ColumnWidth = width | |
Next i | |
End Sub | |
' Or to hide a contiguous range: | |
Sub HideColumns(sheet As String, leftmostCol As Integer, rightmostCol As Integer) | |
For i = leftmostCol To rightmostCol | |
Sheets(sheet).Cells(1, i).EntireColumn.Hidden = True | |
Next i | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment