Inserting new rows based on cell value and copying data to the new rows

10digwa

New Member
Joined
Jun 2, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I've been searching for a solution to my issue on the board but could not find a working solution that covers my needs.
I'd really appreciate some help to come up with a VBA code that does the following:

I need to break down a single row of data consisting of multiple columns into separate rows but the same column, similar to a transpose, but not exactly. Some rows contain different amounts of columns/data.
There is also a field, where I can enter a value, and this is the amount of lines that get inserted per cell/column within the row. I hope that makes sense.

I have the below code working as closest as I can to what I want, the only issue is, if row 1 has 8 values in 8 different columns, and row 2 has 6 Values in 6 different columns, then the code thinks there is data in row 2, Column 7 & 8. and creates those extra blank inserts. Is there a way i can remove these extra columns ?

CODE BELOW:

Sub Inert_rows()
Dim r As Long


For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
With Cells(r, 3)
If IsNumeric(.Value) And Not IsEmpty(.Value) Then
Rows(r + 1).Resize(.Value).Insert

Range(Replace("H#", "#", r)).Copy Destination:=Range("D" & r + 1).Resize(.Value)
Range(Replace("I#", "#", r)).Copy Destination:=Range("E" & r + 1).Resize(.Value)
End If
End With
Next r


For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
With Cells(r, 3)
If IsNumeric(.Value) And Not IsEmpty(.Value) Then
Rows(r + 1).Resize(.Value).Insert

Range(Replace("F#", "#", r)).Copy Destination:=Range("D" & r + 1).Resize(.Value)
Range(Replace("G#", "#", r)).Copy Destination:=Range("E" & r + 1).Resize(.Value)
End If
End With
Next r



For r = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
With Cells(r, 3)
If IsNumeric(.Value) And Not IsEmpty(.Value) Then
Rows(r + 1).Resize(.Value).Insert

Range(Replace("D#", "#", r)).Copy Destination:=Range("D" & r + 1).Resize(.Value)
Range(Replace("E#", "#", r)).Copy Destination:=Range("E" & r + 1).Resize(.Value)
End If
End With
Next r


End Sub

Order 12Order 1 Product 11Order 1 Product 21Order 1 Product 31Order 1 Product 41
Order 23Order 2 Product 11Order 2 Product 21
Order 34Order 3 Product 11Order 3 Product 21
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

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