Duplicate number of lines based on conditions

piethein123

New Member
Joined
Oct 19, 2023
Messages
16
Office Version
  1. 365
Platform
  1. Windows
I have a question regarding duplicating a number of lines based on condition.

so for example , based on the condition in column F "packed road" , i would need to generate an additional amount of lines with the exact same info. In this case 4 additional lines

Is this possible in excel?

PlantShip-To PartyMaterialIncotermsShipping condition codeShipping condition (general)Avg
1​
2​
3​
DAP
10​
PACKED ROAD
0,207450335​
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
so for example , based on the condition in column F "packed road" , i would need to generate an additional amount of lines with the exact same info. In this case 4 additional lines
You didn't explain why that record would require 4 additional lines, so my guess is that 4 = Column C + 1 (column C is 3 in your example, so a total of 5 rows will be created on sheet2)
With this guess, this macro will create on Sheet2 a replica of the starting data with the additional lines:
VBA Code:
Sub NewList()
Dim I As Long, RArr, LastCol As Long, RNext As Long, LFor As String
Dim RowsNum As Long
'
LFor = "PACKED ROAD"
'
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
Sheets("Sheet2").Range("A:A").Resize(, LastCol).ClearContents       'Clear Sheet2!
RNext = 1
For I = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    RArr = Cells(I, 1).Resize(1, LastCol).Value
    If RArr(1, 6) = LFor Then RowsNum = RArr(1, 3) + 2 Else RowsNum = 1
    Sheets("Sheet2").Cells(RNext, 1).Resize(RowsNum, LastCol) = RArr
    RNext = RNext + RowsNum
Next I
End Sub
 
Upvote 0
Sorry for the confusion. The additional amount of rows is not based on the value column c. It is based on how many lines i need to upload which can be different based on information in column D or E or F. I would like the VBA to use the additional amount of rows based on what number i fill in a cell (or even the formula).
Would that be possible ?
 
Upvote 0
OK , i can add that in the last column ; so this is the exact layout of the file. I have made up the numbers so that's not important. Column K is the amount of times the specific row needs to be duplicated.

PlantShip-To PartyMaterialIncotermsShipping condition codeShipping condition (general)Total EURTotal gross without palletPer unit Gross KG without palletavglines to added
1​
2​
3​
DAP
10​
PACKED ROAD
10​
20​
0,207450335​
0,12​
4​
 
Upvote 0
So modify this line in my macro:
VBA Code:
If RArr(1, 6) = LFor Then RowsNum = RArr(1, 11)  Else RowsNum = 1
 
Upvote 0
Solution
Thank you for the feedback
(maybe it had to be ...Then RowsNum = RArr(1, 11) +1 Else ... ?)
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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