Sub ProjectDraft45()
Dim Main_Sheet As String
Dim Search_Cell As String
Dim SearchType_Cell As String
Dim Paste_Cell As String
Dim Searched_Sheets As Variant
Dim Searched_Tables As Variant
Dim Copy_Format As Boolean
Dim Last_Row As Long
Dim Last_Column As Long
Dim Used_Range As Range
Dim Value1 As String
Dim Case_Sensitive As Boolean
Dim Count As Long
Dim S As Long, i As Long, j As Long
Dim Value2 As String
Dim Rng As Range
Dim Paste_Range As Range
Dim UniqueDict As Object
Dim RowString As String
Main_Sheet = "Conditional"
Search_Cell = "B10"
SearchType_Cell = "C10"
Paste_Cell = "B14"
Searched_Sheets = Array("Product List")
Searched_Tables = Array("Table1")
Copy_Format = True
' Clear the entire paste range before starting the search
With Sheets(Main_Sheet)
Last_Row = .Cells(.Rows.Count, .Range(Paste_Cell).Column).End(xlUp).Row
Last_Column = .Cells(.Range(Paste_Cell).Row, .Columns.Count).End(xlToLeft).Column
Set Used_Range = .Range(.Cells(.Range(Paste_Cell).Row, .Range(Paste_Cell).Column), .Cells(Last_Row, Last_Column))
Used_Range.ClearContents
Used_Range.ClearFormats
End With
Value1 = Sheets(Main_Sheet).Range(Search_Cell).Value
Count = -1
If Sheets(Main_Sheet).Range(SearchType_Cell).Value = "Case-Sensitive" Then
Case_Sensitive = True
ElseIf Sheets(Main_Sheet).Range(SearchType_Cell).Value = "Case-Insensitive" Then
Case_Sensitive = False
Else
MsgBox ("Choose a Search Type.")
Exit Sub
End If
' Initialize the dictionary for unique rows
Set UniqueDict = CreateObject("Scripting.Dictionary")
For S = LBound(Searched_Sheets) To UBound(Searched_Sheets)
Set Rng = Sheets(Searched_Sheets(S)).Range(Searched_Tables(S))
For i = 1 To Rng.Rows.Count
For j = 1 To Rng.Columns.Count
Value2 = Rng.Cells(i, j).Value
If PartialMatch(Value1, Value2, Case_Sensitive) = True Then
' Create a string representation of the row
RowString = ""
For k = 1 To Rng.Columns.Count
RowString = RowString & Rng.Cells(i, k).Value & "|"
Next k
' Check if the row is unique
If Not UniqueDict.exists(RowString) Then
UniqueDict.Add RowString, True
Count = Count + 1
Rng.Rows(i).Copy
Set Paste_Range = Sheets(Main_Sheet).Cells(Range(Paste_Cell).Row + Count, Range(Paste_Cell).Column)
If Copy_Format = True Then
Paste_Range.PasteSpecial Paste:=xlPasteAll
Else
Paste_Range.PasteSpecial Paste:=xlPasteValues
End If
End If
End If
Next j
Next i
Next S
Application.CutCopyMode = False
End Sub
Function PartialMatch(Value1 As String, Value2 As String, Case_Sensitive As Boolean) As Boolean
If Case_Sensitive Then
PartialMatch = InStr(1, Value2, Value1, vbBinaryCompare) > 0
Else
PartialMatch = InStr(1, Value2, Value1, vbTextCompare) > 0
End If
End Function