Auto fit columns and rows while in protected mode

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I trying to have my columns and rows adjust automatically while the sheet is protected. There is no password. I got it to work no problem while the worksheet is unprotected. I added Sheets ("Xman") at the begging and end of VBA but it made no difference. I'm hoping it is just a minor correction.

Thank you,
VBA Code:
Option Explicit
 
Sub AutoFitAll()
     
    Sheets("XMan").Unprotect
    Application.ScreenUpdating = False
    Dim wkSt As String
    Dim wkBk As Worksheet
    wkSt = ActiveSheet.Name
    For Each wkBk In ActiveWorkbook.Worksheets
        On Error Resume Next
        wkBk.Activate
        Cells.EntireColumn.AutoFit
        Cells.EntireRow.AutoFit
    Next wkBk
    Sheets(wkSt).Select
    Application.ScreenUpdating = True
    Sheets("XMan").Protect
End Sub
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
VBA Code:
Application.ScreenUpdating = True
    Sheets("XMan").Protect, AllowFormattingColumns:=True, AllowFormattingRows:=True
 
Upvote 0
I tried different variations of this from removing what I had and replacing it what you provided, and I replaced a portion of what I had. Am I missing something, because unfortunately nothing changed.?
Thank you,
 
Upvote 0
Try
VBA Code:
Sub AutoFitAll()
    Application.ScreenUpdating = False
    Dim wkSt As String, wkBk As Worksheet
    wkSt = ActiveSheet.Name
    For Each wkBk In Worksheets
        wkBk.Activate
        wkBk.Unprotect
        Cells.EntireColumn.AutoFit
        Cells.EntireRow.AutoFit
        wkBk.Protect
    Next wkBk
    Sheets(wkSt).Select
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
I tried yours, and for now it wants to autofill on other worksheets, I only need this specifically on one worksheet. Though this VBA is placed in the worksheet and not in a module I don't think that should affect the coding, Yet I can't be sure as I'm still new to this.
 
Upvote 0
Hello,
I found something much simpler and I still locked the worksheet I just checked the format rows and columns. The users still cannot mess with the settings.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Columns.AutoFit
Rows.AutoFit
End Sub
 
Upvote 0
Hello,
I found something much simpler and I still locked the worksheet I just checked the format rows and columns. The users still cannot mess with the settings.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Columns.AutoFit
Rows.AutoFit
End Sub
 
Upvote 0
Well, your initial post suggested ALL sheets in the workbook !!
VBA Code:
For Each wkBk In Worksheets
Hence my solution
 
Upvote 0
Well, your initial post suggested ALL sheets in the workbook !!
VBA Code:
For Each wkBk In Worksheets
Hence my solution
You’re right, this is a slow process for me in this learning phase. Thanks all the same, it did me look a little harder. Thank you
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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