andrewlau2881
New Member
- Joined
- Oct 20, 2023
- Messages
- 15
- Office Version
- 365
- Platform
- Windows
I have code that opens all the workbooks in a folder and hides columns based on whether the first cell in the column is blank, or not. It works perfectly as intended, unless the Workbook has protected sheets. I have password protected the sheets as they will require other users to enter data and I don't want them messing with the formatting. I know there is some code that will unprotect the sheet, and then protect it again before moving on to the next one. I just have no idea where to add the code.
As always, thank you for any help you can give me.
As always, thank you for any help you can give me.
VBA Code:
Sub m()
Const path As String = "folder address" '<<<Change<<<'
Dim f As String
Dim p As Long, i As Long
Application.ScreenUpdating = False
f = Dir(path & "*.xlsm*")
Do While Len(f) > 0
With Workbooks.Open(path & f)
For i = 4 To IIf(.Sheets.Count > 105, 100, .Sheets.Count)
With .Sheets(i)
For p = 2 To 31
.Columns(p).Hidden = .Cells(1, p).Value = ""
Next p
End With
Next i
.Close True
End With
f = Dir
Loop
Application.ScreenUpdating = True
End Sub