Hi,
I have download your practise file searchable list easy method could you please help me to protect data so others can not edit the range but they can search. I protect with password but unable to search error occurs. thank you
Hi,
I have download your practise file searchable list easy method could you please help me to protect data so others can not edit the range but they can search. I protect with password but unable to search error occurs. thank you
Hello sayanrai and welcome to the forum.
I have been a bit busy over the holidays so that is why I didn't answer sooner.
To solve you issue is quite easy.
First: sellect cells C5 (input) and E5 (field), then using "Format Cells" select the "Protection" tab and uncheck "Locked"
Second: In each macro add these 2 lines: (at the beginning) MainSheet.Unprotect "Password" and (before End Sub) MainSheet.Protect "Password".
I have added these to your macros but without a password (shown in bold below; that is the only change made). Replace the word Password with the password of your choosing.
Sub Searchable_List_Simple()
' TeachExcel.com
Dim MainSheet As Worksheet
Dim MyTable As ListObject
Set MainSheet = Sheets("Main")
Set MyTable = MainSheet.ListObjects("MyTable")
UserInput = MainSheet.Range("C5").Value
FieldName = MainSheet.Range("E5").Value
'Check if there is any user input.
If UserInput <> "" Then
MainSheet.Unprotect
'User input a value, so, proceed
'Clear any filters.
MyTable.AutoFilter.ShowAllData
'Apply new filters
If FieldName = "ID" Then
'Filter by ID
MyTable.Range.AutoFilter Field:=1, Criteria1:=UserInput
ElseIf FieldName = "Name" Then
'Filter by Name
MyTable.Range.AutoFilter Field:=2, Criteria1:=UserInput
End If
Else
MsgBox "Yo man, input something!"
End If
MainSheet.Protect
End Sub
Sub Clear_Filters()
Dim MainSheet As Worksheet
Dim MyTable As ListObject
Set MainSheet = Sheets("Main")
Set MyTable = MainSheet.ListObjects("MyTable")
MainSheet.Unprotect
'Clear user input and field
MainSheet.Range("C5").Value = ""
MainSheet.Range("E5").Value = ""
'Clear any filters.
MyTable.AutoFilter.ShowAllData
MainSheet.Protect
End Sub
Happy New Year
Cheers :-)
Hi Sayanrai and welcome to the Forum.
Sorry about the delay in replying (busy over Christmas) but you can protect the sheet to prevent users accessing anything but the two input cells and macro buttons. I've done that in the attached file (with the password myPassword) then modified the macro so that it unprotects the sheet (if there's an input) then reprotects it after searching- see changes and comments in bold below):
Public Pwd As String
Sub Searchable_List_Simple()
' TeachExcel.com
Dim MainSheet As Worksheet
Dim MyTable As ListObject
Set MainSheet = Sheets("Main")
Set MyTable = MainSheet.ListObjects("MyTable")
'record sheet password here
Pwd = "myPassword"
UserInput = MainSheet.Range("C5").Value
FieldName = MainSheet.Range("E5").Value
'Check if there is any user input.
If UserInput <> "" Then
'User input a value, so, proceed
'unprotect sheet
MainSheet.Unprotect (Pwd)
'Clear any filters.
MyTable.AutoFilter.ShowAllData
'Apply new filters
If FieldName = "ID" Then
'Filter by ID
MyTable.Range.AutoFilter Field:=1, Criteria1:=UserInput
ElseIf FieldName = "Name" Then
'Filter by Name
MyTable.Range.AutoFilter Field:=2, Criteria1:=UserInput
End If
'Reprotect sheet
MainSheet.Protect (Pwd)
Else
MsgBox "Yo man, input something!"
End If
End Sub
Note that I've declared the password variable outside the module (and declared the value in the main search macro) so that you only have to record it in one place since it can also be used when the clear module is used, modified as in bold below:
Sub Clear_Filters()
Dim MainSheet As Worksheet
Dim MyTable As ListObject
Set MainSheet = Sheets("Main")
Set MyTable = MainSheet.ListObjects("MyTable")
'unprotect sheet
MainSheet.Unprotect (Pwd)
'Clear user input and field
MainSheet.Range("C5").Value = ""
MainSheet.Range("E5").Value = ""
'Clear any filters.
MyTable.AutoFilter.ShowAllData
'Reprotect sheet
MainSheet.Protect (Pwd)
End Sub
I haven't initialised the password in this sub (which assumes that a search is done before the ShowAll button is pressed).
Note that you'll need to change your sheet password in the worksheet (and record it between inverted comments in the main sub).
Hope this make sense and fixes your problem. If so, please remember to mark this Answer as Selected..