Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to Delete rows above the find - VBA Assistance Requested

Status
Not open for further replies.

Randy11

Technical User
Oct 4, 2002
175
CA
Code below finds "Data Start" in column 3 & deletes the row found in & all those above this point. Need to leave the found line & delete those above this point only. Must be getting late... Assistance appreciated.

Sub Delete_Stuff_Above_Line()
'

Dim rng As Range
Sheets("Import_Data").Select
Set rng = Columns(3).Find("Data Start", Cells(3, 3))
If Not rng Is Nothing Then
Range(Cells(1, 1), rng).EntireRow.Delete
End If
End Sub
 
This will work. You might need to put some error handling back in.

Sub Delete_Stuff_Above_Line()
'

'Dim rng As Range
Dim MyRow As Integer
Sheets("Import_Data").Select
MyRow = Columns(3).Find(What:="Data Start", After:=Cells(3, 3)).Row - 1
Rows("1:" & MyRow).Delete Shift:=xlUp
'If Not rng Is Nothing Then
'Range(Cells(1, 1), rng).EntireRow.Delete
'End If
End Sub
 
Code:
Sub Delete_Stuff_Above_Line()
'

Dim rng As Range
with Sheets("Import_Data")
    Set rng = .Columns(3).Find("Data Start", Cells(3, 3))
    If Not rng Is Nothing Then[b]
       Range(.Cells(1, 1), rng.offset(-1)).EntireRow.Delete[/b]
    End If
End with
End Sub

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Thanks Both for the assistance. Skip, just removed the . before Cells & it worked like a charm.
 

What you did ONLY if that sheet is ACTIVE.

I almost NEVER use Activate and Select Methods!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Excellent point Skip, this is more efficient.... Adopted this approach, will try in other scenarios as well. Will particularly play close attention to code that either runs slow or poorly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top