+ Reply to Thread
Results 1 to 9 of 9

Find&Copy only copying 1 instance of match

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-02-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    209

    Find&Copy only copying 1 instance of match

    Hi guys,

    Can someone please tell me how to get my below code to not copy 1 match of the inputed item, rather all of them.

    At the moment if i enter 'Sarah' only 1 sarah is found when in reality sarah appears many times, though only 1 row is being copied with it.

    Thanks in advance,

    
    Sub findandmake()
    
    Dim ws1 As Worksheet 'Search
    Dim SearchRng As Range
    Dim ws2 As Worksheet 'Physical
    Dim FindRng2 As Range
    Dim ws3 As Worksheet 'Virtual
    Dim FindRng3 As Range
    Dim Ws4 As Worksheet 'Output
    
    Dim CopyRng As Range 'Set when Found
    Dim PasteRng As Range 'keeps the latest row
    
    Set ws1 = Worksheets("Servers To Find")
    Set SearchRng = ws1.Range("A1:A" & ws1.Cells(ws1.Cells.Rows.Count, 1).End(xlUp).Row)
    
    Set ws2 = Worksheets("Physical Servers")
    Set FindRng2 = ws2.Range("P2:P" & ws2.Cells(ws2.Cells.Rows.Count, 4).End(xlUp).Row)
    
    Set Ws4 = Worksheets("Server Results")
    Set PasteRng = Ws4.Cells(1, 1)
    
    
    'Loop through cells removing excess spaces in CRRng of Croff Ref Data WS
    With Sheets("Servers To Find")
            
        For Each cl In SearchRng
            If Len(cl) > Len(WorksheetFunction.Trim(cl)) Then
                cl.Value = WorksheetFunction.Trim(cl)
            End If
        Next cl
    
    End With
    
    
    'Clear all
    Ws4.Cells.ClearContents
    
    'First the Headers
    ws2.Range("2:2").Copy Destination:=PasteRng
    Set PasteRng = PasteRng.Offset(1, 0)
    
    'If Found in Ws1 then copy entire row to Ws4
    For Each Ccell In SearchRng
      Set CopyRng = FindRng2.Find(What:=Ccell, LookAt:=xlPart)
      If Not CopyRng Is Nothing Then
        CopyRng.EntireRow.Copy Destination:=PasteRng
        Set PasteRng = PasteRng.Offset(1, 0)
                End If
    Next
    
    
    
    Call resizeCol2 'this calls the sub that will resize columns in the Server Results WS.
    Sheets("Menu").Select
      MsgBox ("Server CI search complete. See 'Server Results' work sheet")
    End Sub
    Last edited by SarahPintal; 07-14-2010 at 06:38 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find&Copy only copying 1 instance of match

    Try this:
    Option Explicit
    
    Sub findandmake()
    
    Dim ws1         As Worksheet 'Search
    Dim SearchRng   As Range
    Dim ws2         As Worksheet 'Physical
    Dim FindRng2    As Range
    Dim ws3         As Worksheet 'Virtual
    Dim FindRng3    As Range
    Dim Ws4         As Worksheet 'Output
    
    Dim CopyRng     As Range     'Set when Found
    Dim CopyFirst   As Range
    Dim PasteRng    As Range     'keeps the latest row
    Dim cl          As Range
    
    Set ws1 = Worksheets("Servers To Find")
    Set SearchRng = ws1.Range("A1:A" & ws1.Cells(ws1.Cells.Rows.Count, "A").End(xlUp).Row)
    
    Set ws2 = Worksheets("Physical Servers")
    Set FindRng2 = ws2.Range("P2:P" & ws2.Cells(ws2.Cells.Rows.Count, "D").End(xlUp).Row)
    
    Set Ws4 = Worksheets("Server Results")
    Set PasteRng = Ws4.Cells(1, 1)
    
    
    'Loop through cells removing excess spaces in CRRng of Croff Ref Data WS
    With ws1
            
        For Each cl In SearchRng
            If Len(cl) > Len(WorksheetFunction.Trim(cl)) Then
                cl.Value = WorksheetFunction.Trim(cl)
            End If
        Next cl
    
    End With
    
    
    'Clear all
    Ws4.Cells.ClearContents
    
    'First the Headers
    ws2.Range("2:2").Copy Destination:=PasteRng
    Set PasteRng = PasteRng.Offset(1, 0)
    
    'If Found in Ws1 then copy entire row to Ws4
    For Each cl In SearchRng
      Set CopyRng = FindRng2.Find(What:=cl, LookAt:=xlPart)
      
      If Not CopyRng Is Nothing Then
        
        If CopyFirst Is Nothing Then Set CopyFirst = CopyRng
        Do
          CopyRng.EntireRow.Copy Destination:=PasteRng
          Set PasteRng = PasteRng.Offset(1, 0)
          Set CopyRng = FindRng2.FindNext(After:=CopyRng)
        Loop Until CopyRng.Address = CopyFirst.Address
        
      End If
    Next cl
    
    Call resizeCol2 'this calls the sub that will resize columns in the Server Results WS.
    Sheets("Menu").Select
      MsgBox ("Server CI search complete. See 'Server Results' worksheet")
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    12-02-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    209

    Re: Find&Copy only copying 1 instance of match

    Hi JB,

    It find all of them now though does not break out of the loop... i had to break out and when i looked at the results there was over 1000 lines, with about 100 instances of the same line.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find&Copy only copying 1 instance of match

    Time to post a sample workbook so we can see this stuff together. Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook.

  5. #5
    Forum Contributor
    Join Date
    12-02-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    209

    Re: Find&Copy only copying 1 instance of match

    Sure thing; It is the second last sub.

    I have put dummy data into the report...

    thanks again
    Attached Files Attached Files

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find&Copy only copying 1 instance of match

    what's the password?

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find&Copy only copying 1 instance of match

    Nevermind, I think an AutoFilter is a much faster method. Try this instead:
    Option Explicit
    
    Sub FindStrings()
    Dim Strings As Range
    Dim MyStr   As Range
    Dim LR      As Long
    Dim wsOUT   As Worksheet
    Application.ScreenUpdating = False
    
    Set wsOUT = Sheets("Server Results")
    wsOUT.Cells.Clear
    Set Strings = Sheets("Servers To Find").Range("A:A").SpecialCells(xlConstants)
    
    With Sheets("Physical Servers")
        .Range("A1").EntireRow.Copy wsOUT.Range("A1")
        .Range("P:P").AutoFilter
        
        For Each MyStr In Strings
            .Range("P:P").AutoFilter Field:=1, Criteria1:="*" & MyStr & "*"
            LR = .Range("P" & .Rows.Count).End(xlUp).Row
            If LR > 1 Then _
                .Range("A2:A" & LR).EntireRow.Copy _
                   wsOUT.Range("A" & Rows.Count).End(xlUp).Offset(1)
        Next MyStr
        
        .Range("P:P").AutoFilter
    End With
    
    wsOUT.Columns.AutoFit
    Set wsOUT = Nothing
    Set Strings = Nothing
    Application.ScreenUpdating = True
    End Sub

  8. #8
    Forum Contributor
    Join Date
    12-02-2009
    Location
    Australia
    MS-Off Ver
    Excel 2003
    Posts
    209

    Re: Find&Copy only copying 1 instance of match

    Worked like a charm - ill try my best to learn how you did this as this was very quick and clean compared to my find subs.

    Thanks

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Find&Copy only copying 1 instance of match

    When evaluating the SAME column row by row, cell by cell for the same value, it's much faster to simply turn on the Data > Filter > AutoFilter and filter that column by the value you're searching for. Once the AutoFilter is on, non-matching rows are hidden so you can literally just copy the whole range and it will ignore the hidden rows...very cool, and much faster overall.

    Play around manually with the AutoFilter so you can see all the various things you can do with it.

    Cheers.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1