VBA to auto resize column width when altered

Binbs

New Member
Joined
Jan 7, 2022
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hello,

I found the following code that will resize a column to the desired width.

VBA Code:
Sub Set_Column_Range_Width()

Dim MySheet As Worksheet

Set MySheet = ActiveSheet


With MySheet.Columns("C")
 .ColumnWidth = 52.27
End With
End Sub

Is there a way to make this VBA code run when a column is resized? Protecting the sheet is not an option.

I have the following VBA code below already in the sheet so I am hoping to incorporate the solution within that code.

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

    Dim KeyCells As Range

    Set KeyCells = Range("H7:K7")

    If Not Application.Intersect(KeyCells, Target) Is Nothing Then

        Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh
    
      
End If
    
    
End Sub


Thank you!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
If you are asking for an 'auto-fit columns' macro ....

VBA Code:
Option Explicit

Sub autofitwidth()
Dim i As Integer
Cells.EntireColumn.AutoFit
    For i = 1 To ActiveSheet.UsedRange.Columns.Count
        Columns(i).ColumnWidth = Columns(i).ColumnWidth
    Next i

Worksheets("Sheet1").Range("A:A").EntireRow.AutoFit
End Sub
 
Upvote 0
If you are asking for an 'auto-fit columns' macro ....

VBA Code:
Option Explicit

Sub autofitwidth()
Dim i As Integer
Cells.EntireColumn.AutoFit
    For i = 1 To ActiveSheet.UsedRange.Columns.Count
        Columns(i).ColumnWidth = Columns(i).ColumnWidth
    Next i

Worksheets("Sheet1").Range("A:A").EntireRow.AutoFit
End Sub
Hi and thanks but I am not trying to autofit but rather fit to a specific width.

I want the code to resize the column to the desired width (ex. 52.27) whenever it's changed to anything other than that width. Basically preventing anyone from changing the width for that column to anything except 52.27. Protecting the sheet isn't an option.

Please let me know if that isn't clear.
 
Upvote 0
Ok ... does this work ?

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim MySheet As Worksheet
Dim KeyCells As Range

Set MySheet = ActiveSheet
Set KeyCells2 = Range("H7:K7")

        If Not Application.Intersect(KeyCells2, Target) Is Nothing Then

            Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh
      
    End If


    With MySheet.Columns("C")
         .ColumnWidth = 52.27
    End With

End Sub
 
Upvote 0
Ok ... does this work ?

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

Dim MySheet As Worksheet
Dim KeyCells As Range

Set MySheet = ActiveSheet
Set KeyCells2 = Range("H7:K7")

        If Not Application.Intersect(KeyCells2, Target) Is Nothing Then

            Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh
     
    End If


    With MySheet.Columns("C")
         .ColumnWidth = 52.27
    End With

End Sub
No, that doesn't seem to work to resize column C when it is changed.
 
Upvote 0
No, that doesn't seem to work to resize column C when it is changed.
And if you change

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

to

VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)

Does it work then?
 
Upvote 0
Solution
And if you change

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)

to

VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)

Does it work then?
This works to resize column C! Thanks so much to you both for the help!

But, it causes my other code to run slowly and not perform the way I need it to so I ended up doing this:

VBA Code:
Sub Worksheet_Change(ByVal Target As Range)


Dim KeyCells As Range


Set KeyCells2 = Range("H7:K7")

        If Not Application.Intersect(KeyCells2, Target) Is Nothing Then

            Worksheets("CST View").PivotTables("AutoOL").PivotCache.Refresh
      
    End If


    
End Sub


Sub Worksheet_SelectionChange(ByVal Target As Range)

Dim MySheet As Worksheet

Set MySheet = ActiveSheet


With MySheet.Columns("C")
 .ColumnWidth = 52.27
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,489
Messages
6,125,093
Members
449,205
Latest member
ralemanygarcia

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top