Skip to content

Instantly share code, notes, and snippets.

@RichPollock
Last active February 1, 2019 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichPollock/5f15a1bbc76b65a907cb to your computer and use it in GitHub Desktop.
Save RichPollock/5f15a1bbc76b65a907cb to your computer and use it in GitHub Desktop.
Change Excel column widths by column number
' 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