Stops working when protected "Runtime error '1004'"

VectorW2

New Member
Joined
Sep 11, 2013
Messages
36
Office Version
  1. 365
  2. 2010
This sub works great; however, as soon as you protect the code; e.g., (View Code), (Tools), (VBAProject - Project Properties), (Proctection) and click the (Lock project for viewing) box and enter a password, the code stops working and I get the Runtime error '1004'. I would like to protect this code. Thanks.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$J$1" Then
Select Case Target.Value
Case "Clear":
Range("A2:H2").Select
Range(Selection, Selection.End(xlDown)).Select
Range("A2:H2001").Select
Selection.ClearContents
Range("J3").Select
ActiveCell.FormulaR1C1 = "=IF(R2C1<>"""",""¡ Click Here !"","""")"
Range("L1").Select
ActiveCell.FormulaR1C1 = "=R[2]C[-2]&"" ""&R[1]C[-10]&""-""&R[1]C[-11]"
Range("J1").Activate
Selection.ClearContents
Range("A2").Select
Case "Ready for Upload":
Range("A2").Select
Sheets("Uploadable").Select
Application.Goto Reference:="Paste"
Selection.ClearContents
Sheets("Conversion").Visible = True
Sheets("Conversion").Select
Application.Goto Reference:="Copy"
Selection.Copy
Sheets("Uploadable").Select
Application.Goto Reference:="Paste"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Conversion").Select
Application.CutCopyMode = False
Sheets("Conversion").Visible = xlVeryHidden
Sheets("Paste Special Data (A2)").Select
Range("A2").Select
Case "Save":
Sheets("Uploadable").Select
ChDir "G:\File Name\6I - Upload Files (2022)"
ActiveWorkbook.SaveAs Filename:= _
"G:\File Name\6I - Upload Files (2022)\" & Range("L1") & ".csv", _
FileFormat:=xlCSVUTF8, CreateBackup:=False
End Select
End If

End Sub
 

Attachments

  • Stops working when protected.jpg
    Stops working when protected.jpg
    227.6 KB · Views: 7

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try turning off the events:

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Address = "$J$1" Then
    
    Application.EnableEvents = False
    
    Select Case Target.Value
      Case "Clear":
        'Range("A2:H2").Select
        'Range(Selection, Selection.End(xlDown)).Select
        Range("A2:H2001").ClearContents
        Range("J3").FormulaR1C1 = "=IF(R2C1<>"""",""¡ Click Here !"","""")"
        Range("L1").FormulaR1C1 = "=R[2]C[-2]&"" ""&R[1]C[-10]&""-""&R[1]C[-11]"
        Range("J1").ClearContents
        Range("A2").Select
      Case "Ready for Upload":
        Range("A2").Select
        Sheets("Uploadable").Select
        Application.Goto Reference:="Paste"
        Selection.ClearContents
        Sheets("Conversion").Visible = True
        Sheets("Conversion").Select
        Application.Goto Reference:="Copy"
        Selection.Copy
        Sheets("Uploadable").Select
        Application.Goto Reference:="Paste"
        Selection.PasteSpecial Paste:=xlPasteValues
        Sheets("Conversion").Select
        Application.CutCopyMode = False
        Sheets("Conversion").Visible = xlVeryHidden
        Sheets("Paste Special Data (A2)").Select
        Range("A2").Select
      Case "Save":
        Sheets("Uploadable").Select
        ChDir "G:\File Name\6I - Upload Files (2022)"
        ActiveWorkbook.SaveAs Filename:= _
        "G:\File Name\6I - Upload Files (2022)\" & Range("L1") & ".csv", _
        FileFormat:=xlCSVUTF8, CreateBackup:=False
    End Select
  
    Application.EnableEvents = True
  
  End If
End Sub
 
Upvote 0
When I do that non of the macro works. If unhidden, the whole macro works. If hidden (need a code to view macro), then the first few parts of the macro work, just not the save. The saving triggers the run time error.
 
Upvote 0
(View Code), (Tools), (VBAProject - Project Properties), (Proctection) and click the (Lock project for viewing) box
I already did that and the macro works.

"G:\File Name\6I - Upload Files (2022)\"
The name of the folder is correct?

& Range("L1") &
Does cell L1 in "Uploadable" sheet contain something?

ActiveWorkbook.SaveAs Filename:= _
"G:\File Name\6I - Upload Files (2022)\" & Range("L1") & ".csv", _
FileFormat:=xlCSVUTF8, CreateBackup:=False
Change this: xlCSVUTF8 for this: xlCSV


Try again.
 
Upvote 0

Forum statistics

Threads
1,213,543
Messages
6,114,243
Members
448,555
Latest member
RobertJones1986

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