Help on loop

tigersden

Board Regular
Joined
Oct 5, 2005
Messages
91
I am using a user form to enter a start date & end date. The dates are found using the find method and set the rows where bith dates are on the worksheet.
I need advice on how to loop to set the 2 variables for the dates rows at present I have the same code twice but using the dates see below:
Code:
        'Start Date 
        strStartDate = Format(txtStartDT, "Short Date")
 
        'Find Start Date Row
        Set rng = rngCol.Find(What:=CDate(strStartDate), _
                After:=Cells(1), _
                LookIn:=xlValues, _
                LookAt:=xlPart, _
                SearchOrder:=xlByRows, _
                SearchDirection:=xlNext, _
                MatchCase:=False, _
                SearchFormat:=False)
            
    'Set Start Date Row
    lngStartRow = rng.Row
    
    'End Date
    strEndDate = Format(txtEndDT, "Short Date")
 
        'Find Stop Date
        Set rng = rngCol.Find(What:=CDate(strEndDate), _
                          After:=Cells(1), _
                          LookIn:=xlValues, _
                          LookAt:=xlPart, _
                          SearchOrder:=xlByRows, _
                          SearchDirection:=xlNext, _
                          MatchCase:=False, _
                          SearchFormat:=False)
    
        
    'Set Stop Date Row For Copying
    lngStopRow = rng.Row
Thanks in advance
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Von Pookie

MrExcel MVP
Joined
Feb 17, 2002
Messages
13,686
I haven't tested this, but I think you could use a small array:

Code:
Dim myDates As Variant, i As Long

'set start date and end date
strstartdate = Format(txtStartDT, "Short Date")
strenddate = Format(txtEndDT, "Short Date")

'put the variables in an array
myDates = Array(strstartdate, strenddate)

For i = LBound(myDates) To UBound(myDates)
    Set rng = rngCol.Find(What:=CDate(myDates(i)), _
    After:=Cells(1), LookIn:=xlValues, LookAt:=xlPart)
    
    If Not rng Is Nothing Then
        If i = 0 Then
            lngStartRow = rng.Row
        Else
            lngStopRow = rng.Row
        End If
    Else
        MsgBox "Value was not found"
    End If
Next i
 

Forum statistics

Threads
1,136,701
Messages
5,677,284
Members
419,684
Latest member
BOB101

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
Top