VBA - Copy row, adjust cell content and

haddy

New Member
Joined
Sep 3, 2014
Messages
22
Hi,

Apologies if this type of solution has been posted elsewhere - I have been attempting to use a variation of the solution provided under https://www.mrexcel.com/forum/excel...-paste-row-while-replacing-2-cell-values.html

What I'm trying to achieve is having excel look through a column to locate ", " and split up the text within the cell located into a number of rows. As shown in the tables below; ID 3 would be copied over three times and ID 4 twice. I found difficulty with identifying the 'original' row with its cell contents (minus what had already been copied over).

New to the VBA scene, so I'm hoping someone could assist.


Current data
IDContent
1RD-001
2RD-002
3RD-003, RD-004, RD-005
4RD-006, RD-007

<tbody>
</tbody>

Data I'd like to have after running the macro
IDContent
1RD-001
2RD-002
3RD-003
3RD-004
3RD-005
4RD-006
4RD-007

<tbody>
</tbody>


Edit; Title was meant to read 'VBA - Copy row, adjust cell content and remove redundant cell content'
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this.
If you have a large data and the result is more than 100K rows then you need to adjust this part of the code:
ReDim vb(1 To 100000, 1 To 2)

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1089775a()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1089775-vba-copy-row-adjust-cell-content.html[/COLOR][/I]

[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], j [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], k [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] va, vb

va = Range([COLOR=brown]"A2:B"[/COLOR] & Cells(Rows.count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp).Row)
[COLOR=Royalblue]ReDim[/COLOR] vb([COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]100000[/COLOR], [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]2[/COLOR])

[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])

    [COLOR=Royalblue]If[/COLOR] InStr(va(i, [COLOR=crimson]2[/COLOR]), [COLOR=brown]", "[/COLOR]) [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] x [COLOR=Royalblue]In[/COLOR] Split(va(i, [COLOR=crimson]2[/COLOR]), [COLOR=brown]", "[/COLOR])
        j = j + [COLOR=crimson]1[/COLOR]
        vb(j, [COLOR=crimson]1[/COLOR]) = va(i, [COLOR=crimson]1[/COLOR]): vb(j, [COLOR=crimson]2[/COLOR]) = x
        [COLOR=Royalblue]Next[/COLOR]
    [COLOR=Royalblue]Else[/COLOR]
        j = j + [COLOR=crimson]1[/COLOR]
        vb(j, [COLOR=crimson]1[/COLOR]) = va(i, [COLOR=crimson]1[/COLOR]): vb(j, [COLOR=crimson]2[/COLOR]) = va(i, [COLOR=crimson]2[/COLOR])
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]

[COLOR=Royalblue]Next[/COLOR]

Range([COLOR=brown]"A2"[/COLOR]).Resize(j, [COLOR=crimson]2[/COLOR]) = vb

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR][/FONT]
 
Upvote 0
Thank you Akuini for your timely solution,

Working through trying to understand what you've done but it works with the smaller example. I've adjusted the code you've put together to suit data I'm looking at - please correct me if I'm wrong.

My column range is between A & W, and my cells I'd like to split is under Column D.
What am I missing?


Code:
Sub a1089775a()'https://www.mrexcel.com/forum/excel-questions/1089775-vba-copy-row-adjust-cell-content.html


Dim i As Long, j As Long, k As Long
Dim va, vb


va = Range("A2:W" & Cells(Rows.Count, "D").End(xlUp).Row)
ReDim vb(1 To 100000, 1 To 2)


For i = 1 To UBound(va, 1)


    If InStr(va(i, 2), ", ") Then
        For Each x In Split(va(i, 2), ", ")
        j = j + 1
        vb(j, 1) = va(i, 1): vb(j, 2) = x
        Next
    Else
        j = j + 1
        vb(j, 1) = va(i, 1): vb(j, 2) = va(i, 2)
    End If


Next


Range("A2").Resize(j, 2) = vb


End Sub
 
Upvote 0
Ok. try this one:

Code:
[FONT=lucida console][COLOR=Royalblue]Sub[/COLOR] a1089775b()
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1089775-vba-copy-row-adjust-cell-content.html[/COLOR][/I]

[COLOR=Royalblue]Dim[/COLOR] i [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], j [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR], k [COLOR=Royalblue]As[/COLOR] [COLOR=Royalblue]Long[/COLOR]
[COLOR=Royalblue]Dim[/COLOR] va, vb

va = Range([COLOR=brown]"A2:W"[/COLOR] & Cells(Rows.count, [COLOR=brown]"A"[/COLOR]).[COLOR=Royalblue]End[/COLOR](xlUp).Row)
[COLOR=Royalblue]ReDim[/COLOR] vb([COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]100000[/COLOR], [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]2[/COLOR]))

[COLOR=Royalblue]For[/COLOR] i = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] UBound(va, [COLOR=crimson]1[/COLOR])

    [COLOR=Royalblue]If[/COLOR] InStr(va(i, [COLOR=crimson]4[/COLOR]), [COLOR=brown]", "[/COLOR]) [COLOR=Royalblue]Then[/COLOR]
        [COLOR=Royalblue]For[/COLOR] [COLOR=Royalblue]Each[/COLOR] x [COLOR=Royalblue]In[/COLOR] Split(va(i, [COLOR=crimson]4[/COLOR]), [COLOR=brown]", "[/COLOR])
        j = j + [COLOR=crimson]1[/COLOR]
            [COLOR=Royalblue]For[/COLOR] n = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]23[/COLOR]
            vb(j, n) = va(i, n)
            [COLOR=Royalblue]Next[/COLOR]
        vb(j, [COLOR=crimson]4[/COLOR]) = x
        [COLOR=Royalblue]Next[/COLOR]
    [COLOR=Royalblue]Else[/COLOR]
        j = j + [COLOR=crimson]1[/COLOR]
            [COLOR=Royalblue]For[/COLOR] n = [COLOR=crimson]1[/COLOR] [COLOR=Royalblue]To[/COLOR] [COLOR=crimson]23[/COLOR]
            vb(j, n) = va(i, n)
            [COLOR=Royalblue]Next[/COLOR]
    [COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]If[/COLOR]

[COLOR=Royalblue]Next[/COLOR]

Range([COLOR=brown]"A2"[/COLOR]).Resize(j, UBound(vb, [COLOR=crimson]2[/COLOR])) = vb

[COLOR=Royalblue]End[/COLOR] [COLOR=Royalblue]Sub[/COLOR]
[/FONT]
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,734
Members
448,987
Latest member
marion_davis

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