Deleting rows not containing value

blondie7228

New Member
Joined
Feb 23, 2012
Messages
3
I am trying to delete all rows until the macro reaches the row that contains "Loan Detail" in column A. I am using the following code, it seems to find the row but deletes it. I want it to delete UNTIL reaching that row.

Any input?


Sub DeleteRow()
Dim F
Dim MyValue
MyValue = "Loan Detail"
With Cells Set F = .Find(What:=MyValue, After:=.Cells(1, 1), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
If ((Not F Is Nothing) And (F.Row <> 1)) Then
Range(Range("A1"), Cells(F.Row - 1, 1)).EntireRow.Delete
If (F.Column <> 1) Then
Range(Range("A1"), Cells(1, F.Column - 1)).EntireColumn.Delete
End If
End If
End With
ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
 
Last edited:

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
this works for me
Code:
Sub DeleteRow()
Dim F
Dim MyValue
MyValue = "Loan Detail"
With Cells
Set F = .Find(What:=MyValue, After:=.Cells(1, 1), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
If ((Not F Is Nothing) And (F.Row <> 1)) Then
Range(Range("A1"), Cells(F.Row - 1, 1)).EntireRow.Delete
End If
End With
End Sub
 
Upvote 0
Thanks, I deleted the "ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks).EntireRow.Delete" and it ran great.

One more question, can I use multiple values for "My Value"? Ideally, I would like to use an or statement, something like My Value= "Property Value" OR "Property Financials" OR "Loan Detail" but I am not sure the proper way to code that.
 
Upvote 0
Blondie
I can see your list getting bigger as your work progresses !
So, you could use an Input Box to decide which text you want to Find
Code:
Sub DeleteRow()
Dim F, MV1
MV1 = InputBox("What Text Do you want to Find ?")
If MV1 = False Then Exit Sub
With Cells
Set F = .Find(What:=MV1, After:=.Cells(1, 1), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
If ((Not F Is Nothing) And (F.Row <> 1)) Then
Range(Range("A1"), Cells(F.Row - 1, 1)).EntireRow.Delete
End If
End With
End Sub
 
Upvote 0
OR
You make a list of "names to be found", and use almost the same code to pick the value from the list
Code:
Sub DeleteRow()
Dim F, MV1
MV1 = Application.InputBox("What Text Do you want to Find ?", Type:=8)
If MV1 = False Then Exit Sub
With Cells
Set F = .Find(What:=MV1, After:=.Cells(1, 1), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False)
If ((Not F Is Nothing) And (F.Row <> 1)) Then
Range(Range("A1"), Cells(F.Row - 1, 1)).EntireRow.Delete
End If
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,893
Messages
6,122,118
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top