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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
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,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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