VBA to Split Duplicate rows and change values

OK2020

New Member
Joined
Apr 28, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm trying to write a macro to copy every row which has a value in column C, insert-paste it below it and then change the values in both rows.

I need to go from this:

ProductHoursPercent Agreed
Apples
10
Pears
10
0.5​
Oranges
20
0.75​


to this

ProductQuantityPercent Agreed
Apples
10
Pears
5
0.5​
Pears
5
0.5​
Oranges
15
0.75​
Steve
5
0.75​



I was thinking something like this, but got stuck:

VBA Code:
Option Explicit

Sub Split_Approved_Rows()
Dim i As Integer
i = 2
Dim a As Integer
a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To a

If Worksheets("Sheet1").Cells(a, 3).Value > 0 Then

Worksheets("Sheet1").Rows(i).Copy

Worksheets("Sheet1").Rows(i + 1).Insert??

End If

Next

End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this.
VBA Code:
Option Explicit

Sub Split_Approved_Rows()
    Dim i As Integer
    i = 2
    Dim a As Integer
    a = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
    For i = a To 2 Step -1
        If Worksheets("Sheet1").Cells(i, 3).Value > 0 Then
            Worksheets("Sheet1").Rows(i).Copy
            Worksheets("Sheet1").Rows(i + 1).Insert Shift:=xlDown
            Worksheets("Sheet1").Cells(i, 2) = Worksheets("Sheet1").Cells(i, 2) * Worksheets("Sheet1").Cells(i, 3)
            Worksheets("Sheet1").Cells(i + 1, 2) = Worksheets("Sheet1").Cells(i + 1, 2) - Worksheets("Sheet1").Cells(i, 2)
        End If
    Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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