Skip to content

Instantly share code, notes, and snippets.

@airstrike
Created December 30, 2014 04:31
Show Gist options
  • Save airstrike/1848dbb81ac5eab5cd32 to your computer and use it in GitHub Desktop.
Save airstrike/1848dbb81ac5eab5cd32 to your computer and use it in GitHub Desktop.
Excel: Autofit current column and make selected columns the same size as the largest of them
Option Explicit
Sub AutoFit()
Dim Sht As Worksheet
Set Sht = ActiveSheet
With Selection
.EntireColumn.AutoFit
End With
If Selection.Columns.Count > 1 Then
With Application
Dim SU As Boolean
SU = .ScreenUpdating
.ScreenUpdating = False
Dim x As Long, max As Long
For x = 1 To Selection.Columns.Count
If Selection.Columns(x).ColumnWidth > max Then _
max = Selection.Columns(x).ColumnWidth
Next
Dim r As Range
For x = 1 To Selection.Columns.Count
Set r = Selection.Columns(x)
r.ColumnWidth = max
Next
Application.ScreenUpdating = SU
End With
End If
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment