Find data on sheet using date

johnsonk

Board Regular
Joined
Feb 4, 2019
Messages
172
Hi, I have a sheet with data (Sheet1) and I have a search sheet (Sheet2) where I can search the data from sheet1 and shows the results in sheet2 at the moment I do a search entering a batch number and what I need is to be able to do another search by entering the date. Can anyone help..


Private Sub Search_Click()
Dim itemcode As Variant, i As Long
Dim R As Range, f As Range, cell As String

itemcode = Sheet2.Range("c2").Value 'search box
If itemcode = "" Then
MsgBox "Please Enter The Batch number!"
Exit Sub
End If

Set R = Sheet1.Range("A:H") 'column range
Set f = R.Find(itemcode, LookIn:=xlValues, Lookat:=xlWhole)
If Not f Is Nothing Then
cell = f.Address
i = 5 'start in 5th row
Do
Sheet2.Range("A" & i).Resize(1, 8).Value = f.Resize(1, 8).Value
i = i + 1
Set f = R.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
MsgBox "Search Complete"
Else
MsgBox "Batch number does not exist!"
End If
End Sub

Private Sub ClearSearch_Click()
Range("C2").ClearContents
Range("A5:H1000000").ClearContents
ActiveWindow.ScrollRow = 1
Range("C2").Select
End Sub

Private Sub PrintSearch_Click()
Application.CommandBars.ExecuteMso ("PrintPreviewAndPrint")
End Sub

This is sheet2 so what I need is to search for batch number on certain date.
1584628349816.png


Regards
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this

I highlighted the modified rows

Rich (BB code):
Private Sub Search_Click()
  Dim itemcode As Variant, i As Long
  Dim r As Range, f As Range, cell As String
  
  itemcode = Sheet2.Range("C2").Value 'search box
  If itemcode = "" Then
    MsgBox "Please Enter The Batch number!"
    Exit Sub
  End If
  
  Set r = Sheet1.Range("H:H") 'column range
  Set f = r.Find(itemcode, LookIn:=xlFormulas, Lookat:=xlWhole)
  If Not f Is Nothing Then
    cell = f.Address
    i = 5 'start in 5th row
    Do
      Sheet2.Range("A" & i).Resize(1, 8).Value = Sheet1.Cells(f.Row, "A").Resize(1, 8).Value
      i = i + 1
      Set f = r.FindNext(f)
    Loop While Not f Is Nothing And f.Address <> cell
    MsgBox "Search Complete"
  Else
    MsgBox "Batch number does not exist!"
  End If
End Sub
 
Upvote 0
Hi, DanteAmor

I have tried the amended code and tried to do a search using both date and the batch with no joy.

1584793934686.png
1584793999060.png

Could I do it where they have to enter the batch in C2 and when they click in C4 they choose a date from a popup calendar then press search data is that possible ?

1584794492660.png


Regards
 
Upvote 0
You have to have a code to search by date and another to number.
In your images there is no data, you have to test with data.
 
Upvote 0
You can put the image of sheet1 to see the data.
What data do you have in column H on sheet1, are they dates or texts that looks like a date?
What do you put in cell C2 on sheet2 a date or a text that looks like a date?
 
Upvote 0
Hi, DanteAmor
As you can see sheet 1 has the data and sheet 2 is where we do a search by searching the batch but what I also need is to also be able to search using the date.

SHEET 1 (Data)
1585039355926.png

SHEET 2 (Search)
1585039489447.png


Regards
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,693
Members
449,048
Latest member
81jamesacct

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