Find text and insert nth blank rows above until text is on row 20

Andy15

Board Regular
Joined
Apr 1, 2017
Messages
56
Hi Guys,

I am looking for a bit of help to move some data down to a specific row across multiple worksheets.

I have the text "Name" in column B, the text "Name" could be on any row from 1 to 15. There will only be 1 occurrence of the text per worksheet.

I would like to insert blank rows above the text "Name" as many rows as necessary to make the text "Name" be on row 20

I would then like to move on to the next worksheet until all worksheets have been done.

Many thanks for any help
Andy15
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Andy15,

You might consider the following

Code:
Sub InsertRows()
Dim ws As Worksheet
Dim found As Long

For Each ws In ThisWorkbook.Worksheets
    found = ws.Columns(2).Find(What:="Name", After:=ws.Cells(1, 2), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
    ws.Rows(found & ":" & found + (19 - found)).Insert
Next ws
MsgBox "Done!"
End Sub

Cheers,

tonyyy
 
Upvote 0
Hi tonyyy,

Thanks for the code. It works as requested which is great, however my mistake but I forgot to add that new worksheets are added regularly and I need to run the code every week. What is happening is when the macro is run again the sheets that have already been adjusted are moved down by another 2 rows. Is there a way to say if the text "Name is on row 20 do nothing.

Cheers
Andy15
 
Upvote 0
Code:
Sub InsertRows()
Dim ws As Worksheet
Dim found As Long

For Each ws In ThisWorkbook.Worksheets
    found = ws.Columns(2).Find(What:="Name", After:=ws.Cells(1, 2), LookIn:=xlFormulas, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
    [COLOR=#ff0000]If found < 20 Then [/COLOR]ws.Rows(found & ":" & found + (19 - found)).Insert
Next ws
MsgBox "Done!"
End Sub
 
Upvote 0
You're welcome, Andy15. Glad it worked out...
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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