Originally posted here:
I have a macro that renames certain headers, deletes certain columns and inserts a row on specific sheets.
I need to apply a filter across the sheets IF the header is found on the sheet.
Here is the complete code for the macro:
Originally when I tested the "filter" code, it worked great - from what I can remember. But now, I have the same issue I was having on my other post, which is that the code gets stuck in an infinite loop and I can only exit it by "Ctrl + Break".
When I "break" it and click on debug, then it takes me to the End If of the If UBound statement.
I then proceed to check the sheets to see if the filter was applied, and it was only applied to the first sheet that had "Status" as a header. All the other sheets with "Status" it was not applied to.
I also have no clue if the second part works - to delete the filtered rows, which comes immediately after the filter. I do not know if it works because the code never reaches that line.
I have a macro that renames certain headers, deletes certain columns and inserts a row on specific sheets.
I need to apply a filter across the sheets IF the header is found on the sheet.
Here is the complete code for the macro:
Code:
Sub ManipulateSheets()
Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
Dim a As Long, w As Long
Dim keepCols()
Dim filterCols As Variant
Dim wkbk1 As Workbook
Set wkbk1 = Workbooks("testWorkbook.xlsm")
'Set sheets to be used in each workbook
Set ws2 = wkbk1.Sheets("thisSheet")
Set ws3 = wkbk1.Sheets("thatSheet")
Set ws4 = wkbk1.Sheets("mySheet")
keepCols = Array("Employee Number", "Status")
filterCols = Array("Status")
wkbk1.Activate
ws2.Range("A1").EntireRow.Insert
ws2.Range("A1").Value = "Employee Number"
ws3.Range("A1").EntireRow.Insert
ws3.Range("A1").Value = "Employee Number"
ws4.Range("A1").EntireRow.Insert
ws4.Range("A1").Value = "Employee Number"
For Each ws1 In wkbk1.Sheets
ws1.Cells(1, 1).EntireRow.Replace What:="USERID", Replacement:="Employee Number", LookAt:=xlWhole
ws1.Cells(1, 1).EntireRow.Replace What:="STATUS", Replacement:="Status", LookAt:=xlWhole
ws1.Cells(1, 1).EntireRow.Replace What:="USER_ID", Replacement:="Employee Number", LookAt:=xlWhole
ws1.Cells(1, 1).EntireRow.Replace What:="USER-ID", Replacement:="Employee Number", LookAt:=xlWhole
ws1.Cells(1, 1).EntireRow.Replace What:="USER_STATUS", Replacement:="Status", LookAt:=xlWhole
ws1.Cells(1, 1).EntireRow.Replace What:="HR_STATUS", Replacement:="Status", LookAt:=xlWhole
Next ws1
Call DeleteIrrelevantColumns
With wkbk1
For w = 1 To .Worksheets.count
With Worksheets(w)
'Search for the Word "Status" on the first row of the Sheet
Set StatusFound = Worksheets(w).Rows(1).Find(What:="Status", LookAt:=xlWhole)
'If Status is found then apply filter
If Not StatusFound Is Nothing Then
For a = .Columns.count To 1 Step -1
If UBound(filter(filterCols, Worksheets(w).Cells(1, a), True, vbTextCompare)) < 0 Or IsEmpty(.Cells(1, a)) Then _
Worksheets(w).UsedRange.AutoFilter Field:=StatusFound.Column, Criteria1:=("INACTIVE"), Operator:=xlFilterValues
End If
Next a
End If
LstRw = .Cells(.Rows.count, "A").End(xlUp).Row
Set rng = .Range("A2:A" & LstRw).SpecialCells(xlCellTypeVisible)
'Delete visible cells
rng.EntireRow.Delete
'Remove filter
.AutoFilterMode = False
End With
Next w
End With
End Sub
Originally when I tested the "filter" code, it worked great - from what I can remember. But now, I have the same issue I was having on my other post, which is that the code gets stuck in an infinite loop and I can only exit it by "Ctrl + Break".
When I "break" it and click on debug, then it takes me to the End If of the If UBound statement.
I then proceed to check the sheets to see if the filter was applied, and it was only applied to the first sheet that had "Status" as a header. All the other sheets with "Status" it was not applied to.
I also have no clue if the second part works - to delete the filtered rows, which comes immediately after the filter. I do not know if it works because the code never reaches that line.