Copy-paste rows as many times as nb of values on a cell

notexpert

New Member
Joined
Feb 9, 2021
Messages
2
Platform
  1. Windows
Hello,

I have files with the following format:

Column AColumn BColumn CColumn DColumn EColumn FColumn GColumn H
Location nameStand 1Mn25Maria24/241/12,1002-02-2021
Location nameStand 1Jn79John24/241/11,902-02-2021
Location nameStand 1Ge3George24/241/14,1202-02-2021
Location nameStand 1So2Sophie24/241/13,1102-02-2021

Every time I have to copy-paste the rows as many times as the values on column G (separated by comma) and only keep one value at the time on column G. The number of values on this column can vary from 1 up to 8 different values. So for the example above, the desired outcome would be the following:

Column AColumn BColumn CColumn DColumn EColumn FColumn GColumn H
Location nameStand 1Mn25Maria24/241/1
2​
02-02-2021
Location nameStand 1Mn25Maria24/241/1
10​
02-02-2021
Location nameStand 1Jn79John24/241/1
1​
02-02-2021
Location nameStand 1Jn79John24/241/1
9​
02-02-2021
Location nameStand 1Ge3George24/241/1
4​
02-02-2021
Location nameStand 1Ge3George24/241/1
12​
02-02-2021
Location nameStand 1So2Sophie24/241/1
3​
02-02-2021
Location nameStand 1So2Sophie24/241/1
11​
02-02-2021

How could I automate this? It takes forever to do it manually and I have files with hundreds of rows!

Thanks a lot in advance, it would be a world-changer for me if someone could help me! I don't have the knowledge to create macros and trying to just copy-paste macros that kinda do this, didn't work so far.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi, @notexpert. Welcome to the Forum
Try this:
I put result in col J

VBA Code:
Sub a1161175a()
Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, vc, x, z

n = Range("A" & Rows.Count).End(xlUp).Row
va = Range("A1:H" & n)
vb = Range("G1:G" & n)
ReDim vc(1 To UBound(va, 1) * 8, 1 To UBound(va, 2))

For i = 1 To UBound(va, 1)
    z = Split(vb(i, 1), ",")
    
    For Each x In z
        k = k + 1
    
        For j = 1 To UBound(va, 2)
            If j = 7 Then
                vc(k, j) = x
                Else
                vc(k, j) = va(i, j)
            End If
        Next
    
    Next

Next

'put result in col J
Range("J1").Resize(k, UBound(vc, 2)) = vc
End Sub
 
Upvote 0
Solution
Hi, @notexpert. Welcome to the Forum
Try this:
I put result in col J

VBA Code:
Sub a1161175a()
Dim i As Long, j As Long, k As Long, n As Long
Dim va, vb, vc, x, z

n = Range("A" & Rows.Count).End(xlUp).Row
va = Range("A1:H" & n)
vb = Range("G1:G" & n)
ReDim vc(1 To UBound(va, 1) * 8, 1 To UBound(va, 2))

For i = 1 To UBound(va, 1)
    z = Split(vb(i, 1), ",")
   
    For Each x In z
        k = k + 1
   
        For j = 1 To UBound(va, 2)
            If j = 7 Then
                vc(k, j) = x
                Else
                vc(k, j) = va(i, j)
            End If
        Next
   
    Next

Next

'put result in col J
Range("J1").Resize(k, UBound(vc, 2)) = vc
End Sub
Hi @Akuini Thanks a lot for your reply!
I tried it and it works! Many thanks! You made my day!
 
Upvote 0
You're welcome, glad to help & thanks for the feedback. :)
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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