VBA Copy whole rows based on find hits

Davavo

Board Regular
Joined
Aug 3, 2019
Messages
82
Hi,
could really use some help with this. (hopefully not as dumb as question as last time!

I am trying to write a macro that will search through a load of sheets for a value in column L, and if it finds that value, copy the whole row (and repeat for all non blank rows in sheet)
I will need to loop this through sheets too, but for now, i am just having trouble copying the whole row.
I can find the right cell, and report the address, but I am not sure of the correct method of copying the row. (would prefer to restrict it to defined columns rather than the whole row)

Any help much appreciated.

Code:
Sub CCAutoWrite()

    'Purpose: Find in column 'L' all instances of strings containing "ZHAN".  Copy whole row of each insatnce to another sheet.  Loop for all instances on sheet.  Loop for all sheets in workbook.


    Dim CC_Test As Workbook
    
    Dim Source As Worksheet 'since will expand this macro to loop through sheets
    Dim Target As Worksheet 'target will be the same
    
    Dim lastRow As Long 'name of row on which value was found
    Dim TLastRow As Long 'name of last non blank row of target sheet so that results can be pasted underneath
    
    Dim strSearch As String 'define name of search operation
    
    Dim aCell As Range 'name of range in which the string was found
    Dim copyrng As Range 'name of range to be copied
   
    
    On Error GoTo Whoa 'error exit
    
    Set Source = Sheets("May") 'set for testing prior to coding a loop
    Set Target = Sheets("TargetSheet") 'set for testing prior to coding a loop
    
    TLastRow = Target.Cells(Rows.Count, 1).End(xlUp).Row 'Set lastrow as the bottommost row containing values in the target sheet
    
    
    With Source
    
    lastRow = Source.Range("L" & .Rows.Count).End(xlUp).Row 'this makes lastRow the last non empty row on the source sheet so delimits the search area
    strSearch = "ZHAN"  'defines the search term
    
    'set the value of aCell
    Set aCell = Source.Range("L1:L" & lastRow).Find(what:=strSearch, LookIn:=xlValues, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    
   
   [B] Set copyrng = ****************************************   'how to identify the row on which the value was found?[/B]
    
    'if something is found
    If Not aCell Is Nothing Then
    MsgBox "Value Found in Cell " & aCell.Address 'create a messagebox with address to confirm just for testing during coding
    
   
    copyrng.Copy
    'aCell.EntireRow.Copy 'would be better to restrict to A:Z
    Target.Range("A" & TLastRow).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'pastes to the first blank row
   
    
    End If


    
    Exit Sub
    
Whoa:


    MsgBox Err.Description
    End With
    
End Sub
 
No column headers.
Then what's row 7 ;)

Try
Code:
Sub Davavo()
   Dim Ws As Worksheet, Trgtws As Worksheet
   Dim UsdRws As Long
   
   Set Trgtws = Sheets("TargetSheet")
   For Each Ws In Worksheets
      If Ws.Name <> Trgtws.Name Then
         UsdRws = Ws.Range("L" & Rows.Count).End(xlUp).Row
         Ws.Range("A7:L" & UsdRws).AutoFilter 12, "*zhan*"
         Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Trgtws.Range("L" & Rows.Count).End(xlUp).Offset(1, -11)
         Ws.AutoFilterMode = False
      End If
   Next Ws
End Sub
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Then what's row 7 ;)

Try
Code:
Sub Davavo()
   Dim Ws As Worksheet, Trgtws As Worksheet
   Dim UsdRws As Long
   
   Set Trgtws = Sheets("TargetSheet")
   For Each Ws In Worksheets
      If Ws.Name <> Trgtws.Name Then
         UsdRws = Ws.Range("L" & Rows.Count).End(xlUp).Row
         Ws.Range("A7:L" & UsdRws).AutoFilter 12, "*zhan*"
         Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Trgtws.Range("L" & Rows.Count).End(xlUp).Offset(1, -11)
         Ws.AutoFilterMode = False
      End If
   Next Ws
End Sub

Ah, right, see what you mean. Headers. I thought they would be at the top!

That doesn't quite work. It doesn't collect all of the entries, or it overwrites them. It is also writing some into the K column, when all the rest go into L.
 
Upvote 0
That is probably down to the merged cells.
Unfortunately just unmerging them at the start of the code probably wont help as the values may be in the wrong columns.
 
Upvote 0
Hidden column k!

Code:
Sub Davavo3()   Dim Ws As Worksheet, Trgtws As Worksheet
   Dim UsdRws As Long
   
   Set Trgtws = Sheets("TargetSheet")
   For Each Ws In Worksheets
      If Ws.Name <> Trgtws.Name Then
      Ws.Columns("k").Hidden = False
      
         UsdRws = Ws.Range("L" & Rows.Count).End(xlUp).Row
         Ws.Range("A7:L" & UsdRws).AutoFilter 12, "*zhan*"
         Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Trgtws.Range("L" & Rows.Count).End(xlUp).Offset(1, -11)
         Ws.AutoFilterMode = False
      End If
   Next Ws
End Sub


Works!
 
Last edited:
Upvote 0
Glad you sorted it & thanks for the feedback
 
Upvote 0
That works without even unmerging the evil in advance Fluff.
Cant thank you enough for your help with this.

Very much appreciated!
 
Upvote 0

Forum statistics

Threads
1,215,692
Messages
6,126,230
Members
449,303
Latest member
grantrob

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