VB code to search according to data in search field.

MSchädler

Board Regular
Joined
Apr 27, 2017
Messages
95
Hello, I have an excel sheet with several columns.Each column stands for specific historical data.
I would like to search in the excel sheetover a search field (command button).
A reset command button is planned toreset the sheet after every search and to empty the search field (commandbutton).
The search is limited to the columnsM to U.
All rows containing the searchcriteria have to be shown, all other rows must be hidden. The difficulty is todo a search for date, text and numbers or both.
Thanks for any help!
Marc


This shows my basic sheet set-up with some testdata.


<strike></strike>
MNOPQRSTU
ProcType
clip_image003.png
PID

<tbody>
</tbody>
DateUSPClusR-OTracLine
StatusTPPP000000121.10.20171151747ABCDE-FDEDiv.
TrueGPPP000000221.01.20171150453CDE-FFFDBN-GB
FalseDivP000000310.11.20151153699AIKG_JJU_HH450
FalseGPPP000000421.11.20171153860ZF-GGGHZ-TRVDiv.
Status RolloutTPPP000000521.11.20161154362WERT-KLHJDiv.
Status RolloutDivP000000601.05.20171154551BIU_JHZT-K20-30321
Status RolloutTPPP000000721.11.20171149739ABCDE-FDEDiv.
StatusTPPP000000810.11.20151153860CDE-FFFDDiv.
TrueGPPP000000921.11.20171154362AIKG_JJU_HHBN-GB50

<colgroup><col span="3"><col><col span="5"></colgroup><tbody>
</tbody>
<strike></strike>

 
Hello AFPathfinder,
Thanks for sharing your information. Just to keep things simple, here in Switzerland the date format is DD.MM.YYYY.
This is what has to be displayed in column P at start and at the end of any operation.

I could imagine the following scenario; when r
unning the macro we change the search format from DD.MM.YYYY to the Number, change the format in column "P" from date to numbers as well, fill the searched number into "B2" to "J9", do the search and at the end reformat column "P" back to DD.MM.YYYY.
This might work. Just a thought.

I'm not in a real rush to get a solution. Lets keep at it, I'll try on my side as well and share if results are available.


Kind regards, Marc
 
Last edited:
Upvote 0

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
This may be a little over-engineered and probably a bit inefficient, but this should allow Excel to see it as a date. Give this a shot and see if it works.
Code:
Private Sub CommandButton1_Click()
    Dim x As Integer
    Dim finalRow As Integer
    
    finalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    If Sheets("Tabelle1").FilterMode = True Then
        Sheets("Tabelle1").ShowAllData
    End If
    
    If Len(Suchfeld.Value) > 5 And Len(Suchfeld.Value) < 11 And InStr(Suchfeld.Value, ".") > 0 Then
        Suchfeld.Value = StringToDate Suchfeld.Value
    End If
    
    If Suchfeld.Value <> "" And Not IsNumeric(Suchfeld.Value) And Not IsDate(Suchfeld.Value) Then
        For i = 2 To 10
            Cells(i, i).Value = "*" & Suchfeld.Value & "*"
        Next i
    Else
        For i = 2 To 10
            Cells(i, i).Value = Suchfeld.Value
        Next i
    End If
    
    Sheets("Tabelle1").Columns("M:U").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _
        ("B1:J9"), Unique:=False
        
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    
    Range("M2").Select              'pointer to M2"
    
End Sub

Function StringToDate(strDate As String) As Date
'Converts a d.m.yy date into a Date object

    Dim iPeriod1 As Integer, iPeriod2 As Integer
    Dim iYear As Integer
    Dim iMonth As Integer
    Dim iDay As Integer
    
    iPeriod1 = InStr(strDate, ".")
    iPeriod2 = InStr(iPeriod1 + 1, strDate, ".")
    
    iDay = Left(strDate, iPeriod1 - 1)
    iMonth = Mid(strDate, iPeriod1 + 1, iPeriod2 - iPeriod1 - 1)
    iYear = Right(strDate, Len(strDate) - iPeriod2)
    
    StringToDate = DateSerial(iYear, iMonth, iDay)
End Function
 
Upvote 0
Hello AFPathfinder. Many thanks again for your support. I have entered your code into the file and I get a Syntaxerror at the line

If Len(Suchfeld.Value) > 5 AndLen(Suchfeld.Value) < 11 And InStr(Suchfeld.Value, ".") >0 Then
Suchfeld.Value= StringToDate Suchfeld.Value

Maybe you have an idea why?
I have the testfile under: https://www.dropbox.com/s/kut6zdwn77mu1p1/Test_SearchPlay6.xlsm?dl=0

