Macro to insert row and add data below.

Xirom431

Board Regular
Joined
Dec 2, 2010
Messages
102
Hi,

I have a spreadsheet with 26 Columns and 8500 rows. I would like to have a macro where I can search for a data XXXXX in Column Q, then insert entire row below data XXXX if found and add data YYYYY in the row below data XXXX in Column Q. Thanks in advance.
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try running this on a copy of your workbook, editing the Xstring and Ystring variables as required.
Code:
Sub Find_Data_and_Insert_Row()

    Dim Xstring As String, Ystring As String
    Dim FindData As Range
    Dim FirstAddress As String

    Xstring = "xyz"     'CHANGE AS REQUIRED
    Ystring = "987"     'CHANGE AS REQUIRED
    
    'Search for the X string in column Q of active sheet
        
    Set FindData = Columns("Q").Find(What:=Xstring, After:=Range("Q1"), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
    If Not FindData Is Nothing Then
    
        'Found, so save first cell address
        
        FirstAddress = FindData.Address
        Do
            'Insert a row below and put in the Y string
            
            FindData.Offset(1, 0).EntireRow.Insert
            FindData.Offset(1, 0).Value = Ystring
            
            'Search for X string again
            
            Set FindData = Columns("Q").FindNext(FindData)
            
            'Until not found or back to first cell
            
        Loop Until FindData Is Nothing Or FirstAddress = FindData.Address
    End If
    
End Sub
 
Upvote 0
Hi,

The macro above worked great. How can I also copy the info from XString in Columns E F I J K L to Columns E F I J K L in the new YString row? Thanks.
 
Upvote 0
How can I also copy the info from XString in Columns E F I J K L to Columns E F I J K L in the new YString row?
This should do it:
Code:
Sub Find_Data_and_Insert_Row()

    Dim Xstring As String, Ystring As String
    Dim FindData As Range
    Dim FirstAddress As String

    Xstring = "xyz"     'CHANGE AS REQUIRED
    Ystring = "987"     'CHANGE AS REQUIRED
    
    'Search for the X string in column Q of active sheet
        
    Set FindData = Columns("Q").Find(What:=Xstring, After:=Range("Q1"), LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
        
    If Not FindData Is Nothing Then
    
        'Found, so save first cell address
        
        FirstAddress = FindData.Address
        Do
            
            With FindData
            
                'Insert a row below and put in the Y string
                
                .Offset(1, 0).EntireRow.Insert
                .Offset(1, 0).Value = Ystring
            
                'Copy columns E-F and I-L from X string row to new row
                
                Range(Cells(.Row, "E"), Cells(.Row, "F")).Copy Cells(.Row + 1, "E")
                Range(Cells(.Row, "I"), Cells(.Row, "L")).Copy Cells(.Row + 1, "I")
            End With
                
            'Search for X string again
            
            Set FindData = Columns("Q").FindNext(FindData)
            
            'Until not found or back to first cell
            
        Loop Until FindData Is Nothing Or FirstAddress = FindData.Address
    End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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