Unprotect/protect sheet and still hide the formula while running VBA

MMM_84

New Member
Joined
Jan 13, 2021
Messages
28
Office Version
  1. 365
Platform
  1. Windows
Hi folks,

I would appreciate if you help in following.
I have a password protected sheet and the vba that uprotects/protects and runs the code. However while unprotecting it shows the formula bar, which I would like to keep hidden. Any suggestions, please?

Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Worksheets("Sheet1").Unprotect Password:="password"
'Unprotect

If Range("B20").Value = "Yes" Then
Rows("26:40").EntireRow.Hidden = False
End If

If Range("B20").Value = "No" Then

Rows("26:40").EntireRow.Hidden = True

ElseIf Range("B20").Value = 0 Then
Rows("26:40").EntireRow.Hidden = True


ThisWorkbook.Worksheets("Sheet1").Protect Password:="password"
'Protect
End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I've not tested this, but it should work:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Application.ScreenUpdating =False
ThisWorkbook.Worksheets("Sheet1").Unprotect Password:="password"
'Unprotect

  If Range("B20").Value = "Yes" Then
      Rows("26:40").EntireRow.Hidden = False
 End If

 If Range("B20").Value = "No" Then
       
      Rows("26:40").EntireRow.Hidden = True

 ElseIf Range("B20").Value = 0 Then
       Rows("26:40").EntireRow.Hidden = True
       

           ThisWorkbook.Worksheets("Sheet1").Protect Password:="password"
'Protect
       End If
Application.ScreenUpdating =True
   End Sub
 
Upvote 0
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Me.Unprotect Password:="password"
   'Unprotect
   
   If Range("B20").Value = "Yes" Then
      Rows("26:40").EntireRow.Hidden = False
   ElseIf Range("B20").Value = "No" Then
      Rows("26:40").EntireRow.Hidden = True
   ElseIf Range("B20").Value = 0 Then
      Rows("26:40").EntireRow.Hidden = True
   End If
   
   Me.Protect Password:="password"
   'Protect
End Sub
You're code only protects the sheet if B20=0
 
Upvote 0
Е
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Me.Unprotect Password:="password"
   'Unprotect
  
   If Range("B20").Value = "Yes" Then
      Rows("26:40").EntireRow.Hidden = False
   ElseIf Range("B20").Value = "No" Then
      Rows("26:40").EntireRow.Hidden = True
   ElseIf Range("B20").Value = 0 Then
      Rows("26:40").EntireRow.Hidden = True
   End If
  
   Me.Protect Password:="password"
   'Protect
End Sub
You're code only protects the sheet if B20=0
Thanks a ton! Works perfectly :)
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

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