I have a macro-enabled sheet which has three worksheets. I want to delete the entire row if the value in column F is "T" only from sheet1. Here's the code I tried:
Sub RemoveRows()
Dim i As Long
Set ws = Sheets("Sheet1")
lRow = ws.Range("F" & Rows.Count).End(xlUp).Row
With ws
For i = 2 To lRow
If InStr(.Range("F" & i), "T") > 0 Then
ThisWorkbook.ActiveSheet.Range("F" & i).EntireRow.Delete
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
It deleted the rows, but not completely. Still, I can see the value "T" in column F. Can anyone please point out me with the wrong in the code?
It will miss some of the rows that are immediately below other rows that were deleted. This is why you should loop backwards like this:
Sub RemoveRows()
Dim i As Long
Set ws = Sheets("Sheet1")
lRow = ws.Range("F" & Rows.Count).End(xlUp).Row
With ws
For i = lRow To 2 Step -1
If InStr(.Range("F" & i), "T") > 0 Then
ws.Range("F" & i).EntireRow.Delete
End If
Next i
End With
Application.ScreenUpdating = True
End Sub
This worked for me for one sheet as is just a minor tweak on what's already out there.
Option Explicit ' Make your intentions known.
' Option Compare Binary ' Introduce case sensitivity (Optional)
Public Sub RemoveRows()
' Activate our worksheet
ThisWorkbook.Worksheets("Sheet1").Activate
Dim i as Long
Dim my_rng as Range
Set my_rng = Range("F2")
' We'll start at row 2 and go down to as many filled rows are in the column
For i = my_rng.Row to Cells(Rows.Count, my_rng.Column).End(xlUp).Row
With Range("F" & i)
' vbBinaryCompare to consider "T" vs "t"
If InStr(1, .Value, "T", vbBinaryCompare) > 0 Then
.EntireRow.Delete
End If
End with
Next i
' Clean up objects
Set my_rng = Nothing
End Sub
It's always tricky when you combine a loop with a delete row . In a lot of cases you can achieve the same end by the use of a filter – and deleting all the rows en masse . The first code deletes all rows on a single sheet (Sheet1) where it finds a T
in column F
. The second deletes rows on the first 3 sheets.
Option 1 - first sheet only
Sub RemoveRows1sheet()
With Sheet1.Cells(1, 1).CurrentRegion
.AutoFilter 6, "*T*" '<~~ Filter for any instance of "T" in column F (6)
.Offset(1).EntireRow.Delete '<~~ exclude headers & delete rows
.AutoFilter '<~~ turn off AutoFilter
End With
End Sub
Option 2 - delete rows on 3 sheets based on Sheet1 values
Option Explicit
Sub RemoveRows3sheets()
Dim LastRow As Long, i As Long '<~~ declare variables
LastRow = Cells(Rows.Count, 6).End(xlUp).Row '<~~ set the last row
With Sheet1.Cells(1, 1).CurrentRegion '<~~ apply the filter to sheet 1
.AutoFilter 6, "*T*"
End With
For i = LastRow To 2 Step -1
If Sheet1.Rows(i).Hidden Then
GoTo skip
Else
Sheet1.Cells(i, 1).EntireRow.Delete '<~~ delete this row number from each sheet
Sheet2.Cells(i, 1).EntireRow.Delete
Sheet3.Cells(i, 1).EntireRow.Delete
End If
skip:
Next i
Sheet1.AutoFilterMode = False '<~~ turn off the AutoFilter
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.