how to find an entry and delete it leaving the first intact

caprichoo

Board Regular
Joined
Nov 2, 2005
Messages
53
Hi, just a quick one

I have imported several text files into one worksheet. all the text files has a heading .. how do i create a macro so that it will find the second occurance of heading and then delete 4 rows with it?

the heading are all the same so i am thinking using a find function and search for the heading words and then delete all the subsequent occurance. i only have the idea but not sure how to implement it ... anyone can help ...Thanks a million
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,526
Hi,

This code deletes all subsequent rows (plus the following 3) from Sheet1 using A1 as the match string:
Code:
Sub DelHeads()
Dim rData As Range
Dim vRow As Variant, vHeadText As Variant
Dim WS As Worksheet

Set WS = Sheets("Sheet1")
vHeadText = WS.Range("A1").Text

Set rData = WS.Range("A5:A" & Rows.Count)
On Error Resume Next
vRow = WorksheetFunction.Match(vHeadText, rData, 0)
Do While IsNumeric(vRow)
    WS.Rows(vRow + 4 & ":" & vRow + 7).Delete shift:=xlUp
    Set rData = WS.Range("A5:A" & Rows.Count)
    vRow = "*"
    vRow = WorksheetFunction.Match(vHeadText, rData, 0)
Loop
End Sub
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,168,120
Messages
5,857,492
Members
431,882
Latest member
saaaaaaaaaaaaaaaaaaaaaa

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
Top