Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

searchable list filtering

0

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

Answer
Discuss

Answers

0

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   :-)

Discuss
0

 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..

Discuss

Discussion

@John
Looks like, once again, we both arived at the same/similar solution and posted around the same time. LOL

Cheers   ;-)
WillieD24 (rep: 557) Dec 30, '22 at 5:30 pm
@Willie You're right! Suspect we were writing answers at much the same time but I was interrupted and answered later, before I spotted your response.

Happy New Year! 
John_Ru (rep: 6142) Dec 30, '22 at 5:44 pm
So did you try that Sayanrai?
John_Ru (rep: 6142) Jan 4, '23 at 9:16 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login