• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help Lock/Unlock Worksheet with other Worksheet as input

Dear all

I want to lock the worksheet (database) me so no one can edit the worksheet unless know the password.
I have tried to lock worksheet (database) I of the ribbon menu in Excel VBA but it makes me not working

for you know, I use another worksheet (receipts) as input to the worksheet (Database)

Also attached file that I mean

please help me to solve this problem. My confusion to finish it because I was a new and never before learning VBA


Thank you

Afan
 

Attachments

  • tesing_7.xlsm
    46.9 KB · Views: 3
Something like below. Change "Your Password" to actual password that you set.
Code:
Private Sub CommandButton1_Click()
'Pindah
Dim WSK As Worksheet
Dim WDB As Worksheet

Set WSK = Worksheets("Kuitansi")
Set WDB = Worksheets("Database")

If WorksheetFunction.CountIf(WDB.Columns(1), [L1].Value) = 1 Then
    MsgBox "Receipst Number already exist,Please again"
    Exit Sub
    Else
        WDB.Unprotect "Your Password"
        nextrow = WDB.Cells(Rows.Count, 1).End(xlUp).Row + 1
        WDB.Cells(nextrow, 1).Resize(1, 26).Value = Array([L1], [G7], [K7], [G8], [G9], [G10], [G11], _
        [H11], [D35], [J15], [J16], [J17], [J18], [J19], [J20], [J21], [J22], [J23], [J24], [J25], [J26], [J27], [J28], [J29], [E27], [E28])
'save
        WDB.Protect "Your Password"
        ActiveWorkbook.Save

'print preview
        Sheet1.PrintPreview
    End If

   
End Sub
 
Thanks Chihiro..

i try input like you said

Private Sub CommandButton1_Click()
'Pindah
Dim WSK As Worksheet
Dim WDB As Worksheet

Set WSK = Worksheets("Kuitansi")
Set WDB = Worksheets("Database")

If WorksheetFunction.CountIf(WDB.Columns(1), [L1].Value) = 1 Then
MsgBox "Receipst Number already exist,Please again"
Exit Sub
Else
WDB.Unprotect ([123])
nextrow = WDB.Cells(Rows.Count, 1).End(xlUp).Row + 1
WDB.Cells(nextrow, 1).Resize(1, 26).Value = Array([L1], [G7], [K7], [G8], [G9], [G10], [G11], _
[H11], [D35], [J15], [J16], [J17], [J18], [J19], [J20], [J21], [J22], [J23], [J24], [J25], [J26], [J27], [J28], [J29], [E27], [E28])
'save
WDB.Protect ([123])
ActiveWorkbook.Save

'print preview
Sheet1.PrintPreview
End If


End Sub

but
it's not working
there is show a warning box
"run time error '1004'
"The password not supplied is not correct. verify that the caps lock key is off and be sure to use the correction capitalization"

is there any mistake i did?
 
Thank you... Guys...

it's working.....
aoooww... i'm happy for that...


the problem is simple.. i forgot the password....so it'x make the vba erorr said "Password not match"

Lucky me, i still have another master file So i can create new vba with code you gave...

Thank You again... it's Helpfull.
 
Back
Top