Thanks for your idea? Marc<strike>
</strike>

 
Upvote 0
My fault. The function call syntax wasn't correct. Try this one:
Code:
Private Sub CommandButton1_Click()
    Dim x As Integer
    Dim finalRow As Integer
    
    finalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    If Sheets("Tabelle1").FilterMode = True Then
        Sheets("Tabelle1").ShowAllData
    End If
    
    If Len(Suchfeld.Value) > 5 And Len(Suchfeld.Value) < 11 And InStr(Suchfeld.Value, ".") > 0 Then
        Suchfeld.Value = StringToDate(Suchfeld.Value)
    End If
    
    If Suchfeld.Value <> "" And Not IsNumeric(Suchfeld.Value) And Not IsDate(Suchfeld.Value) Then
        For i = 2 To 10
            Cells(i, i).Value = "*" & Suchfeld.Value & "*"
        Next i
    Else
        For i = 2 To 10
            Cells(i, i).Value = Suchfeld.Value
        Next i
    End If
    
    Sheets("Tabelle1").Columns("M:U").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range _
        ("B1:J9"), Unique:=False
        
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    
    Range("M2").Select              'pointer to M2"
    
End Sub


Function StringToDate(strDate As String) As Date
'Converts a d.m.yy date into a Date object


    Dim iPeriod1 As Integer, iPeriod2 As Integer
    Dim iYear As Integer
    Dim iMonth As Integer
    Dim iDay As Integer
    
    iPeriod1 = InStr(strDate, ".")
    iPeriod2 = InStr(iPeriod1 + 1, strDate, ".")
    
    iDay = Left(strDate, iPeriod1 - 1)
    iMonth = Mid(strDate, iPeriod1 + 1, iPeriod2 - iPeriod1 - 1)
    iYear = Right(strDate, Len(strDate) - iPeriod2)
    
    StringToDate = DateSerial(iYear, iMonth, iDay)
End Function
 
Upvote 0
Good day AFPathfinder.
Many thanks again for your help.
I have entered your code into the sheet and did a filter search. Somehow it doesn't filter the date.
Here is the link to my drop box for sharing: https://www.dropbox.com/s/unkf0fh6kx5fb2k/Test_SearchPlay7.xlsm?dl=0

I did the stepping through the macro and saw that when going through the *Function stringtodate()" and then through the "if Suchfeld value..." it entered the format dd.mm.yyyy into "B2" to "J9" of the sheet.
Is this correct? Should it not convert into a number?
What do you think.

Appreciate your help, because I'm passed with my knowledge on this.
Marc



 
Upvote 0
It is a number, but it formats the number into a date, just like a cell does. I assume it would wrap the date as your operating system set region. I can set to the date/time of the column, but Excel will still search by the date format of my computer. The important thing is that Excel sees it as a date and not a string. If you want to pass it as a straight integer, you'll need to format the date to show as such. You'll want to change the format at the point of insert into the advanced filter area.
Code:
    If Suchfeld.Value <> "" And Not IsNumeric(Suchfeld.Value) And Not IsDate(Suchfeld.Value) Then
        For i = 2 To 10
            Cells(i, i).Value = "*" & Suchfeld.Value & "*"
        Next i
[COLOR=#0000ff]    ElseIf IsDate(Suchfeld.Value) Then[/COLOR]
[COLOR=#0000ff]        For i = 2 To 10[/COLOR]
[COLOR=#0000ff]            Cells(i, i).Value = Format(Suchfeld.Value, "######")[/COLOR]
[COLOR=#0000ff]        Next i[/COLOR]
    Else
        For i = 2 To 10
            Cells(i, i).Value = Suchfeld.Value
        Next i
    End If

This passes the date value as an integer, but in my testing, it doesn't make a difference. However, it is filtering just fine for me. If you are having issues with it filtering, place a stop on the advanced filter line and use the immediate window to investigate the values it is trying to match.
Code:
? IsDate(Range("P5").Value)
This tests the value in cell P5 to make sure it is seen by Excel as a date; returns True if it sees a date in that cell.
 
Upvote 0
Good evening,
it is 5pm here and I have to give you a very big thank you!!!
It works wonderfully in my test file too, and I'm amazed that it took this simple input into the vb code.

I will now do some testing in the life sheet and see how it behaves. To date we have 823 rows of data.

I'll allow myself to contact you again, if I have some issues with the live sheet.
Otherwise my sincere thanks for your very professional help.

Very kind regard.
Marc:)
 
Upvote 0
You're welcome! It was a nifty problem to solve and I can add the solution to my VBA toolbox. Let me know if you run into any issues.
 
Upvote 0
Good dayAFPathfinder
First, I wish you a HAPPY NEW YEAR.
Since our last contact I was able to work well with my Excel and your vb-code, Now I have the following question/request.
What line in the vb-code do I have to change in order to filter, not end ofmonth date but actual day date or older?
This would allow me to archive the lines with dates equal today or older.
Thanks for your appreciated inputs. Marc
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,523
Messages
6,125,318
Members
449,218
Latest member
Excel Master

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