Using VBA code in a protected excel sheet

MagicCarpet

New Member
Joined
Aug 1, 2024
Messages
3
Office Version
  1. 2010
Platform
  1. Windows
I have below code and need to use it in an password protected sheet. I tried but not working.
""
________________________________________________________________________________________________________________________________________________
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, _
Cancel As Boolean)

'ThisWorkbook.Sheets("FATURA").Unprotect password:="lomokafr"
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet

Set ws = Sheet1

Set cboTemp = ws.OLEObjects("TempCombo")
On Error Resume Next
With cboTemp
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Cancel = True
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)

With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
'open the drop down list automatically
Me.TempCombo.DropDown
End If
errHandler:

'ThisWorkbook.Sheets("FATURA").Protect password:="lomokafr"

Exit Sub

End Sub
_____________________________________________________________________________________________________________________________________________________________________________
Private Sub TempCombo_LostFocus()

'ThisWorkbook.Sheets("FATURA").Unprotect password:="lomokafr"
With Me.TempCombo
.Top = 5
.Left = 5
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
'ThisWorkbook.Sheets("FATURA").Protect password:="lomokafr"

End Sub
_______________________________________________________________________________________________________________________________________________________________________________
Private Sub TempCombo_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)

'ThisWorkbook.Sheets("FATURA").Unprotect password:="lomokafr"
Select Case KeyCode
Case 9 'Tab
ActiveCell.Offset(0, 1).Activate
Case 13 'Enter
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
'ThisWorkbook.Sheets("FATURA").Protect password:="lomokafr"

End Sub
""
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
The code to unprotect and reprotect the sheet is commented out. It will be ignored. So if the code you pasted here is exactly what you are running, it will not unprotect the sheet.

What module contains this code?
What is the sheet name for Sheet1?

Please explain what you mean by "not working". Exactly what happens, and how is that different from what you expect?

The Exit Sub at the end of your first sub is superfluous, but will not cause an error.
 
Upvote 0
The code to unprotect and reprotect the sheet is commented out. It will be ignored. So if the code you pasted here is exactly what you are running, it will not unprotect the sheet.

What module contains this code?
What is the sheet name for Sheet1?

Please explain what you mean by "not working". Exactly what happens, and how is that different from what you expect?

The Exit Sub at the end of your first sub is superfluous, but will not cause an error.
I have commented out both protect and unprotect code lines because I am running actually this code in an unprotected sheet. As soon as I protect the sheet and place out of comments the two protect and unprotect lines the entire code doesn't work.
Regarding sheet1 please note that is the same sheet named "FATURA".
Not working means it doesn't appear the combo box named "TempCombo" and the drop down list.
Thank you for helping me.
 
Upvote 0

Forum statistics

Threads
1,223,929
Messages
6,175,460
Members
452,644
Latest member
gjcase

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