Macro fails to work if Worksheet Protected with Password

donggri84

New Member
Joined
Jul 31, 2013
Messages
18
Hello,

I've got this VBA code below (mr excel members wrote it for me and it works great) for a worksheet.
However It does not work if worksheet is protected with password.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("B23:HN23")) Is Nothing Then
If UCase(Target.Value) = "SAMPLE" Then
MsgBox ("Please fill the next column!")
End If
End If
On Error GoTo Err:
If Application.Intersect(Target, Range("B8")) = "New Zealand" Then
Rows("22:23").EntireRow.Hidden = True
End If

If Application.Intersect(Target, Range("B8")) = "Australia" Then
Rows("22:23").EntireRow.Hidden = False
End If
Err:

End Sub



Could anyone help me to have the code working with worksheet protection?

Thank you so much
Regards,
HJY
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hello,

I've got this VBA code below (mr excel members wrote it for me and it works great) for a worksheet.
However It does not work if worksheet is protected with password.

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("B23:HN23")) Is Nothing Then
If UCase(Target.Value) = "SAMPLE" Then
MsgBox ("Please fill the next column!")
End If
End If
On Error GoTo Err:
If Application.Intersect(Target, Range("B8")) = "New Zealand" Then
Rows("22:23").EntireRow.Hidden = True
End If

If Application.Intersect(Target, Range("B8")) = "Australia" Then
Rows("22:23").EntireRow.Hidden = False
End If
Err:

End Sub



Could anyone help me to have the code working with worksheet protection?

Thank you so much
Regards,
HJY
Hi donggri84,

You can add lines to the beginning and end of your code which will unprotect / reprotect the sheet. I suspect the finished code would look something like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
ActiveSheet.Unprotect "YOUR PASSWORD"
If Not Intersect(Target, Range("B23:HN23")) Is Nothing Then
If UCase(Target.Value) = "SAMPLE" Then
MsgBox ("Please fill the next column!")
End If
End If
On Error GoTo Err:
If Application.Intersect(Target, Range("B8")) = "New Zealand" Then
Rows("22:23").EntireRow.Hidden = True
End If

If Application.Intersect(Target, Range("B8")) = "Australia" Then
Rows("22:23").EntireRow.Hidden = False
End If
Err:
ActiveSheet.Protect "YOUR PASSWORD"
End Sub
 
Upvote 0
Hi donggri84,

You can add lines to the beginning and end of your code which will unprotect / reprotect the sheet. I suspect the finished code would look something like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
ActiveSheet.Unprotect "YOUR PASSWORD"
If Not Intersect(Target, Range("B23:HN23")) Is Nothing Then
If UCase(Target.Value) = "SAMPLE" Then
MsgBox ("Please fill the next column!")
End If
End If
On Error GoTo Err:
If Application.Intersect(Target, Range("B8")) = "New Zealand" Then
Rows("22:23").EntireRow.Hidden = True
End If

If Application.Intersect(Target, Range("B8")) = "Australia" Then
Rows("22:23").EntireRow.Hidden = False
End If
Err:
ActiveSheet.Protect "YOUR PASSWORD"
End Sub

PERFECT!
Thank you :D
 
Upvote 0
I'm glad this was what you were looking for as I have really only been teaching myself all this since mid February. These forums have definitely been a goldmine of information for me so I am more than happy to share what I have been learning.
 
Upvote 0

Forum statistics

Threads
1,223,956
Messages
6,175,616
Members
452,661
Latest member
Nonhle

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