Find Value

RLJ

Active Member
Joined
Mar 15, 2011
Messages
417
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet that In Column A, I want to Look up instances where the cell has a value of ABC123 and then run a Copy Data Code and return to look up the next instance of ABC123, but the code is loosing ABC123 and not going through all instances of it. I made some comments in my code below where it is breaking.

Rich (BB code):
Sub Trial()


    Sheets("Deal Tracker").Select
    
    With Worksheets("Deal Tracker").Range("a1:a500")
     Set c = .Find("ABC123", LookIn:=xlValues)
     If Not c Is Nothing Then
        firstAddress = c.Address
        Do
            c.Select
            CopyData
            Set c = .FindNext(c) 'When the code gets to this point, c is still = "ABC123"
        If c Is Nothing Then     'But immediately after c goes to "Nothing" even though there are still more instances of "ABC123"
            GoTo DoneFinding
        End If
        Loop While c.Address <> firstAddress
      End If
DoneFinding:
End With
End Sub


Sub CopyData()

    Dim lookVal As String


    ActiveCell.Offset(0, 1).Select
    lookVal = ActiveCell.Value 'Property Name
    
    Sheets("Import Temp").Select 'Main Sheet that is being Updated
    
    Cells.Find(What:=lookVal, After:=ActiveCell, LookIn:= _
        xlFormulas, lookat:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
        xlNext, MatchCase:=False, SearchFormat:=False).Activate
        Range("C" & (ActiveCell.Row)).Select
    
    ActiveCell.Offset(0, 9).Copy
    
    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 2).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
        
    Sheets("Import Temp").Select 'Main Sheet that is being Updated
    ActiveCell.Offset(0, 10).Copy


    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
        
    Sheets("Import Temp").Select 'Main Sheet that is being Updated
    ActiveCell.Offset(0, 19).Copy


    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
        
    Sheets("Import Temp").Select 'Main Sheet that is being Updated
    ActiveCell.Offset(0, 20).Copy


    
    Sheets("Deal Tracker").Select 'Main Sheet where data is being copied
    ActiveCell.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, skipBlanks _
        :=False, Transpose:=False
    ActiveCell.Offset(0, -6).Select


End Sub

I'm lost as to why this is happening. I'm really hoping this can be solved.

Thanks for all your help.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
It's because you are using .Find in the CopyData macro.
As FindNext repeats the previous Find its looking for lookVal, not ABC123
 
Upvote 0
Can I change the code so that it can still look for ABC123?
 
Upvote 0
Try
Code:
Sub Trial()
   Dim Fnd As Range
   Dim i As Long, Qty As Long

    
   Worksheets("Deal Tracker").Select
   
   Set Fnd = Range("a1")
   Qty = Application.CountIf(Range("a1:a500"), "Devon")
   For i = 1 To Qty
      Set Fnd = Range("a1:a500").find("Devon", Fnd, , , , xlNext, False, , False)
      Fnd.Select
      CopyData
   Next i
End Sub
 
Upvote 0
This looks like it is working just as I need.
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

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