Do until Loop?

Skyhawk32806

New Member
Joined
May 8, 2010
Messages
26
So my 2nd question for today...

I have lots of data in sheet2. I need to find the right 'code' in column D with the correct date in column C. Once the row of information is found I need to stop the loop, as there will only be one instance in the file. But from what I'm reading a loop is a bad idea (take too long?, as I have to do this loop business about 40 with different codes and dates...)
For example:

Code:
  For Each c In Range("D:D")
                If c.Value = code And cDriverValue.Offset(0, -1) = WeekStartDate Then
                    c.Offset(0, 1).Value.Copy
                    Sheet2.Range("C1").PasteSpecial
                    Exit For
                End If
            Next c

Can it be accomplished using something like
Code:
Cells.Find(What:=Code, After:=[A1], LookIn:=xlFormulas, _
          LookAt:=xlPart, SearchOrder:=xlByRows, _
           SearchDirection:=xlNext, MatchCase:=False).Activate

but then also looking at the cell offset 0, -1? I'm just not sure what the best way to do something like this.

By the way I have just over 10,000 rows of data.

Thanks again, you guys are great.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
You would SET the result of the find to a range object variable.
Then you can use that to offset from...

Rich (BB code):
Dim MyCell As Range
SET MyCell = Cells.Find(..blah..blah..blah) 'Notice no activate here
 
MyCell.Offset(0, 1).Copy

Hope that helps.
 
Upvote 0
You would SET the result of the find to a range object variable.
Then you can use that to offset from...

Rich (BB code):
Dim MyCell As Range
SET MyCell = Cells.Find(..blah..blah..blah) 'Notice no activate here
 
MyCell.Offset(0, 1).Copy

Hope that helps.

Hmmm...

So I could something like:?

Rich (BB code):
Dim MyCell As Range
SET MyCell = Cells.Find(CODE) 
 
MyCell.Offset(0, 1).Copy

But how do I only copy MyCell.Offset(0, 1) if MyCell(0, -1) = WeekStartDate? Is there a find next function or find until that could be used?

I guess I could combine the 2 columns together to make unique values, I'm just trying to figure out the best way to get this done.

Thanks!
 
Upvote 0
Here's a sample
Searches column A for "Jon"
If Offset(0,1) = "x" then gives a message box

Hope it helps.

Code:
Sub test()
Set MyCell = Range("A:A").Find("Jon", Range("A1"))
 
If Not MyCell Is Nothing Then
    If MyCell.Offset(0, 1) = "x" Then
        MsgBox "found in " And MyCell.Address
    Else
        Do Until MyCell.Offset(0, 1).Value = "x"
            Set MyCell = Range("A:A").Find("Jon", MyCell)
        Loop
        MsgBox "found in " And MyCell.Address
    End If
Else
    MsgBox "Not Found"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
Members
452,915
Latest member
hannnahheileen

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