This Code has 5 steps which will describe here.
First Step
Declaration of variables and assigning variables
Dim a As Long, b As Long, c As Long, d As Long
Dim i As Integer, j As Integer, k As Integer, l As IntegerDim dl As Worksheet, da As Worksheet, wo As Worksheet, wk As Worksheet
Dim st As VariantDim rn As RangeApplication.EnableEvents = FalseApplication.ScreenUpdating = FalseSet dl = Sheets("Data List")Set da = Sheets("Dashboard")Set wo = Sheets("List Without Keywords")Set wk = Sheets("List With Keywords")
There are 5 sheets in this workbook. I have assigned 4 of them to variables for easy use. Then turned off events and screen updating to speed up the macro,
Second Step
a = dl.Cells(Rows.Count, "A").End(xlUp).Rowi = dl.Cells(1, Columns.Count).End(xlToLeft).Columnj = da.Cells(Rows.Count, "A").End(xlUp).Rowwo.Cells.Clearwk.Cells.Cleardl.Activatedl.Range("A1", Cells(1, i)).Copy Destination:=wo.Range("A1")dl.Range("A1", Cells(1, i)).Copy Destination:=wk.Range("A1")b = 2l = Range(da.Range("B2").Value & 1).Column
variable b is going to use as row number of first row which is blank in the sheet List With Keywords sheet. Initially it is 2
Step Three
Loop through every cell in keyword list, filter data set using the each keyword and copy data with keyword to the sheet List With Keywords. Finally visible cells in the Data List sheet colored using yellow.
For k = 2 To jst = da.Cells(k, "A").Valuedl.Range("A1", Cells(a, i)).AutoFilter Field:=l, Criteria1:="=*" & st & "*", Operator:=xlAndSet rn = NothingOn Error Resume NextSet rn = dl.Range("A2", Cells(a, i)).SpecialCells(xlCellTypeVisible)On Error GoTo 0If Not rn Is Nothing Thenrn.Copy Destination:=wk.Range("A" & b)b = b + rn.Cells.Count / irn.Interior.Color = RGB(255, 255, 0)End IfNext
During every iterate of the loop b recalculating to have last empty row number. Here we can't use rn.Rows.Count because rn is not a continuous range. Some rows are hidden according to the filter and therfore rn.Rows.Count give the count of first continuous rows which is not correct. To overcome this error this code calculate all cells in the range and divide it by number of columns. (Cells Count = Columns Count * Rows Count)
Step Four
Filter the Data List sheet to show all rows which are not having any color and copy those data to the sheet List Without Keywords. These are the subset of Data List sheet data which don't having any keyword.
dl.Range("A1", Cells(a, i)).AutoFilter Field:=l, Operator:=xlFilterNoFillSet rn = NothingOn Error Resume NextSet rn = dl.Range("A2", Cells(a, i)).SpecialCells(xlCellTypeVisible)On Error GoTo 0If Not rn Is Nothing Thenrn.Copy Destination:=wo.Range("A2")End Ifdl.AutoFilterMode = Falsedl.Cells.Interior.Pattern = xlNone
After all filter in the sheet Data List is removed using AutoFilterMode=False and all colors add to the cells set to none.
Step Five
If one row in Data List sheet included more than one keyword, macro copied it more than one to the List With Keywords sheet. Step Five is to remove those duplicates. If there is more than one row to consider .RemoveDuplicates method need to have all column numbers as a array. First four lines of this part is to build that array.
Step SixReDim st(0 To i - 1)For a = 0 To UBound(st)st(a) = a + 1Nextwk.Activatewk.Range("A2", Cells(a, i)).RemoveDuplicates Columns:=(st), Header:=xlNo
Finally code will display the Dashboard sheet and enable Events and screen updates.
da.ActivateMsgBox "Filter Complete", vbInformationApplication.ScreenUpdating = TrueApplication.EnableEvents = True
During this part code notify the user that process is over. If you turned off events and screen updating during any macro, make sure to turned on within the macro, Otherwise they are turned of until you close all opened excel files and open excel.
You can download the sample excel sheet with the macro using below link