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 Tables - Cannot Search Numbers or Dates

0

I watched your video 

Searchable Lists in Excel - Easy Method https://www.youtube.com/watch?v=56GPcLAj55A and it worked I can search text just fine but I cannot search numbers or dates, please can you advise how I can do this.  I have uploaded a sample file

Answer
Discuss

Discussion

Please make your title more descriptive for people who come here looking for answers in the future.
don (rep: 1989) Jun 19, '21 at 5:58 am
Sam

I agree with Don- both users and contributors need an appropriate title to a question and a full question (no details hidden in Discussion points).

Your question title should probably refer to searching for partial numbers (if that's what you want to do) since the macro with Don's video DOES filter for "full" numbers. It doesn't in your file since you made the change I suggested for partial string searches i.e. you added the bits in bold:
    If FieldName = "ID" Then
        'Filter by ID
    
        MyTable.Range.AutoFilter Field:=2, Criteria1:="*" & UserInput & "*"
(remove the bits in bold and an ID search for 7803072222485 will correctly filter to two records).

On date filters, are you looking to fitler for a single date or a range?

I see that Variatus has provided an answer to your original question. If that misses some requirement, kindly discuss with Variatus and/or EDIT your original question to add the requested details (for the benefit of other users too).
John_Ru (rep: 6142) Jun 19, '21 at 1:16 pm
Add to Discussion

Answers

0

You have three types of search criteria, alphanumeric text, numeric text and dates. The search method is different for each of them. Therefore your code must modify whatever is entered by the user to match the requirement of the filter. The code below does that.

Option Explicit

Sub SearchTable()
' 266 ++ TeachExcel.com

    Dim MainSheet       As Worksheet
    Dim MyTable         As ListObject
    Dim Criterium       As String                      ' item to search for
    Dim FieldName       As String
    Dim Clm             As Variant                      ' field number

    ' set basics
    Set MainSheet = Worksheets("Main")
    With MainSheet
        Set MyTable = .ListObjects("MyTable")
        Criterium = .Range("B3").Value
        FieldName = .Range("D3").Value
    End With

    'Check if there is any user input
    If Criterium <> "" Then                             ' skip if no input
        If IsDate(Criterium) Then
            Criterium = Format(Criterium, "dd/mm/yyyy")
        ElseIf Not IsNumeric(Criterium) Then
            Criterium = "*" & Criterium & "*"
        End If
        With MyTable
            .AutoFilter.ShowAllData                     ' Clear any filters
            Clm = WorksheetFunction.Match(FieldName, .HeaderRowRange, 0)
            .Range.AutoFilter Field:=Clm, Criteria1:=Criterium
        End With
    Else
        MsgBox "Please enter a search criterium in cell B3.", _
               vbExclamation, "No selection made"
    End If
End Sub

My code cuts out all the IFs and reads the field column directly from the table's headers. So, the focus of the code is on modifying the user's entry as opposed to your version of it which uses many lines to identify the column to be filtered. The change may strike you as confusing initially. However, when the chips are down the above procedure does more even though it's shorter. Please find it demonstrated in the attached workbook.

EDIT

For what it's worth, I attach a second workbook which has some changes from the first. For example, the code was moved from the worksheet's code module to a standard code module, which is the normal location of procedures called from a worksheet. But that isn't all.

The main difference is that changing the Field criterium loads a list of available Search criteria into a validation drop-down in B3. And making a selection from B3, immediately sets the filter for that selection.

These additions make your project not only more exciting but also more complex. It will take a lot of additional testing to iron out any possible kinks. I didn't do that. It's yours to play with. Good luck.

Discuss


Answer the Question

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