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

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
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
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,738
Members
449,094
Latest member
dsharae57

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