Unprotect_Run code_Protect sheet Excel VBA

B_Siva

New Member
Joined
Feb 8, 2009
Messages
14
Office Version
  1. 2007
Platform
  1. Windows
Need Help...
I got runtime error 1004. Application defined or object defined error.
In excel sheet, Column H,I,J is locked and protected.
I want to capture the user name, date and week in column H,I,J when someone update the cell in column G.

below is the code. (It's work when sheet is not protected/remove the line ActiveSheet.Unprotect = "HullPU")



Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect = "HullPU"
If Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("G6:G3000")) Is Nothing Then 'change your range on this line"

If Target.Value = 30 Then
Target.Offset(0, 1).Value = UserName
Target.Offset(0, 2).Value = Date
Target.Offset(0, 3).Value = Mid(Year(Date), 3, 2) & "_" & ISOWeekNum(Date)
Else
Target.Offset(0, 2).Value = Target.Offset(0, 2).Value

End If
End If
ActiveSheet.Protect = "HullPU"
End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Need Help...
I got runtime error 1004. Application defined or object defined error.
In excel sheet, Column H,I,J is locked and protected.
I want to capture the user name, date and week in column H,I,J when someone update the cell in column G.

below is the code. (It's work when sheet is not protected/remove the line ActiveSheet.Unprotect = "HullPU")



Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Unprotect = "HullPU"
If Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("G6:G3000")) Is Nothing Then 'change your range on this line"

If Target.Value = 30 Then
Target.Offset(0, 1).Value = UserName
Target.Offset(0, 2).Value = Date
Target.Offset(0, 3).Value = Mid(Year(Date), 3, 2) & "_" & ISOWeekNum(Date)
Else
Target.Offset(0, 2).Value = Target.Offset(0, 2).Value

End If
End If
ActiveSheet.Protect = "HullPU"
End Sub


I think you need to write 'password'

e.g.

Code:
Sheets("sheetname").Select
    Sheets("sheetname").Unprotect Password:="[COLOR=#0000FF]HullPU[/COLOR]"
   
Your Code                                     

    Sheets("sheetname").Protect Password:="[COLOR=#0000FF]HullPU[/COLOR]"
 
Last edited:
Upvote 0
Just remove the = signs
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 ActiveSheet.Unprotect  "HullPU"
    If Target.Cells.Count > 1 Then Exit Sub

   If Not Intersect(Target, Range("G6:G3000")) Is Nothing Then  'change your range on this line"

        If Target.Value = 30 Then
            Target.Offset(0, 1).Value = UserName
            Target.Offset(0, 2).Value = Date
            Target.Offset(0, 3).Value = Mid(Year(Date), 3, 2) & "_" & ISOWeekNum(Date)
        Else
            Target.Offset(0, 2).Value = Target.Offset(0, 2).Value

    End If
    End If
    ActiveSheet.Protect  "HullPU"
End Sub
 
Upvote 0
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Intersect(Target, Range("G6:G3000")) Is Nothing Then Exit Sub
    ActiveSheet.Unprotect Password:="HullPU"
    If Target.Value = 30 Then
        Target.Offset(0, 1) = Application.UserName
        Target.Offset(0, 2) = Date
        Target.Offset(0, 3) = Mid(Year(Date), 3, 2) & "_" & WorksheetFunction.WeekNum(Date)
    Else
        Target.Offset(0, 2).Value = Target.Offset(0, 2).Value
    End If
    ActiveSheet.Protect Password:="HullPU"
End Sub
 
Upvote 0
Fluff,
Nope. It doesn't work... below code works when sheet is not protected..
It don't work when I insert code for unprotect and protect again.


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub


If Not Intersect(Target, Range("G6:G3000")) Is Nothing Then 'change your range on this line"


If Target.Value = 30 Then
Target.Offset(0, 1).Value UserName
Target.Offset(0, 2).Value Date
Target.Offset(0, 3).Value Mid(Year(Date), 3, 2) & "_" & ISOWeekNum(Date)
Else
Target.Offset(0, 2).Value = Target.Offset(0, 2).Value


End If
End If

End Sub
 
Upvote 0
I very much doubt that code works at all, there are too many things wrong with it.
Try the code that mumps supplied, if that does not work please show what line is giving the error.
 
Upvote 0
The code that works without sheet/column protected/locked
I have declare public function for ISOWeekNum, UserName before this subroute

Below code works well

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub


If Not Intersect(Target, Range("G6:G3000")) Is Nothing Then 'change your range on this line"


If Target.Value = 30 Then
Target.Offset(0, 1).Value = UserName
Target.Offset(0, 2).Value = Date
Target.Offset(0, 3).Value = Mid(Year(Date), 3, 2) & "_" & ISOWeekNum(Date)
Else
Target.Offset(0, 2).Value = Target.Offset(0, 2).Value


End If
End If

End Sub

result in excel sheet
[TABLE="width: 0"]
<tbody>[TR]
[TD]Cutting Status
[/TD]
[TD]Updated By
[/TD]
[TD]Cutting Plan Actual Cut Date
[/TD]
[TD]Cutting Plan Cut Week
[/TD]
[/TR]
[TR]
[TD]30
[/TD]
[TD]30000270
[/TD]
[TD]12-Aug-19
[/TD]
[TD]19_33
[/TD]
[/TR]
</tbody>[/TABLE]


 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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