VBA code to insert row when a search criteria is found

John T

Board Regular
Joined
Nov 28, 2013
Messages
145
Office Version
  1. 365
Platform
  1. Windows
I have data in cells B2:E2 and this can go down 100+ rows.

In column B i have invoice numbers but some cells contain the word "Deposit".

I have sorted this data so that the invoice numbers appear first and then all the Deposits.

I need a code to find the first instance of the word "Deposit" and to insert a row so that all the invoices and Deposits are seperated by a single row.

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi John T,

Something like this - inserts a row at all instances of 'Deposit' in Column B

Code:
Sub InsertRowAtDeposit()


    Dim Found As Range, FirstFound As Range, LastFound As Range


    'Init
    Set Found = ActiveSheet.Range("B:B").Find( _
                What:="Deposit", _
                After:=Range("B1"), _
                LookIn:=xlValues, _
                Lookat:=xlWhole, _
                SearchOrder:=xlByRows, _
                MatchCase:=True)


    If Not Found Is Nothing Then
        Set FirstFound = Found
        Set LastFound = Found


        'Loop
        Do
            Found.EntireRow.Insert Shift:=xlShiftDown, CopyOrigin:=False


            Set Found = ActiveSheet.Range("B:B").Find( _
                        What:="Deposit", _
                        After:=LastFound, _
                        LookIn:=xlValues, _
                        Lookat:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        MatchCase:=True)


            Set LastFound = Found


            DoEvents
        Loop While Found.Address <> FirstFound.Address


    End If


End Sub
 
Upvote 0
Thanks, this works except i need it to stop when it finds the first deposit.

Your code inserts a row between every instance of the word deposit.

I just need it to insert one row.
 
Upvote 0
Thanks, this works except i need it to stop when it finds the first deposit.

Your code inserts a row between every instance of the word deposit.

I just need it to insert one row.

I didn't understand your layout initially. You don't have sub-totalling per-se, just an ordered list.

That shortens it considerably then.

Code:
Sub InsertRowAtDeposit()


    Dim Found As Range


    'Init
    Set Found = ActiveSheet.Range("B:B").Find( _
                What:="Deposit", _
                After:=Range("B1"), _
                LookIn:=xlValues, _
                Lookat:=xlWhole, _
                SearchOrder:=xlByRows, _
                MatchCase:=True)


    If Not Found Is Nothing Then Found.EntireRow.Insert Shift:=xlShiftDown, CopyOrigin:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

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