Need a macro to duplicate rows conditionally

Gamermatt

Board Regular
Joined
May 14, 2009
Messages
186
Hello,

I have a large database. In columns A, B, C, and D there may be text. Every row has text in column A, and only some have text in B-D. I need a macro to duplicate the rows that have text in B, C, and/or D, copying those cells down to the appropriate cell. here is a basic example

before:

Excel Workbook
ABCD
1product1text1text1text3
2product2txt1**
Sheet1



after:

Excel Workbook
ABCD
4product1text1**
5product1text2**
6product1text3**
7product2txt1**
Sheet1


The actual database has content in dozens of cells for each row, so make sure it copies the entire row.

Thanks
Matt
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Code:
Sub repeater()
    Application.ScreenUpdating = False
 
    Dim val As Variant
 
 
    'find the last row
    Dim rowcount As Integer
    rowcount = 1
    While Cells(rowcount, 1).Value <> ""
        rowcount = rowcount + 1
    Wend
    rowcount = rowcount - 1
 
 
    'Iterate through all the rows, bottom to top so we don't process the rows that we insert
    For rowItr = rowcount To 1 Step -1
 
        'Iterate through the cells of each row that would cause us to copy a row.
        For colItr = 4 To 3 Step -1
 
            If Cells(rowItr, colItr).Value <> "" Then
                'copy the row, and insert it below
                Rows(rowItr).Copy
                Rows(rowItr + 1).Insert Shift:=xlDown
 
                'clean up the cells
                val = Cells(rowItr, colItr).Value
                Cells(rowItr, colItr).Value = ""
                Range(Cells(rowItr + 1, 2), Cells(rowItr + 1, 4)).Clear
                Cells(rowItr + 1, 2).Value = val
 
            End If
 
        Next colItr
 
    Next rowItr
 
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,565
Messages
6,179,549
Members
452,927
Latest member
rows and columns

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