Getting a 1004 error on an unprotected sheet

melodramatic

Board Regular
Joined
Apr 28, 2003
Messages
180
Office Version
  1. 365
Platform
  1. Windows
I'm running a macro to simply put data into a table quickly, and am getting a Run-time error '1004': stating I've got a protected sheet. No, I dont'!

While the sheet is protected, I clearly run the code to unprotect it before the input, then re-protect it after. And that code is working. When the macro bombs ou

Capture.JPG
Error 1004.JPG


My coding from the beginning is as follows:

VBA Code:
Private Sub CommandButton1_Click()
'This macro adds to the bank
Dim pwd As String
Dim nextrow As Long
Dim playername As String
Dim money As Long

pwd = Range("K2")
nextrow = Range("L2")
playername = Range("B2")
money = Range("B3")

Worksheets("Activity").Visible = True
Worksheets("Activity").Unprotect Password:=pwd
Worksheets("Activity").Select
MsgBox nextrow
Range("A" & nextrow).Value = playername
Range("B" & nextrow).Value = Date
Range("C" & nextrow).Value = money
Range("D" & nextrow).Value = "Add"
Range("F" & nextrow).Value = Environ("username")
ActiveSheet.Protect Password:=pwd
Worksheets("Balances").Select
Worksheets("Activity").Visible = False

I first tried running the unprotect code as the following (but that didn't work, either):

VBA Code:
Worksheets("Activity").Visible = True
Worksheets("Activity").Select
ActiveSheet.Unprotect Password:=Pwd
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Where is that code located?
 
Upvote 0
The code you posted in your op.
 
Upvote 0
OK, the larger strand of code is within a command button on the master page (Input).

The smaller strand of code is no longer there - it's showing what I had originally used (unprotecting the ActiveSheet as opposed to Worksheets("Activity").
 
Upvote 0
Ok the problem is that when code is in a sheet module, unqualified ranges will refer to the sheet that contains the code, not the active sheet.
Try it like
VBA Code:
Private Sub CommandButton1_Click()
'This macro adds to the bank
Dim pwd As String
Dim nextrow As Long
Dim playername As String
Dim money As Long

pwd = Range("K2")
nextrow = Range("L2")
playername = Range("B2")
money = Range("B3")

With Worksheets("Activity")
   .Visible = True
   .Unprotect Password:=pwd
   MsgBox nextrow
   .Range("A" & nextrow).Value = playername
   .Range("B" & nextrow).Value = Date
   .Range("C" & nextrow).Value = money
   .Range("D" & nextrow).Value = "Add"
   .Range("F" & nextrow).Value = Environ("username")
   .Protect Password:=pwd
End With
Worksheets("Balances").Select
Worksheets("Activity").Visible = False
 
Upvote 0
Hi,
You don't need to select sheets - Try qualifying your ranges to your worksheets to ensure that your variables have been correctly intialized

VBA Code:
Private Sub CommandButton1_Click()
'This macro adds to the bank
    Dim pwd As String, playername As String
    Dim nextrow As Long, money As Long
    Dim wsBalances As Worksheet, wsActivity As Worksheet
   
    With ThisWorkbook
        Set wsBalances = .Worksheets("Balances")
        Set wsActivity = .Worksheets("Activity")
    End With

    With wsBalances
        pwd = .Range("K2").Value
        nextrow = .Range("L2").Value
        playername = .Range("B2").Value
        money = .Range("B3").Value
    End With

With wsActivity
    .Visible = False
    .Unprotect Password:=pwd
    .Range("A" & nextrow).Value = playername
    .Range("B" & nextrow).Value = Date
    .Range("C" & nextrow).Value = money
    .Range("D" & nextrow).Value = "Add"
    .Range("F" & nextrow).Value = Environ("username")
    .Protect Password:=pwd
End With
End Sub

I am assuming that you have a method to increment Nextrow variable?

Hope Helpful

Dave
 
Upvote 0
That did it perfectly. I need to learn that method of handling entry on sheets.

Thank you so much for your help, Fluff!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Oh, and I just saw your question about incrementing the nextrow variable. In this case, it's not needed. I only do one entry at a time, and nextrow only goes to figure out the next row # to add to the entry table.

Thanks, though!
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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