AutoFit meth of Range class failed 'Run-time error '1004

Livin404

Well-known Member
Joined
Jan 7, 2019
Messages
743
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Greetings, I have a fairly complex worksheet that automatically corrects errors with the activation of a button. All seems to be well regarding working the buttons. It is because I have to protect the sheet, it is the AutoFit macro which is causing the problems. Do I need to have "Call Sheet Protection" for every macro? Any help with this would be great for it's driving me mad.
Thank you for much indeed.

VBA Code to protect the worksheet (This has it's own Module:

VBA Code:
Sub SheetProtection()
    If ThisWorkbook.Sheets("Xman").ProtectContents = True Then
      ThisWorkbook.Sheets("Xman").Unprotect "mozzer"
    Else
      ThisWorkbook.Sheets("Xman").Protect "mozzer"
    End If
End Sub

VBA Code for Auto Fit- (This is not in a module but rather in Sheet1 (Xman):

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Call SheetProtection
Columns.AutoFit
Call SheetProtection
End Sub


auto.JPG
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Do I need to have "Call Sheet Protection" for every macro?
The protection needs to be removed for it to be sorted, so if you have removed and re-applied protection then you will need to remove it again.
There is an alternative method that you can use, ideally with a workbook_open event that changes the protection settings so that your code will have access but users will not, see last example here Excel VBA: Macro Code To Run Macros On Protected Worksheets & Sheets
 
Upvote 0
Solution
I found a better way of dealing with the macros then inserting Call between each macro.
I used the following VBA much easier and it seems to work. Regarding the autofit I had to check the "allow format Columns/Rows" when locking it.

VBA Code:
Private Sub Workbook_Open()
    Sheets(1).Protect Password:="mozzer", _
    UserInterFaceOnly:=True

    End Sub
 
Upvote 0
You're right, I went and changed the status. It is exactly the link I was looking at as well after I searched on my own. Thank you,
 
Upvote 0

Forum statistics

Threads
1,213,553
Messages
6,114,279
Members
448,562
Latest member
Flashbond

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