Autofit Column VBA not working.

Strider89

New Member
Joined
Dec 28, 2017
Messages
22
I have a code that checks to see if there is a change in Column C, and then autofits columns C:U, then hides a specific column. However, the code is not doing anything now. No errors occur, nothing happens to the sheet, and if there too many characters in a cell, the ### is all I see, which is what this code is designed to stop. What have I done wrong?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)

Application.ScreenUpdating = False
                
If Target.Column = 3 Then
    ActiveSheet.Unprotect
    Columns("C:U").AutoFit
    Columns("T").Hidden = True
    ActiveSheet.Protect


Else
    Exit Sub
End If
    
Application.ScreenUpdating = True
    
End Sub
 
Last edited:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
That code doesn't check if a change is made in column C, it checks if column C has been selected.

Try using the Change event instead.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Application.ScreenUpdating = False
                
    If Target.Column = 3 Then
        Me.Unprotect
        Columns("C:U").AutoFit
        Columns("T").Hidden = True
        Me.Protect
    End If
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
2 possibilities come to mind. Your code leaves Screenupdating as false if it wasn't column 3 that changed.
Also, you MAY have events turned off
 
Upvote 0
In the Worksheet itself. As in, right-click, view code, and select "Worksheet" from the drop-down menu on the top left. Sorry if that was redundant, I'm still learning all the lingo and wanted to be sure I got the location as correct as possible with my limited understanding.
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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