Delete Filtered Rows - excel

Overview:
I have 2 macros, one filters the data and the second deletes the visible rows below the header rows (header rows 1-12).
2 Questions:
1) How do I best combine these into a single macro?
2) How do I get the second one to work properly?
I receive the
Run Time Error 1004: Method 'Range' of object '_worksheet' failed
on the Delete() macro line:
Set rng = .Range("A12:A" & LastRow).SpecialCells(xlCellTypeVisible)
I have also tried:
Set rng = .Range("A12:A" & LastRow).SpecialCells(xlCellTypeVisible).cells
Sub Filter()
'filter and delete rows that have AW as FALSE
For Each sht In ThisWorkbook.Worksheets
sht.Range("A12:AW12").autofilter Field:=49, Criteria1:="FALSE"
Next sht
End Sub
Sub Delete()
Dim sht As Worksheet, rng As Range, lastRow As Long
Set sht = Worksheets("Sheet1")
With sht
lastRow = Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row
Set rng = .Range("A12:A" & LastRow).SpecialCells(xlCellTypeVisible)
rng.EntireRow.Delete
.AutoFilterMode = False
End With
End Sub

Sub Filter()
'filter and delete rows that have AW as FALSE
Dim rng As Range
For Each sht In ThisWorkbook.Worksheets
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
sht.Range("A1:O1").AutoFilter Field:=9, Criteria1:="FALSE"
sht.Range("A2:A" & LastRow).Delete
If sht.AutoFilterMode Then
sht.AutoFilterMode = False
End If
Next sht
End Sub

This is a working code for anyone that might find this going forward.
Sub Filter23()
'filter and delete rows that have AW as FALSE (working)
Dim sht As Worksheet, rng As Range, lastRow As Long
For Each sht In ThisWorkbook.Worksheets
sht.Range("A12:AW12").autofilter Field:=49, Criteria1:="FALSE"
With sht
lastRow = .Cells(Rows.Count, "A").End(xlUp).Row
Set rng = sht.Range("A12:A" & lastRow).SpecialCells(xlCellTypeVisible)
rng.EntireRow.Delete
End With
Next sht
End Sub

Related

VBA Insert row instead of copy

I'm trying to run before I can crawl. I have pieced together this code, but I need it to Insert at row 24, not copy.
Dim sh4 As Worksheet, sh5 As Worksheet, lr As Long, rng As Range
Set sh4 = Sheets("est")
Set sh5 = Sheets("gaf letter")
lr = sh4.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh4.Range("a1:a" & lr)
rng.EntireRow.Copy sh5.Rows("24:24")
I've attempted using .Insert, but it comes up with Method Insert of object Range Failed. The code works fine if I wanted to just copy, but I need it to insert and shift the remaining rows below it, down.
Option Explicit ' declare all variables
Sub InsertRows()
Dim sh4 As Worksheet, sh5 As Worksheet
Dim lr As Long, rng As Range
Set sh4 = Sheets("est")
Set sh5 = Sheets("gaf letter")
Application.ScreenUpdating = False
With sh4
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Rows("1:" & lr)
rng.Copy
sh5.Rows(24).Insert shift:=xlDown
End With
Application.ScreenUpdating = True
Application.CutCopyMode = False
End Sub
just go
With Sheets("est")
.Range("A1", .Cells(.rows.Count, 1).End(xlUp)).EntireRow.Copy
Sheets("gaf letter").rows(24).Insert shift:=xlDown
Application.CutCopyMode = False
End With

Im trying to copy the hole row of a cell and paste it on last row of another column, but I keep getting an error

Im getting error 1004 vba application defined
here goes my code:
Sub Example()
Dim finalRowF As Integer
finalRowF = Cells(Rows.Count, "F").End(xlUp).Row
Range("A1").EntireRow.Copy Cells(finalRowF + 1, "H")
End Sub
Can someone take a look?
How about:
Sub Example()
Dim ws As Worksheet
Dim FinalRowF As Long
Set ws = Sheets("Sheet1") 'Change as necessary
FinalRowF = ws.Range("F" & ws.Rows.Count).End(xlUp).Row
Set CopyRange = ws.Range("A2:A" & FinalRowF)
CopyRange.SpecialCells(xlCellTypeVisible).Copy
ws.Range("H2").PasteSpecial xlPasteValues
Application.CutCopyMode = False
End Sub

Filter multiple columns in table

