VBA: Delete contents of a sheet after appearance of a string

Drueckeberger

New Member
Joined
Jan 24, 2019
Messages
2
Hi,

I am new to this forum and need your professional help. Thank you in advance :)

Here is my problem:

I have a sheet, lets call it "Statement". In this sheet I have to copy different data sets that always have the same structure. However the amount of rows varies massively. Sometimes the statement data can be 10000 lines, sometimes only 5. Also there are two versions of data sets. So two different structures.

What I want to achieve is that when I run the marco, that all content or all rows after the appearance of a specific string are deleted.
My problem is, that sometimes that string A and String B can either be in A10000 or A5 or A823 (but always column A).

So basically:
  • when running the macro
  • find string A or string b in column A
  • then delete all content/rows below

(string A and B can simply be defined in the module, as they are always the same)

I think this is easy for you, unfortunately not for me :(

Thanks for you help.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Code:
Sub DeleteRows()

With Workbooks("REFERENCE").Sheets("REFERENCE")

LRow = .Cells(.Rows.Count, "A").End(xlUp).Row

StrA = "example1"
StrB = "example2"
Set FindStr = .Range("A:A").Find(StrA)
If FindStr is Nothing Then Set FindStr = .Range("A:A").Find(StrB)
If not FindStr Is Nothing Then 
If FindStr <> LRow Then 
.Rows(FindStr.Row & ":" & LRow).EntireRow.Delete
End If
End If

End with
End Sub

Alternatively, if you want to find the exact strings in the cells.. (instead of searching for e.g. "A" and deleting all rows from the first cell that contains "A")
Code:
Sub DeleteRows()

With Workbooks("REFERENCE").Sheets("REFERENCE")

LRow = .Cells(.Rows.Count, "A").End(xlUp).Row

StrA = "example1"
StrB = "example2"
Set FindStr = .Range("A:A").Find(StrA, lookat:=xlwhole, matchcase=True)
If FindStr is Nothing Then Set FindStr = .Range("A:A").Find(StrB, lookat:=xlwhole, matchcase=True)
If not FindStr Is Nothing Then 
If FindStr <> LRow Then
.Rows(FindStr.Row& ":" & LRow).EntireRow.Delete
End If
End If

End with
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,202,987
Messages
6,052,932
Members
444,616
Latest member
novit19089

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