ExcelGirl1988
New Member
- Joined
- Mar 27, 2017
- Messages
- 44
Hi,
I have been working on a code for my workbook that will search all the worksheets for complaints between 2 dates that the user inputs and then also searches for a keyword within the complaints so basically on the summary page the only complaints that will show will be ones logged between 2 dates that include the keyword but I have been struggling a bit with it because when I run the code it crashes my Excel, I don't know if I have made it too complicated, I might need some help to make it simpler? Any help offered would be greatly appreciated. The code I am using is below:
I have been working on a code for my workbook that will search all the worksheets for complaints between 2 dates that the user inputs and then also searches for a keyword within the complaints so basically on the summary page the only complaints that will show will be ones logged between 2 dates that include the keyword but I have been struggling a bit with it because when I run the code it crashes my Excel, I don't know if I have made it too complicated, I might need some help to make it simpler? Any help offered would be greatly appreciated. The code I am using is below:
Code:
Sub Double_Search()
Dim erow As Long, i As Long, instances As Long, lastrow As Long
Dim myDate As Date, StartDate As Date, EndDate As Date
Dim ws As Worksheet, wsSummary As Worksheet, sht As Worksheet
Dim answer As VbMsgBoxResult
Dim myString As String, firstaddress As String
Dim c As Range
Dim mySize As XlLookAt
Dim found As Boolean
Set wsSummary = ThisWorkbook.Worksheets("Summary")
Application.ScreenUpdating = False
With Worksheets("Home")
StartDate = .Range("E5").Value
EndDate = .Range("E6").Value
End With
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Summary" And ws.Name <> "Home" And ws.Name <> "Data" Then
Application.CutCopyMode = False
For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
Application.CutCopyMode = False
myDate = ws.Cells(i, 2)
If myDate >= StartDate And myDate <= EndDate Then
erow = wsSummary.Cells(wsSummary.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Cells(i, 1).Resize(i, ws.Cells(i, ws.Columns.Count).End(xlToLeft).Column).Copy _
Destination:=wsSummary.Cells(erow, 1)
Application.CutCopyMode = False
End If
Next i
End If
Next ws
wsSummary.Activate
myString = Application.InputBox("Enter Keyword")
If myString = "" Then Exit Sub
If Len(myString) = 0 Then
answer = MsgBox("The Search Field Can Not Be Left Blank" _
& vbLf & vbLf & "Do You Want To Try Again?", vbYesNo + vbQuestion, "Search")
Else
If MsgBox("Exact Match Only? " & vbCrLf & vbCrLf & _
"Yes For Exact Match Of " & myString & vbCrLf & vbCrLf & _
"No For Any Match Of " & myString, vbYesNo + vbQuestion) = _
vbYes Then mySize = xlWhole Else mySize = xlPart
End If
With Worksheets("Summary").UsedRange
Set c = .Find(myString, LookIn:=xlValues, LookAt:=mySize, _
SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not c Is Nothing Then firstaddress = c.Address
found = True
Set c = .FindNext(c)
Do While c.Address <> firstaddress
Loop
End With
Set sht = ThisWorkbook.Worksheets("Home")
wsSummary.Range("A:F").RemoveDuplicates Columns:=Array(1, 3), Header:=xlYes
Application.ScreenUpdating = True
lastrow = wsSummary.Cells(Rows.Count, "A").End(xlUp).Row - 1
If lastrow = 0 Then
MsgBox "No Complaints Found", , "Search Complete"
Else
answer = MsgBox("There are " & lastrow & " complaints found" & vbNewLine & _
"Go to Summary sheet now?", vbYesNo, "Search Complete")
If answer = vbYes Then wsSummary.Activate
End If
End Sub