Inserting / deleting rows when macro hits a certain term in Column A

Dear Experts:

I would like to run two macros on Column A of the active worksheet that performs the following task

1. Whenever the macro hits the term 'Website', the row directly below that row is to be deleted

2. Whenever the macro hits the term 'Name' a row directly below that row is to be inserted.

It should be two macros not a combined one.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

pls try

Sub DeleteRowAfterWebsite()

Set rngCellFound = Range("A1")

For Idx = 1 To WorksheetFunction.CountIf(Columns(1), "Website")
    Set rngCellFound = Columns(1).Find(What:="Website", After:=rngCellFound, _
        LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False)
        rngCellFound.Offset(1).EntireRow.Delete
Next Idx

End Sub

Sub AddRowAfterName()

Set rngCellFound = Range("A1")

For Idx = 1 To WorksheetFunction.CountIf(Columns(1), "Name")
    Set rngCellFound = Columns(1).Find(What:="Name", After:=rngCellFound, _
        LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False)
        rngCellFound.Offset(1).EntireRow.Insert
Next Idx

End Sub

Open in new window

Regards
0
SteveCommented:
You can just as easily loop down the rows:

Sub ChangeSheetWebsite()

Dim lastRow As Long, x As Long
Dim ws As Worksheet

Set ws = ActiveSheet

lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

For x = 1 To lastRow
    If ws.Cells(x, "A") = "Website" Then
        ws.Rows(x + 1).Delete
    End If
Next x

End Sub

Open in new window

Sub ChangeSheetName()

Dim lastRow As Long, x As Long
Dim ws As Worksheet
Set ws = ActiveSheet

lastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row

For x = 1 To lastRow
    If ws.Cells(x, "A") = "Name" Then
        ws.Rows(x + 1).Insert
    End If
Next x

End Sub

Open in new window

U--Example.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dsackerContract ERP Admin/ConsultantCommented:
Public Sub DeleteRows()
    Dim Max As Integer
    Dim Row As Integer

    Max = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For Row = Max To 2 Step -1  ' Start at the bottom, go up
        If LCase(Cells(Row - 1, 1).Value) = "website" Then
            Cells(Row, 1).EntireRow.Delete
        End If
    Next Row
End Sub

Public Sub InsertRows()
    Dim Max As Integer
    Dim Row As Integer

    Max = Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For Row = Max To 2 Step -1  ' Start at the bottom, go up
        If LCase(Cells(Row - 1, 1).Value) = "name" Then
            Cells(Row, 1).EntireRow.Insert
        End If
    Next Row
End Sub

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

SteveCommented:
Working backwards, though nescessary if working with the active row, is not nescessary if working with rows below where you are.
Rows should not be Integers integer will not go high enough if there are more than the old xls limit of 65526 (Integer limit = 32767, so use long)
The use of Lcase is good, if relevant, and may point towards using Like:
If LCase(Cells(Row - 1, 1).Value) Like "*name*" Then

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Hi Steve,

Thank you very much for your great help. I guess the second code of yours 'Insert_Rows' is to be tweaked ...
... since there is no blank row insertion below the last occurrence of the cell containing the term 'name'

But actually this is just a very minor issue with this code, otherwise everything is perfect.

Regards, Andreas
0
Andreas HermleTeam leaderAuthor Commented:
Hi Steve,

forget about it, a second trial produced the right results.

Regards, Andreas
0
Rgonzo1971Commented:
Replace xl Part with xlWhole

Sub DeleteRowAfterWebsite()

Set rngCellFound = Range("A1")

For Idx = 1 To WorksheetFunction.CountIf(Columns(1), "Website")
    Set rngCellFound = Columns(1).Find(What:="Website", After:=rngCellFound, _
        LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False)
        rngCellFound.Offset(1).EntireRow.Delete
Next Idx

End Sub

Sub AddRowAfterName()

Set rngCellFound = Range("A1")

For Idx = 1 To WorksheetFunction.CountIf(Columns(1), "Name")
    Set rngCellFound = Columns(1).Find(What:="Name", After:=rngCellFound, _
        LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
        SearchDirection:=xlNext, MatchCase:=False)
        rngCellFound.Offset(1).EntireRow.Insert
Next Idx

End Sub

Open in new window

0
Andreas HermleTeam leaderAuthor Commented:
Dear all,

great support from all of you. Thank you very much for your professional help. I really appreciate it. Since all of the approaches work just fine, I thought that splitting the points evenly is the best way.

Regards, Andreas
0
SteveCommented:
Indeed Andreas,
an even split seems fair,
there are many ways to perform the task,
this seems to cover the main ones.

ATB
Steve.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.