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

MMM_84

New Member
Joined
Jan 13, 2021
Messages
3
Office Version
  1. 2016
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
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

jmacleary

Well-known Member
Joined
Oct 5, 2015
Messages
1,056
Office Version
  1. 365
  2. 2007
Platform
  1. Windows
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,253
Office Version
  1. 365
Platform
  1. Windows
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
 

MMM_84

New Member
Joined
Jan 13, 2021
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Е
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 :)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,253
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,001
Messages
5,622,125
Members
415,878
Latest member
jjj12345

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
Top