Find negative numbers In column and delete the row with VBA I need find a negati
ID: 3571117 • Letter: F
Question
Find negative numbers In column and delete the row with VBA
I need find a negative numbers in column T and delete the row that contain this numbers.
I create this code but not work.
Sub delete()
Dim r As Long
Dim m As Long
Set ws = Worksheets("Sheet1")
m = ws.Range("T" & ws.Rows.Count).End(xlUp).Row
For r = 2 To m
If ws.Range("T" & r).Value < 0 Then
ws.Range("T" & r).Rows.delete
End If
Next r
End Sub
Thanks for help !!
Explanation / Answer
Hi
As an alternative approach try:
'=========>>
Option Explicit
'--------->>
Public Sub DeleteRange()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range, rCell As Range, delRng As Range
Dim LRow As Long
Dim CalcMode As Long
Set WB = ThisWorkbook
Set SH = WB.Sheets("Sheet1")
With SH
LRow = .Cells(.Rows.Count, "T").End(xlUp).Row
Set Rng = .Range("T2:T" & LRow)
End With
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
For Each rCell In Rng.Cells
With rCell
If IsNumeric(.Value) And .Value < 0 Then
If delRng Is Nothing Then
Set delRng = rCell
Else
Set delRng = Union(rCell, delRng)
End If
End If
End With
Next rCell
If Not delRng Is Nothing Then
delRng.EntireRow.Delete
End If
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.