inserting row above when cell contains certain text

ctrl+alt_del

New Member
Joined
Nov 21, 2005
Messages
4
Trying to create a macro to insert a row above any cell that contains the word ' OPEN '. Any help would be appreciated. Thanks.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Welcome to the Board! How about this:

Code:
Sub FindAll()
Dim c As Range

    With Selection
        Set c = .Find(What:="open", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)
        If Not c Is Nothing Then
            firstAddress = c.Offset(1, 0).Address
            Do While Not c Is Nothing And c.Address <> firstAddress
                c.EntireRow.Insert
                Set c = .FindNext(c)
            Loop
        End If
    End With
End Sub
 
Upvote 0
thanks much, btadams

I should explain that the cells will contain other text, in addition to " Open ". I ran the macro, and it inserted no rows, even though there are hundreds of cells in the 1800+ rows which contain the word " Open ". Does the line:

Set c = .Find(what:="open", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows)

need to be modified because the cells will have more words than just " Open " in them?

Thanks again!
 
Upvote 0
It still did not add any empty rows. Here is an example of the data:
I have over 1800 rows, so a find and manual insert is going to be a pain. Also, some addresses have more rows of data than others, even though that isn't shown in the sample, so I can't just tell the macro add a blank row every X lines.

Thanks yet again....

XYZ Electrical Open
XYZ Electrical, ABC
P.O. Box 387
townname, ST 13245
Persons name
ABCDEFGHIJK Open
ABCDEFGHIJK, Inc.
1234-5 Street Name
townname, ST 12345
Persons Name
QWERTY Co. Open
QWERTY Co.
123 North Street
Townname, ST 12345
Persons Name
 
Upvote 0
I pasted your example into a worksheet and it inserted empty rows, in fact it got stuck in an endless loop because I hadn't thought about "open" being in the first cell of the selection. Try this:

Code:
Sub FindAll()
Dim c As Range

    With Selection
        Set c = .Find(What:="open", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows)
        If Not c Is Nothing Then
            firstAddress = c.Offset(1, 0).Address
            Do While Not c Is Nothing And c.Address <> firstAddress
                If c.Address = "$A$1" Then firstAddress = Range(firstAddress).Offset(1, 0).Address
                c.EntireRow.Insert
                Set c = .FindNext(c)
                MsgBox c.Address
            Loop
        End If
    End With
End Sub
 
Upvote 0
btadams, your last solution did work. My problem was I misinterpreted your lower case L as a Bar | when I read your updated macro off the the web page, and entered it into the VBA screen. I verified all the characters were correct, and it ran perfectly. Much appreciated!

(of course, now the ditz that asked for this in the first place tells me she gave me the wrong data, so I'll need to see if this macro will apply to the updated worksheet she's sending me.... !@#$%!!!!!)
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,484
Members
448,967
Latest member
visheshkotha

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