SOLVED

Formula to Duplicate Entire Rows Based on Cell Value

Copper Contributor

Hello everyone!

 

I'm trying to create a formula that will duplicate an entire row based on a cell's value. Below is a mock photo of what I am hoping to accomplish:

 

Screenshot 2022-03-28 114222.png

 

The formula would go in cell D2 and would duplicate all of column A and B based on the values in column B: i.e. Company 1 gets duplicated twice, Company 2 gets duplicated five times, etc. I have thousands of rows to do this for and it would take too long to do it by hand, especially when the values in column B get to be pretty large. The actual data is a little more complex than this, but any help would be greatly appreciated! Thanks in advance :)

14 Replies
best response confirmed by Corder (Copper Contributor)
Solution

@Corder 

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.

Amazing! 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.

@Corder 

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.

@Corder 

If you are using beta version, you may write the formula as below with 1 helper formula behind.

 

Starrysky1988_2-1648487589711.png

 

 

No worries,
I appreciate the help greatly!
Unfortunately, I don't have access to the beta since it is supplied by my company. I don't mind using a helper column since this sheet won't be seen anyway. Do you know of another formula or set of formulas that will accomplish the same thing using the non-beta version of Excel?

@Corder 

cam_corder_0-1648658867901.png

This is the set of formulas I ended up using for anyone who stubbles upon this thread later on. I have all of this on a helper sheet that I just hide and use sort and filter to call it on a different sheet. Thank you to everyone who helped!

@Corder 

The VBA provides me with a good start to what I am trying to do.  What I am trying to do is duplicate records and replace a cell value.

 

My PRIMARY table has values in ColC which can vary:

BlueBenz_0-1668812394222.png

 

My LOOKUP table lists FirstNames, a count, and then the variations:

BlueBenz_1-1668812394225.png

 

I want to count rows in the PRIMARY sheet and store as a variable and then loop.  The loop:

  1. In PRIMARY, store C2 value (here it is Robert)
  2. Go to LOOKUP table
  3. Compare C2 to each value in ColA.  If no match, increment in PRIMARY table.  Here C2 matches A7, so store each of the variable names in C7:G7 separately and then duplicate the PRIMARY table record A2:H2 five (5) times (per B7) at the end of the PRIMARY table.

Each of the five duplicates now have Robert as a ColC value.

BlueBenz_2-1668812394228.png

 

VBA then increments through the new records and replaces Robert with the variable:

BlueBenz_3-1668812394232.png

 

Can this be done?  Any help would be appreciated.

BlueBenz

@BlueBenz 

An alternative could be Power Query. In the attached file you can enter data in the blue dynamic tables in sheets "PRIMARY" and "LOOKUP". Then you can click in any cell in the green table and right-click with the mouse. Then select refresh.

primary lookup.JPG

lookup.JPG

@OliverScheurich 

This is very close to what I am seeking.  I was looking for VBA to add to existing coding I have.  The Power Query [PQ] concept you have does make it a good possibility. Permit me to further explain.

My workbook starts with two worksheets: PRIMARY and LOOKUP.  PRIMARY has the basic information on the left and is manually input.  To the right of the PRIMARY data are several additional columns which are populated by VBA which takes the values in Cols B, C & D to make cell values  "Ln, Fn Mn", "Ln, Fn Mi", "Fn Mn Ln", "Fn Mi Ln", "Fn Ln." This coding is done and functions.  What I seek is to append the FirstName variations to the existing PRIMARY table before I run the VBA which populates cells to the right of the PRIMARY range, creates additional tabs (e.g., “Ln, Fn Mn”, etc.), exports the tab data to delimited text files, and then deletes the new tabs then return to just having PRIMARY and LOOKUP tabs.

 

The Power Query [PQ] as it is now has the REFRESH table below and it includes the original record and the variations.  It would be much better if:

  1. The PQ was on a separate worksheet from the beginning (named, e.g., VARIATIONS)
  2. A button click launches the PQ (or VBA)
  3. The PQ determines then copies the PRIMARY range data to the VARIATIONS worksheet (A2:H####)
  4. The PQ runs and creates the REFRESH table to the right of the copied PRIMARY range data (K2:R####)
  5. The REFRESH range data (K2:R####) is determined, copied and appended at the end of the PRIMARY

 

My PRIMARY has some cells in color which would be copied between worksheets, so the alternating row color should not be present.

 

I hope this better explains what I am looking for.  My problem is working with FOR/NEXT and LOOPING which I know is an important part . . . I just don’t quite have it under control.  I am not familiar with Power queries, i.e., where they are coded, but with your example will try to understand and adapt.

 

If this can be done with VBA, much better, but any ideas will help.

@BlueBenz 

Certainly you can output the PQ result on a different worksheet named e.g. "VARIATIONS". Instead of a click button you can refresh the query with Alt+F5. Unfortunately i can't help you with the other tasks. Maybe you want to start a new discussion and ask the experts of the community for possible solutions.

variations.JPG

@Starrysky1988 Thank you for this example.  The only problem is that Column D & E are bringing back numbers instead of my values from columns A.  I think I am missing the formula that you have down in the step area? Also noticed it is throwing an error when I have a 1 in column B.  Use case is I would like the email in column A repeated as many times as the value in column B.

I need a list of emails repeated as many times as in column B

 

Screenshot 2023-11-05 at 9.53.06 PM.png

@LaurieBluegator With MS365, you could try something like this to generate a repeating list of email addresses from column A, based on the values in column B:

 

=LET(arr, A2:B6,
REDUCE("Email", SEQUENCE(ROWS(arr)), LAMBDA(v,n,
   VSTACK(v, IF(SEQUENCE(INDEX(arr, n, 2)), INDEX(arr, n, 1))))))

 

Or, if you also need the values in column B to repeat alongside the email addresses, try:

 

=LET(arr, A2:B6,
REDUCE({"Email","Rows"}, SEQUENCE(ROWS(arr)), LAMBDA(v,n,
   VSTACK(v, IF(SEQUENCE(INDEX(arr, n, 2)), INDEX(arr, n, 0))))))

 

Adjust the range reference (A2:B6) as needed. :)

@djclements Thank you so much.  I appreciate the options.  I will give these a try.

1 best response

Accepted Solutions
best response confirmed by Corder (Copper Contributor)
Solution

@Corder 

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.

View solution in original post