Private Sub subMain()
Call subDeleteSheets
Call subCopyRowsToNewSheet("AAA", "")
Call subCopyRowsToNewSheet("RRR", "")
Call subDeleteRowsBasedUponValue(Worksheets(1), Worksheets(1).Cells(1, 13).Value, "FFF,GGG", True)
MsgBox "Rows deleted and moved.", vbOKOnly, "Confirmation"
End Sub
Private Sub subDeleteSheets()
Dim i As Integer
Dim Ws As Worksheet
Worksheets(1).Activate
Application.DisplayAlerts = False
For Each Ws In ActiveWorkbook.Worksheets
If Ws.Index <> 1 Then
Ws.Delete
End If
Next Ws
Application.DisplayAlerts = True
End Sub
Public Sub subCopyRowsToNewSheet(strCriteria As String, strSheetName As String)
Application.ScreenUpdating = False
Worksheets.Add after:=Worksheets(Worksheets.Count)
If strSheetName <> "" Then
ActiveSheet.Name = strSheetName
End If
With Worksheets(1)
.AutoFilterMode = False
With .Range("A1").CurrentRegion
.AutoFilter 13, strCriteria
On Error Resume Next
.EntireRow.Copy ActiveSheet.Range("A1")
On Error GoTo 0
End With
.AutoFilterMode = False
End With
With Worksheets(Worksheets.Count)
.Cells.EntireColumn.AutoFit
End With
Application.ScreenUpdating = True
End Sub
Public Sub subDeleteRowsBasedUponValue(Ws As Worksheet, strColumn As String, strValues As String, blnKeep As Boolean)
Dim lngRow As Long
Dim lngRows As Long
Dim rngfound As Range
With Ws
Set rngfound = Ws.Rows(1).Find(strColumn, LookIn:=xlValues)
If rngfound Is Nothing Then
Exit Sub
End If
lngRows = .Range("A" & Rows.Count).End(xlUp).Row
For lngRow = lngRows To 2 Step -1
' Delete if not in list and just keep list items.
If blnKeep Then
If (InStr(1, strValues, .Cells(lngRow, rngfound.Column), vbTextCompare) = 0) Then
.Cells(lngRow, 1).EntireRow.Delete
End If
End If
' Delete if in list and not keep list items.
If Not blnKeep Then
If (InStr(1, strValues, .Cells(lngRow, rngfound.Column), vbTextCompare) > 0) Then
.Cells(lngRow, 1).EntireRow.Delete
End If
End If
Next lngRow
End With
End Sub