Having Trouble with the Loops in my code

ExcelGirl1988

New Member
Joined
Mar 27, 2017
Messages
44
Hi,

I am trying to set up a search function via VBA in my workbook, the aim is for a message box to come up asking for a start date and then when this is entered another message box will come up asking for the end date, this will then search for any of these dates in the workbook and copy and paste the rows into a new worksheet but I keep running into an error where it says 'compile error: Do without Loop'. Please can someone help? The code is below.


Code:
Sub FindCopy()
 Dim myString1, mystring2, firstAddress As String
 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 c = .Find(myString1, mystring2, LookIn:=xlValues, LookAt:=mySize, _
     SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
'Perform Copy/Paste/FindNext if myString is found'
   If Not c Is Nothing Then
   firstAddress = c.Address
   found = True
     Do
'Copy entire Row to next empty Row in destination sheet if date criterion satisfied'
      foundDate = c.EntireRow.Cells(2).Value
      If foundDate >= startDate And foundDate <= finishDate Then
        nxtRw = nxtRw + 1
        c.EntireRow.Copy wsDestination.Range("A" & nxtRw)
     End If
'Search again'
    Set c = .FindNext(c)
'stop when search range complete'
    Loop While c.Address <> firstAddress
    End If
   End With
  Next i
'inform user if record not found'
  If Not found Then response = MsgBox(myString1 & mystring2 & 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
 Exit Do
 Unload (myString1)
 Unload (mystring2)
End Sub
 
Last edited by a moderator:

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Every "Do" statement requires a matching "Loop" statement in the same way that every "For" requires a matching "Next". Your last Do statement

Code:
      'try again'
        Do Until response = vbNo
            Exit Do
            Unload (myString1)
            Unload (mystring2)
        End Sub


is missing a matching Loop statement
 
Upvote 0
Hi,

I have tried to put a loop statement in on this part but I keep getting errors and I am not sure how to rectify it. I am still quite new at VBA. Could you help?
 
Upvote 0
I think I have sorted out the Loop issue but now the code is throwing up another error with another part of the code, I can't seem to get it all to work:

Code:
Set c = .Find(myString, LookIn:=xlValues, LookAt:=mySize, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
[\Code]
 
Upvote 0
VBA can throw a lot of errors. If you are asking for help with an error, you should post the specific error message
 
Upvote 0
Hi,

The latest error is run-time error '9': subscript out of range and when I click 'debug' it highlights the following part of my code which I have been working on:

Code:
Set Search1 = .Find(what:=myString1, LookIn:=xlValues, LookAt:=mySize, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,         SearchFormat:=False)

the full code I have amended is below:
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:=myString1, LookIn:=xlValues, LookAt:=mySize, _
    SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
    If Not Search1 Is Nothing Then
    Set Search2 = .Find(what:=mystring2, 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 = c.Address
     found = True
      Do
'Copy entire Row to next empty Row in destination sheet if date criterion satisfied'
       foundDate = c.EntireRow.Cells(2).Value
       If foundDate >= startDate And foundDate <= finishDate Then
        nxtRw = nxtRw + 1
        c.EntireRow.Copy wsDestination.Range("A" & nxtRw)
       End If
'Search again'
       Set c = .FindNext(c)
'stop when search range complete'
       Loop While c.Address <> firstAddress
      End If
     End With
    Next i
'inform user if record not found'
 If Not found Then response = MsgBox(myString1 & mystring2 & 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
 Unload (myString1)
 Unload (mystring2)
 End If
End Sub
 
Upvote 0
Code:
Set Search1 = .Find(what:=myString1, LookIn:=xlValues, LookAt:=mySize, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False,         SearchFormat:=False)

As MARK858 says, "mysize" is an illegal value for LookAt. It should be xlWhole or xlPart.

You've gone to the trouble to convert the entered date strings to excel dates, so why are you searching for mystring1 instead of startDate? e.g.

Code:
Set Search1 = .Find(what:=startDate, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
 
Upvote 0

Forum statistics

Threads
1,214,854
Messages
6,121,941
Members
449,056
Latest member
denissimo

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