Macro filtering, remove name99, keep name1 and all data in your row, not working macro
Hello people,
got this macro for filtering but it is not working as it should.
I have a excel list with 5 columns. In A we have file names. All of them contain jpg, png, gif, pdn, sometimes filename jpg pdn, or filename jpg gif pdn. In column 2,3,4... I have additional information.
I want to remove ALL pda and gif. BONUS: It would be really nice, if I could keep all files which pdn. E.g. delete all rows which contain pdn but if it has jpg, keep it. this is only bonus and has no priority.
Here is the macro which is not working, because it only keeping column A, also the bonus mention above is not a option.
PHP Code:
Option Explicit
Public Sub FilterColumn() Dim InputWS As Worksheet: Set InputWS = ActiveSheet Worksheets.Add After:=Worksheets(Worksheets.Count) Dim OutputWS As Worksheet: Set OutputWS = Worksheets(Worksheets.Count) Dim SearchTerms As Variant: SearchTerms = Array("jpg", "png") Dim ExcludeTerms As Variant: ExcludeTerms = Array("gif", "pdn") Dim Counter As Long, RowCounter As Long, TermFound As Boolean Call LudicrousMode(True) For RowCounter = 1 To GetLastRow(InputWS, 1) For Counter = 0 To UBound(SearchTerms) If InStr(1, InputWS.Cells(RowCounter, 1).Value, SearchTerms(Counter)) > 0 Then TermFound = True Next Counter For Counter = 0 To UBound(ExcludeTerms) If InStr(1, InputWS.Cells(RowCounter, 1).Value, ExcludeTerms(Counter)) > 0 Then TermFound = False Next Counter If TermFound = True Then For Counter = 1 To 4 OutputWS.Cells(GetLastRow(OutputWS, Counter) + 1, 1).Value = InputWS.Cells(RowCounter, Counter).Value Next Counter TermFound = False End If Next RowCounter Call LudicrousMode(False) Set OutputWS = Nothing Set InputWS = Nothing End Sub
Public Sub LudicrousMode(ByVal Toggle As Boolean) Application.ScreenUpdating = Not Toggle Application.EnableEvents = Not Toggle Application.DisplayAlerts = Not Toggle Application.Calculation = IIf(Toggle, xlCalculationManual, xlCalculationAutomatic) End Sub
Public Function GetLastRow(ByVal TargetWorksheet As Worksheet, ByVal ColumnNo As Long) As Long GetLastRow = TargetWorksheet.Cells(TargetWorksheet.Rows.Count, Chr(64 + ColumnNo)).End(xlUp).Row End Function
Would be awesome if you could help me out
Best wishes,
Gusop
MatrixMan. --------------------------------------
If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.
Re: Macro filtering, remove name99, keep name1 and all data in your row, not working macro
Yep - that will work - thanks jindon. @Gusop - the invalid use of me is because I suspect you put the code in a module or the workbook object and not the sheet object.
Bookmarks