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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,732
Members
448,987
Latest member
marion_davis

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