Generate duplicate row whenever a row contains a specific value for a column

AntiPivotTable

New Member
Joined
Apr 29, 2013
Messages
6
I have a spreadsheet containing hundreds of row in the following format:

[TABLE="class: s14dydj4-19 lcbBNp"]
<tbody>[TR="class: s14dydj4-20 bDYXIH"]
[TH="class: s14dydj4-25 iHsscd, align: center"]obs_code[/TH]
[TH="class: s14dydj4-25 iHsscd, align: center"]obs_name[/TH]
[TH="class: s14dydj4-25 iHsscd, align: center"]city[/TH]
[TH="class: s14dydj4-25 iHsscd, align: center"]value_yr_1[/TH]
[TH="class: s14dydj4-25 iHsscd, align: center"]value_yr_2[/TH]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104125[/TD]
[TD="class: s14dydj4-21 VJyPR"]E and W[/TD]
[TD="class: s14dydj4-21 VJyPR"]ABC[/TD]
[TD="class: s14dydj4-21 VJyPR"]451[/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104125[/TD]
[TD="class: s14dydj4-21 VJyPR"]E and W[/TD]
[TD="class: s14dydj4-21 VJyPR"]YZW[/TD]
[TD="class: s14dydj4-21 VJyPR"]26[/TD]
[TD="class: s14dydj4-21 VJyPR"]54[/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104126[/TD]
[TD="class: s14dydj4-21 VJyPR"]T and O[/TD]
[TD="class: s14dydj4-21 VJyPR"]YZW[/TD]
[TD="class: s14dydj4-21 VJyPR"]1[/TD]
[TD="class: s14dydj4-21 VJyPR"]251[/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104111[/TD]
[TD="class: s14dydj4-21 VJyPR"]G and Y[/TD]
[TD="class: s14dydj4-21 VJyPR"]ABC[/TD]
[TD="class: s14dydj4-21 VJyPR"]54[/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104111[/TD]
[TD="class: s14dydj4-21 VJyPR"]G and Y[/TD]
[TD="class: s14dydj4-21 VJyPR"]YZW[/TD]
[TD="class: s14dydj4-21 VJyPR"]12[/TD]
[TD="class: s14dydj4-21 VJyPR"]35[/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104111[/TD]
[TD="class: s14dydj4-21 VJyPR"]G and Y[/TD]
[TD="class: s14dydj4-21 VJyPR"]TUS[/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1104111[/TD]
[TD="class: s14dydj4-21 VJyPR"]G and Y[/TD]
[TD="class: s14dydj4-21 VJyPR"]REW[/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[TD="class: s14dydj4-21 VJyPR"]25[/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1154111[/TD]
[TD="class: s14dydj4-21 VJyPR"]X and U[/TD]
[TD="class: s14dydj4-21 VJyPR"]TUS[/TD]
[TD="class: s14dydj4-21 VJyPR"]36[/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[/TR]
[TR="class: s14dydj4-20 bDYXIH"]
[TD="class: s14dydj4-21 VJyPR"]1154113[/TD]
[TD="class: s14dydj4-21 VJyPR"]R and S[/TD]
[TD="class: s14dydj4-21 VJyPR"]REW[/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[TD="class: s14dydj4-21 VJyPR"][/TD]
[/TR]
</tbody>[/TABLE]

I would like to duplicate all rows where column 'obs_code' equals 1104111 and have the duplicate contain the same values (or lack of values) and information for columns 'city', 'value_yr_1' and 'value_yr_2'. The only thing that would be different is that the 'obs_code' would now be 1104211 and the 'obs_name' would now be 'Y' (rather than 'G and Y').

How can I achieve this?

It would be great to have the duplicate row created right under the original row.
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this:
Code:
Public Sub Insert_New_Rows()

    Dim r As Long
    
    Application.ScreenUpdating = False
    For r = Cells(Rows.Count, 1).End(xlUp).Row To 2 Step -1
        If Cells(r, 1).Value = "1104111" Then
            Cells(r, 1).EntireRow.Copy
            Cells(r + 1, 1).Insert shift:=xlDown
            Cells(r + 1, 1).Value = "1104211"
            Cells(r + 1, 2).Value = "Y"
        End If
    Next
    Application.CutCopyMode = False
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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