VBA help: search function not bringing up any results

ExcelGirl1988

New Member
Joined
Mar 27, 2017
Messages
44
Hi,

I have put together a code which will search between 2 dates in the workbook and then copy and paste the result into a new worksheet but when I run the search it keeps telling me no results were found when I know I have got data in the workbook that meets the dates that I put into the search. Can anyone see what I have done wrong? I am quite new to VBA and still learning.

Code:
Sub FindCopy()
 Dim myString1, mystring2, firstAddress As String
 Dim Unionsearch As Range
 Dim Search1 As Range
 Dim Search2 As Range
 Dim nxtRw As Long, i As Integer
 Dim c As Range
 Dim wsDestination As Worksheet
 Dim mySize As XlLookAt
 Dim found As Boolean
 Dim response As VbMsgBoxResult
 Dim start As String, finish As String
 Dim startDate As Date, finishDate As Date, foundDate As Date


startSearch:
'Initialise nxtRw'
nxtRw = 1
'Get input from user'
Do
 found = False
 myString1 = Application.InputBox("Enter the start date", "Start Date")
   Loop While Not IsDate(myString1)
   startDate = CDate(myString1)
'Exit if Cancelled'
 If myString1 = False Then Exit Sub
'Force valid entry'
 If Len(myString1) = 0 Then
 response = MsgBox("The Search Field Can Not Be Left Blank" _
 & vbLf & vbLf & "Do You Want To Try Again?", vbYesNo + vbQuestion, "Search")
Else
'Get end date'
   Do
    mystring2 = Application.InputBox("Enter the end date", "Finish Date")
    Loop While Not IsDate(mystring2)
    finishDate = CDate(mystring2)
'Exit if Cancelled'
 If mystring2 = False Then Exit Sub
'Force valid entry'
 If Len(mystring2) = 0 Then
 response = MsgBox("The Search Field Can Not Be Left Blank" _
 & vbLf & vbLf & "Do You Want To Try Again?", vbYesNo + vbQuestion, "Search")
Else
'add new sheet'
    If wsDestination Is Nothing Then Set wsDestination = Worksheets.Add(After:=Sheets(Sheets.Count))
'look in each worksheet'
    For i = 1 To ThisWorkbook.Worksheets.Count - 1
    With Worksheets(i).UsedRange
'Search usedrange in sheet'
    Set Search1 = .Find(what:=(startDate), LookIn:=xlValues, LookAt:=xlWhole, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not Search1 Is Nothing Then
    Set Search2 = .Find(what:=(finishDate), LookIn:=xlValues, LookAt:=mySize, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    Set Unionsearch = Union(Search1, Search2)
    End If
'Perform Copy/Paste/FindNext if myString is found'
     If Not Unionsearch Is Nothing Then
     firstAddress = Unionsearch.Address
     found = True
      Do
'Copy entire Row to next empty Row in destination sheet if date criterion satisfied'
       foundDate = Unionsearch.EntireRow.Cells(2).Value
       If foundDate >= startDate And foundDate <= finishDate Then
        nxtRw = nxtRw + 1
        Unionsearch.EntireRow.Copy wsDestination.Range("A" & nxtRw)
       End If
'Search again'
       Set c = .FindNext(Unionsearch)
'stop when search range complete'
       Loop While Unionsearch.Address <> firstAddress
      End If
     End With
    Next i
'inform user if record not found'
 If Not found Then response = MsgBox(startDate & finishDate & Chr(10) & "Search String Not Found" & vbCrLf & vbCrLf & _
  "Do You Want To Try Again?", vbYesNo + vbQuestion, "Not Found") Else Exit Sub
 End If
'try again'
 Do Until response = vbNo
  If response = vbNo Then Exit Do
 Loop
 End If
End Sub
 
Last edited by a moderator:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hello, after reading the first two or three sentences I scipped the rest, here just an idea, if the datess are in column A
Code:
 sub Short_as_possible() with cells(1).currentregion      .Autofilter 1, ">#10/1/2018#", "<#11/1/2018#, xland      .copy sheets(2).cells(1)      .autofilter  end with end sub
Obviously, this is an other concept. regards
 
Upvote 0
Hi, I have tried your suggestion but it doesn't seem to work, it seems to have a problem with:

Code:
.AutoFilter 1, ">#10/1/2018#", "<#11/1/2018#, xland"

It may be due to my dates being in column B in the spreadsheets, I tried to change the code so that it would select the correct cells to check but I don't seem to be getting anywhere.

Kind Regards
 
Upvote 0
OK, so I think I know what is going wrong with my code, I think it is this line:

Rich (BB code):
Set Unionsearch = Union(Search1, Search2)


But I don't know how to fix the error, I get a run-time error 5 when this line of code tries to run. I hope someone can help.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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