Add rows above cells containing specific string.

topi1

Board Regular
Joined
Aug 6, 2014
Messages
161
Office Version
  1. 2010
I am hoping someone can help me with the following script.
It changes before to after as follows. I am hoping to change it so that it splits any row like the last row where there are two lookup occurrences ("firewall") and adds a row. Please note how I want row 12 in the "After" to change to rows 12 and 14 as in "Desired after". In fact, if it has third occurrence of "firewall", it should split that again and add row 16.

Additionally, the data in the column A can be anywhere and not necessarily begin at A1. It gives me error if it does not start at A1. I got the script from the web and don't fully understand to do any of these two modifications on my own. Any and all help will be greatly appreciated. Thank you.

Before
rscriptor.xlsm
O
23firewall abc
24x firewall y
25policy y
26xyz firewall
27policy z
28firewall abc
29policy xxx
30x firewall y. xyz firewall
Add row above


After
rscriptor.xlsm
A
1firewall abc
2
3x firewall y
4policy y
5
6xyz firewall
7policy z
8
9firewall abc
10policy xxx
11
12x firewall y. xyz firewall
Add row above


Desired After
rscriptor.xlsm
A
1firewall abc
2
3x firewall y
4policy y
5
6xyz firewall
7policy z
8
9firewall abc
10policy xxx
11
12x firewall y.
13
14xyz firewall
Add row above

VBA Code:
Option Explicit

Sub NewRowInsert()
    
    Const sText As String = "FirEWaLL"
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet: Set ws = wb.Worksheets("Add row above")
    Dim LastRow As Long: LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim rg As Range: Set rg = ws.Range("A2:A" & LastRow)
    Dim sCell As Range: Set sCell = rg.Find(sText, , xlFormulas, xlPart)
    
    Application.ScreenUpdating = False
    
    Dim trg As Range
    Dim sCount As Long
    
    If Not sCell Is Nothing Then
        Dim FirstAddress As String: FirstAddress = sCell.Address
        Do
            If trg Is Nothing Then
                Set trg = sCell
            Else
                Set trg = Union(trg, sCell.Offset(, sCount Mod 2))
            End If
            sCount = sCount + 1
            Set sCell = rg.FindNext(sCell)
        Loop Until sCell.Address = FirstAddress
        trg.EntireRow.Insert
    End If
    
    Application.ScreenUpdating = True
    
    Select Case sCount
    Case 0
        MsgBox "'" & sText & "' not found.", vbExclamation, "Fail?"
    Case 1
        MsgBox "Found 1 occurrence of '" & sText & "'.", _
            vbInformation, "Success"
    Case Else
        MsgBox "Found " & sCount & " occurrences of '" & sText & "'.", _
            vbInformation, "Success"
    End Select

End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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