SOLVED

Copy data and insert value from list

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3163825%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3ECopy%20data%20and%20insert%20value%20from%20list%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3163825%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EHi%20Guys%2C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EApologies%20for%20the%20simplistic%20nature%20of%20this%20request.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EI%20have%20a%20list%20of%20rows%20that%20I%20need%20to%20duplicate%2096%20times%20adding%20in%20a%20value%20from%20a%20defined%20list%20into%20columns%20F2%20and%20G2%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%5C%26quot%3Blia-inline-image-display-wrapper%22%20lia-image-align-inline%3D%22%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fgxcuf89792%2F%5C%26quot%3Bhttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F348104i55D6BD89A4B0961B%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%5C%26quot%3B%22%20role%3D%22%5C%26quot%3Bbutton%5C%26quot%3B%22%20title%3D%22RickWilliams_0-1644856456841.png%22%20alt%3D%22%5C%26quot%3BRickWilliams_0-1644856456841.png%5C%26quot%3B%22%20%2F%3E%26lt%3B%5C%2Fspan%26gt%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EThe%20list%20has%2095%20values%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%5C%26quot%3Blia-inline-image-display-wrapper%22%20lia-image-align-inline%3D%22%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fgxcuf89792%2F%5C%26quot%3Bhttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F348105iC1EAACB26155650B%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%5C%26quot%3B%22%20role%3D%22%5C%26quot%3Bbutton%5C%26quot%3B%22%20title%3D%22RickWilliams_1-1644856510522.png%22%20alt%3D%22%5C%26quot%3BRickWilliams_1-1644856510522.png%5C%26quot%3B%22%20%2F%3E%26lt%3B%5C%2Fspan%26gt%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EThe%20result%20would%20be%20having%2095%20copies%20of%20the%20rows%20ready%20for%20bulk%20loading%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%5C%26quot%3Blia-inline-image-display-wrapper%22%20lia-image-align-inline%3D%22%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Fgxcuf89792%2F%5C%26quot%3Bhttps%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F348106i40C2E5CAB4CF6B43%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%5C%26quot%3B%22%20role%3D%22%5C%26quot%3Bbutton%5C%26quot%3B%22%20title%3D%22RickWilliams_2-1644856637163.png%22%20alt%3D%22%5C%26quot%3BRickWilliams_2-1644856637163.png%5C%26quot%3B%22%20%2F%3E%26lt%3B%5C%2Fspan%26gt%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EThis%20can%20be%20done%20by%20hand%20of%20course%20but%20thought%20best%20to%20see%20if%20there%20was%20a%20scripted%20approach%20as%20this%20will%20be%20a%20task%20that%20I%20will%20need%20to%20repeat%20going%20forward.%3CBR%20%2F%3EThank%20you%20SO%20much%20in%20advance.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ERick%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3163825%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EExcel%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3ETraining%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E
New Contributor

Hi Guys,

Apologies for the simplistic nature of this request.

I have a list of rows that I need to duplicate 96 times adding in a value from a defined list into columns F2 and G2:

RickWilliams_0-1644856456841.png

 

The list has 95 values:

RickWilliams_1-1644856510522.png

The result would be having 95 copies of the rows ready for bulk loading:

RickWilliams_2-1644856637163.png

This can be done by hand of course but thought best to see if there was a scripted approach as this will be a task that I will need to repeat going forward.
Thank you SO much in advance.

Rick

5 Replies
best response confirmed by Sergei Baklan (MVP)
Solution

@RickWilliams 

Maybe with these lines of code. An adjustment to the name of the worksheet containing the 95 values is required. You can click the button in cell I2 to execute the macro.

 

Sub multiply()

Dim i As Integer
Dim j As Integer
Dim z As Integer

z = 0
For j = 2 To 96
For i = 2 To 96

Cells(i + z, 6).Value = Cells(j, 3).Value
Cells(i + z, 7).Value = Cells(j, 4).Value

Next i
z = z + 95

Next j

End Sub

 

@Quadruple_Pawn 

This is FANTASTIC!  

Thank you so very much!

Just in the process of reverse engineering your script so I can fully appreciate/understand!

Thank you again

Rick

@Quadruple_Pawn 

Apologies for further queries, as a separate script, how would I go about copying a block of rows/columns and replicating this ie:

RickWilliams_0-1644920204743.png

If I wanted to duplicate the block of rows several times in a sheet.

Thanks in advance.

@RickWilliams 

Sub multiply()

Dim i As Integer
Dim j As Integer
Dim z As Integer
Dim no_of_rows As Integer
Dim start_row As Integer
Dim no_of_multiplications As Integer

Range("F:G").Clear

start_row = Cells(1, 5).Value
no_of_rows = Cells(2, 5).Value
no_of_multiplications = Cells(3, 5).Value

z = 0

For i = 1 To no_of_multiplications
For j = start_row To no_of_rows + start_row - 1

Cells(i + z, 6).Value = Cells(j, 1).Value
Cells(i + z, 7).Value = Cells(j, 2).Value

z = z + 1

Next j
Next i


End Sub

 

With this code i can enter the start_row, no_of_rows and no_of_multiplications in cells E1, E2 and E3. After clicking the button in cell I2 the block of data is multiplied in columns F and G.

Thank you so very much this is amazing!