Searching for headings and inserting rows

ksak

New Member
Joined
Jun 23, 2011
Messages
12
This is my code:

Sub Insertrow()
Dim ADF As Variant
ADF = Range("D2")

Selection.Find(What:=ADF, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate _
.EntireRow.Insert shift:=xlDown

End Sub

I highlight the entire column B (Selection) because that's where i want the macro to do the searching. It looks for what I type in cell D2 which is my variant. the code works up until this point. What i need help with is having the code insert an entire row underneath the row with the heading i specified in D2.

:confused: Please help!
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
try:
Code:
Sub insertRow2()
    Dim ADF As Variant
    ADF = Range("D2").Value
    Cells.Find(what:=ADF, After:=ActiveCell, LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).EntireRow.Insert shift:=xlDown
End Sub

I'm thinking though that if the activecell is before D2 in the sheet, then all this code is going to do is find D2 then insert a row below this cell.
 
Upvote 0
Thanks! what you mentioned won't be an issue because D2 is at the top of the sheet with all of the data below it.

Minor tweak though if possible. Right now it inserts rows above the heading it finds. anyway to insert rows below? i already tried "Insert shift:=xlUp"
 
Upvote 0
Thanks! what you mentioned won't be an issue because D2 is at the top of the sheet with all of the data below it.

Minor tweak though if possible. Right now it inserts rows above the heading it finds. anyway to insert rows below? i already tried "Insert shift:=xlUp"
I think if you move the reference down a line by using offset

Code:
Cells.Find(what:=ADF, After:=ActiveCell, LookIn:=xlValues, _
 LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
 MatchCase:=False, SearchFormat:=False)[B][COLOR=red].offset(1,0)[/COLOR][/B].EntireRow.Insert shift:=xlDown
 
Upvote 0
The above definitely worked thank you! So this is what I have so far:

Sub Insertrow()
Dim ADF As Variant

ADF = Range("D2")

Range("B:B").Select
Selection.Find(What:=ADF, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).EntireRow.Insert shift:=xlDown

Range("D1").Copy
Selection.Find(What:=ADF, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Offset(1, 0).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

End Sub

It basically searches for a word, inserts a row beneath that word. however i have blocks of similar information repeating itself and i would like this macro to loop until it stops finding the first word. can someone help me out with the loop function?
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,833
Members
452,947
Latest member
Gerry_F

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