How would I search all rows and columns in sheet1 for a particular string, then copy entire row to sheet2 if found, without creating duplicates?
Here's what I have so far based upon this answer but I believe I need to loop this for all columns. This is just searching the first column A.
Sub Main()
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
Call searchtext("organic", "Organic Foods")
wb1.Save
End Sub
Private Sub searchtext(term, destinationsheet)
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
Dim ws1 As Worksheet
Set ws1 = wb1.Sheets(1) 'assumes raw data is always first sheet
Dim ws2 As Worksheet
Dim copyFrom As Range
Dim lRow As Long
With ws1
.AutoFilterMode = False
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
With .Range("A1:A" & lRow)
.AutoFilter Field:=1, Criteria1:="=*" & term & "*"
Set copyFrom = .Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow
End With
.AutoFilterMode = False
End With
'~~> Destination File
Set ws2 = wb1.Worksheets(destinationsheet)
ws2.Cells.ClearContents
With ws2
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lRow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lRow = 1
End If
copyFrom.Copy .Rows(lRow)
End With
End Sub
And when I try to loop out then dedupe, the code below only compares the first two columns. How do I specify all columns to compare for duplicates?
Private Sub RemoveDuplicates(destinationsheet)
Dim wb1 As Workbook
Set wb1 = ThisWorkbook
With wb1.Worksheets(destinationsheet)
Set Rng = Range("A1", Range("B1").End(xlDown))
Rng.RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes
End With
End Sub