Unable to use Userform when Worksheet is Protected

Krosis

New Member
Joined
Sep 10, 2021
Messages
23
Office Version
  1. 365
Platform
  1. Windows
Hi,

Im looking to protect a worksheet and only allow Data input via userform. When I Password Protect the worksheet I get a debug error when trying to submit. I have had a look online and it seems I need some code to allow the userform to work while the worksheet is protected but everything iv tried hasnt worked. Im new to all this so Im not sure which code would be effected or needs editting but my guess would be the submit button? I have linked it below but please let me know if you need anymore information. Thanks!

VBA Code:
Private Sub SubmitButton_Click()

Dim tbl As ListObject
Dim Ws As Worksheet
Dim lrow As Range
Dim lrow2 As Long

Set tbl = Sheets("Task_Check").ListObjects("Table1")

    If tbl.ListRows.Count > 0 Then

        Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
        For col = 1 To lrow.Columns.Count
            If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
                tbl.ListRows.Add
                Exit For
            End If
        Next
    End If

    lrow2 = tbl.ListRows.Count

     tbl.DataBodyRange(lrow2, 1).Value = DateBox.Value
     tbl.DataBodyRange(lrow2, 2).Value = CheckerBox.Value
     tbl.DataBodyRange(lrow2, 3).Value = CaseworkerBox.Value
     tbl.DataBodyRange(lrow2, 4).Value = TeamBox.Value
     tbl.DataBodyRange(lrow2, 5).Value = OutcomeBox.Value
     tbl.DataBodyRange(lrow2, 6).Value = TaskIDBox.Value
     If Len(FeedbackBox.Value) Then tbl.DataBodyRange(lrow2, 7).Formula = "=HYPERLINK(""" & FeedbackBox.Value & """,""Feedback"")"
     tbl.DataBodyRange(lrow2, 8).Value = CommentsBox.Value
     
Unload Me
 
End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi.
Please, try after adding the red line as below.

Set tbl = Sheets("Task_Check").ListObjects("Table1")
Sheets("Task_Check").Protect "YourPasswordGoesHere", UserInterFaceOnly:=True
If tbl.ListRows.Count > 0 Then
 
Upvote 0
Hi.
Please, try after adding the red line as below.

Set tbl = Sheets("Task_Check").ListObjects("Table1")
Sheets("Task_Check").Protect "YourPasswordGoesHere", UserInterFaceOnly:=True
If tbl.ListRows.Count > 0 Then

Hi Osvaldo,

Thank you for the suggestion. Tried the above and I am getting Run-time error: "1004": - Table feature aren't available because the sheet is protected. Might be a stupid question but am I meant to Protect the sheet via Review tab?

Iv highlighted the code im getting the error on below.

VBA Code:
Private Sub SubmitButton_Click()

Dim tbl As ListObject
Dim ws As Worksheet
Dim lrow As Range
Dim lrow2 As Long


Set tbl = Sheets("Task_Check").ListObjects("Table1")
Sheets("Task_Check").Protect "Apples", UserInterFaceOnly:=True

    If tbl.ListRows.Count > 0 Then

        Set lrow = tbl.ListRows(tbl.ListRows.Count).Range
        For col = 1 To lrow.Columns.Count
            If Trim(CStr(lrow.Cells(1, col).Value)) <> "" Then
                [B]tbl.ListRows.Add <------- Highlighted in Yellow[/B]
                Exit For
            End If
        Next
    End If

    lrow2 = tbl.ListRows.Count

     tbl.DataBodyRange(lrow2, 1).Value = DateBox.Value
     tbl.DataBodyRange(lrow2, 2).Value = CheckerBox.Value
     tbl.DataBodyRange(lrow2, 3).Value = CaseworkerBox.Value
     tbl.DataBodyRange(lrow2, 4).Value = TeamBox.Value
     tbl.DataBodyRange(lrow2, 5).Value = OutcomeBox.Value
     tbl.DataBodyRange(lrow2, 6).Value = TaskIDBox.Value
     If Len(FeedbackBox.Value) Then tbl.DataBodyRange(lrow2, 7).Formula = "=HYPERLINK(""" & FeedbackBox.Value & """,""Feedback"")"
     tbl.DataBodyRange(lrow2, 8).Value = CommentsBox.Value
Unload Me
 
End Sub
 
Upvote 0
Found a ref online which uses .Unprotect password and .Protect password which is all I need. Still open to suggestions in general as im looking to improve my knowledge for future projects. Thanks for the advice so far!
 
Upvote 0
Solution

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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