My code filters one column then prints.
I need to filter based on two columns and then print. I.e. filter based on engineer name (column 1) and route (column 2). Right now, it filters on engineer name (column 1).
Option Explicit
Sub filterandprint()
Dim TempWks As Worksheet
Dim wks As Worksheet
Dim myRng As Range
Dim myCell As Range
'change to match your worksheet name
Set wks = Worksheets("Table")
Set TempWks = Worksheets.Add 'creates temporary worksheet
wks.AutoFilterMode = False 'remove the arrows
'assumes headers only in row 1, columns(1) will be the number of the column you base your filtering
'this copies the unique filtering and pastes it on a new temp worksheet
wks.Columns(1).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=TempWks.Range("A1"), Unique:=True
With TempWks
Set myRng = .Range("a2", .Cells(.Rows.Count, "A").End(xlUp))
End With
'looping
With wks
For Each myCell In myRng.Cells
.UsedRange.AutoFilter Field:=1, Criteria1:=myCell.Value
'.UsedRange.AutoFilter Field:=2, Criteria1:=myCell.Value
.PrintOut Preview:=True
Next myCell
End With
Application.DisplayAlerts = False
TempWks.Delete 'deletes temporary worksheet
Application.DisplayAlerts = True
End Sub
For anyone else searching for an answer, edited the above looping section to the below and it worked:
...
Dim iLoop As Integer
'looping
With wks
For iLoop = 2 To 65
.UsedRange.AutoFilter Field:=1, Criteria1:=TempWks.Cells(iLoop, 1).Value
.UsedRange.AutoFilter Field:=2, Criteria1:=TempWks.Cells(iLoop, 2).Value
.PrintOut Preview:=True
Next iLoop
End With
Application.DisplayAlerts = False
TempWks.Delete 'deletes temporary worksheet
Application.DisplayAlerts = True
End Sub

Autofilter with out selecting sheet

I want to use autofilter to delete some rows without selecting the sheet
If the sheet is selected the code runs
If the sheet is not selected the code fails I get the error object required and Set ws = ThisWorkbook.Sheets("LI_Data_Prepped") is highlighted`
the code fails even if I use Select when seting ws
I can not figure out why this is throwing the error possibly because I am using a array for the criteria?
Thanks
Edit: 10-29-14
Sub DeleteSomeNamesIn_LI_Data_Prepped()
Dim ws As Excel.Worksheet
Dim lCol As Long
Dim lRow As Long
Dim cNumber As Integer
'Does NOT work like this
Set ws = ThisWorkbook.Sheets("LI_Data_Prepped").Select
'Does NOT work like this either
Set ws = ThisWorkbook.Sheets("LI_Data_Prepped")
With ws
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.AutoFilterMode = False
.Range("A1", Cells(lRow, lCol)).Cells.AutoFilter
.Range("A1", Cells(lRow, lCol)).Cells.AutoFilter Field:=2, Criteria1:=Array("Bob", "Carol", "Ted", "Alis", "="), Operator:=xlFilterValues
'If no rows to delete will error
On Error Resume Next
.UsedRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilterMode = False
.Range("A1").End(xlDown).Offset(1).Resize(ActiveSheet.UsedRange.Rows.Count).EntireRow.Delete
On Error GoTo 0
End With
End Sub
To avoid pre-selecting the worksheet:
Use Set ws = Worksheets("LI_Data_Prepped") instead of
Set ws = ThisWorkbook.Sheets("LI_Data_Prepped").Select
You can also use Dim ws As Worksheet instead of Dim ws As Excel.Worksheet

Error deleting discontinuous entire rows

I have the following code which looks down column M to find empty cells. Each time an empty cell is found, then the entire row is deleted.
The code always leaves one row undeleted.
Can someone help to identify what is wrong with the code? I suspect that in deleting a row, the cell count goes wrong.
Private Sub CreateInvoice_Click()
Dim LastRow As Long
Dim cl As Range, rng As Range
With Sheet4
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng = Sheet4.Range("M1:M" & LastRow)
For Each cl In rng
If IsEmpty(cl) Then
cl.EntireRow.Select ' MsgBox .Range("A" & cl.Row).Value & " has nothing in it"
End If
Next
End With
End Sub
I would use a filter as it's the fastest and most efficient way of deleting empty rows. Also the below doesn't use .Select method.
Sub RemoveEmpties()
Dim ws As Worksheet
Dim rng As Range
Dim lastRow As Long
Set ws = Sheet4
lastRow = ws.Range("M" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("M1:M" & lastRow)
With rng
.AutoFilter Field:=1, Criteria1:=""
.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete
End With
ws.AutoFilterMode = False
End Sub
Columns("M:M").Select
Selection.SpecialCells(xlCellTypeBlanks).Select
Selection.EntireRow.Delete
Try using this

Resources