PDA

View Full Version : ERROR in code



pdeshazier
05-08-2006, 02:24 PM
I have the following code which is erroring out on the lastRow= statement. I just need to loop through rows looking for "TL" in column K and if not "TL", delete the whole row. Thanks.

Sub DELETE_ROWS()
Dim start As Long
Dim lastRow As Long
Dim i As Long
Dim sTL As String:dunno

lastRow = Cells(Rows.Count, "F").End(x1Up).Row
For i = 1 To lastRow
If Left(sTL, 2) <> "TL" Then
Rows.Delete
End If
Next i
End Sub

Jacob Hilderbrand
05-08-2006, 02:29 PM
It should be xlUp not x1Up.

lucas
05-08-2006, 03:33 PM
Try this from the forum:
comment or delete the line Option compare text if you just want to look for capital letters.

Option Explicit
Option Compare Text
Sub DeleteTL()
Dim test As Boolean, x As Long, lastrow As Long, col As Long
Range("K1").Select
col = ActiveCell.Column
lastrow = Cells(65536, col).End(xlUp).Row
For x = lastrow To 1 Step -1
test = Cells(x, col).Text <> "TL"
If test = True Then Cells(x, col).EntireRow.Delete
Next
End Sub

pdeshazier
05-09-2006, 05:56 AM
thanks jake

pdeshazier
05-09-2006, 12:12 PM
I changed the code from x1 to xl, but when i run it, it's deleting ALL my rows regardless of whether TL is found or not. I've attached the file.:banghead:

stanl
05-09-2006, 12:35 PM
I changed the code from x1 to xl, but when i run it, it's deleting ALL my rows regardless of whether TL is found or not. I've attached the file.:banghead:

You indicated you wanted to delete based on Column K, but your code looks at Column F - is that intentional? Stan

pdeshazier
05-09-2006, 12:45 PM
No, sorry. I want to delete the current row if column F contains "TL". does rows.delete only delete the current row?

Norie
05-09-2006, 01:10 PM
This piece of code deletes all rows.

Rows.Delete
Try this.

Sub DELETE_ROWS()
Dim start As Long
Dim lastRow As Long
Dim i As Long
Dim sTL As Range

lastRow = Cells(Rows.Count, "F").End(xlUp).Row
For i = lastRow To 1 Step -1
Set sTL = Range("F" & i)
If Left(sTL.Value, 2) <> "TL" Then
sTL.EntireRow.Delete
End If
Next i
End Sub

lucas
05-10-2006, 09:14 AM
In post 1 you say:
I just need to loop through rows looking for "TL" in column K and if not "TL", delete the whole row. Thanks.


In post 7 you say:


I want to delete the current row if column F contains "TL". does rows.delete only delete the current row?


Can you clarify because there have been several posts here that address post 1 but now you have seemed to change what you want to do?? Also would help to know what you mean by current row. Do you mean you have selected the entire row...or a cell in that row...??