Forum Discussion
Formula to Duplicate Entire Rows Based on Cell Value
- Mar 28, 2022
Sub companyemployees() Dim i As Double Dim j As Double Dim z As Double Dim w As Double For i = 2 To 1000 j = Cells(i, 2).Value For z = 2 To j + 1 Cells(z + w, 4).Value = Cells(i, 1).Value Cells(z + w, 5).Value = Cells(i, 2).Value Next z w = w + z - 2 Next i End Sub
Maybe with these lines of VBA code. Click the button in cell G2 in the attached file to start the macro.
Sub companyemployees()
Dim i As Double
Dim j As Double
Dim z As Double
Dim w As Double
For i = 2 To 1000
j = Cells(i, 2).Value
For z = 2 To j + 1
Cells(z + w, 4).Value = Cells(i, 1).Value
Cells(z + w, 5).Value = Cells(i, 2).Value
Next z
w = w + z - 2
Next i
End Sub
Maybe with these lines of VBA code. Click the button in cell G2 in the attached file to start the macro.
- TrecsApr 04, 2024Copper Contributor
Hi All, the VBA table and code would work perfectly for my application of duplicating stock values which than need to be exported as csv. I am unfortunately very unfamiliar with VBA and am not sure how to extend the code to have the same function for Rows A to G, taking the copy value from Row D.
Any help in how to adjust this is greatly appreciated.
- OliverScheurichApr 04, 2024Gold Contributor
Sub product() Dim i As Double Dim j As Double Dim z As Double Dim k As Long Dim w As Double Range("K:Q").Clear k = Range("A" & Rows.Count).End(xlUp).Row For i = 2 To k j = Cells(i, 4).Value For z = 2 To j + 1 Cells(z + w, 11).Value = Cells(i, 1).Value Cells(z + w, 12).Value = Cells(i, 2).Value Cells(z + w, 13).Value = Cells(i, 3).Value Cells(z + w, 14).Value = Cells(i, 4).Value Cells(z + w, 15).Value = Cells(i, 5).Value Cells(z + w, 16).Value = Cells(i, 6).Value Cells(z + w, 17).Value = Cells(i, 7).Value Next z w = w + z - 2 Next i End Sub
In the attached file in sheet "Tabelle2" you can click the button in cell I2 to run the macro that returns the result in columns K to Q.
- RodelioFJul 23, 2024Copper Contributor
Good day!
Hi! OliverScheurich and all,
Can you please help me on my macro excel for entire rows. Below is a mock photo of what I am hoping to accomplish: Thank you!
- CorderMar 28, 2022Copper ContributorAmazing! Would this still work when trying to have the results appear on a separate sheet? I'm still new to VBA in Excel; is this SharePoint compatible? The document this is on is shared Teams Group and needs to be accessible for everyone.
- OliverScheurichMar 28, 2022Gold Contributor
The results can appear on a different sheet. Click the button in cell G2 in the attached file and check the results in sheet "Tabelle2". I don't work with Sharepoint and can't answer this question unfortunately.
- CorderMar 29, 2022Copper ContributorNo worries,
I appreciate the help greatly!