JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,691
- Office Version
- 365
- Platform
- Windows
Here's macro that I think will allow me to restore the widths of the columns in a sheet after they get screwed up when I move or insert columns.
It requires a row of cells in the columns in question. It has two "paths". (1) It will store the current widths in the selected cells. (2) It will restore the widths of the columns to the values in the selected cells.
Before I do something that I fear might screw up some column widths, I select a row of cells in those columns and call the macro to fill in the current widths. If any columns get screwed up, I select those same cells and call the macro to restore the columns to the width in the selected cells. It works great. I have not found an cases that fail.
I would appreciate any comments or suggestions.
The one thing I do not like about it is the MsgBox. I wish MsgBox would allow custom buttons. I am trying to decide whether to just live with it, replace it with a userform, or switch to two macros called by two button controls. I'd also appreciate any comments or suggestions on that.
It requires a row of cells in the columns in question. It has two "paths". (1) It will store the current widths in the selected cells. (2) It will restore the widths of the columns to the values in the selected cells.
Before I do something that I fear might screw up some column widths, I select a row of cells in those columns and call the macro to fill in the current widths. If any columns get screwed up, I select those same cells and call the macro to restore the columns to the width in the selected cells. It works great. I have not found an cases that fail.
I would appreciate any comments or suggestions.
The one thing I do not like about it is the MsgBox. I wish MsgBox would allow custom buttons. I am trying to decide whether to just live with it, replace it with a userform, or switch to two macros called by two button controls. I'd also appreciate any comments or suggestions on that.
VBA Code:
Sub ManageColumnWidths()
Const MyName As String = "ManageColumnWidths"
Const MaxColumns As Long = 100
Dim actionChoice As VbMsgBoxResult
Dim cell As Range
Dim colWidth As Double
Dim MsgText As String
' Check that the selection is a single row
If Selection.Rows.Count > 1 Or Selection.Areas.Count > 1 Then
MsgBox "Please select a single row (not multiple rows or non-contiguous cells).", vbExclamation, MyName
Exit Sub
End If
' Check that the selection length is reasonable (not the entire row)
If Selection.Columns.Count > MaxColumns Then
MsgText = "Selection is > " & MaxColumns & " cells. Do you want to proceed anyway?"
If MsgBox(MsgText, vbYesNo + vbExclamation, MyName) = vbNo Then
Exit Sub
End If
End If
'Ask the user what action to take
MsgText = "Select the action to perform:" & vbCrLf _
& "Yes = Store column widths in the selected cells" & vbCrLf _
& "No = Restore column widths from the selected cells" & vbCrLf _
& "Cancel = Exit."
actionChoice = MsgBox(MsgText, vbYesNoCancel + vbQuestion, MyName)
Select Case actionChoice
Case vbYes 'Store column widths in the selected cells
For Each cell In Selection 'Loop thru each cell in the selected range
colWidth = cell.ColumnWidth 'Get the width in character units
'Debug.Print "Storing Column " & cell.Column & " Width (Pixels): " & colWidth
cell.Value = colWidth 'Store the width in the cell
Next cell
Case vbNo 'Restore the column widths from the selected cells
For Each cell In Selection 'Loop thru each cell in the selected range
colWidth = cell.Value 'Get the width value from the cell
'Debug.Print "Restoring Column " & cell.Column & " Width: " & colWidth
cell.ColumnWidth = colWidth 'Set the width of the corresponding column
Next cell
Case Else 'Exit
End Select
End Sub