Macro or function to copy cell B to multiple rows and then populate cell A2 onwards

avaris79

New Member
Joined
Dec 23, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
Hi All,

I am new to this forum and excel macro.
I have data in column A where it is a single value & in column B where it is multiple values separated by ";".
Is there a way, using macro or function, to convert column B to multiple rows, then copy values from A1 to A+n onwards? Refer to my attached pictures for reference.
Thank you.
 

Attachments

  • Source.jpg
    Source.jpg
    17.1 KB · Views: 13
  • Target.jpg
    Target.jpg
    19.2 KB · Views: 13

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Hi
And welcome
Try
VBA Code:
Sub test()
    lr = Cells(Rows.Count, 2).End(xlUp).Row
    For i = lr To 2 Step -1
        x = Split(Cells(i, 2), ";")
        Cells(i, 1).Offset(1).Resize(UBound(x)).EntireRow.Insert
        Cells(i, 1).Offset(1).Resize(UBound(x) - 1) = Cells(i, 1)
        Cells(i, 2).Resize(UBound(x) + 1) = Application.Transpose(x)
    Next
End Sub
 
Upvote 0
Hi
And welcome
Try
VBA Code:
Sub test()
    lr = Cells(Rows.Count, 2).End(xlUp).Row
    For i = lr To 2 Step -1
        x = Split(Cells(i, 2), ";")
        Cells(i, 1).Offset(1).Resize(UBound(x)).EntireRow.Insert
        Cells(i, 1).Offset(1).Resize(UBound(x) - 1) = Cells(i, 1)
        Cells(i, 2).Resize(UBound(x) + 1) = Application.Transpose(x)

[/QUOTE]

Thank you very much! 1 issue is that when there are more than 1 row in the source A1, the last value in A+n seems to be blank.

Also, how do we also built a function to copy value from column C & D to C+n & D+n, as shown in attachment.
 

Attachments

  • ScreenHunter 1288 20201224.jpg
    ScreenHunter 1288 20201224.jpg
    27.6 KB · Views: 9
  • source2.jpg
    source2.jpg
    23.1 KB · Views: 11
  • target2.jpg
    target2.jpg
    47.9 KB · Views: 9
Upvote 0

avaris79

In you original pose #1
Your data was ended with << ; >>
In the above post there is non

What is the correct data layout you want
 
Upvote 0

avaris79

In you original pose #1
Your data was ended with << ; >>
In the above post there is non

What is the correct data layout you want
Sorry didn't notice that, the data is supposed end with no ";" in column B.
 
Upvote 0
In this case
just
change
VBA Code:
Cells(i, 1).Offset(1).Resize(UBound(x) - 1) = Cells(i, 1)
to
VBA Code:
Cells(i, 1).Offset(1).Resize(UBound(x)) = Cells(i, 1)
 
Upvote 0
And
VBA Code:
Sub test()
    lr = Cells(Rows.Count, 2).End(xlUp).Row
    For i = lr To 2 Step -1
        x = Split(Cells(i, 2), ";")
        Cells(i, 1).Offset(1).Resize(UBound(x)).EntireRow.Insert
        Cells(i, 1).Offset(1).Resize(UBound(x)) = Cells(i, 1)
        Cells(i, 2).Resize(UBound(x) + 1) = Application.Transpose(x)
        Cells(i, 3).Offset(1).Resize(UBound(x)) = Cells(i, 3)
        Cells(i, 4).Offset(1).Resize(UBound(x)) = Cells(i, 4)
    Next
End Sub
for the second issue
 
Upvote 0
Solution
Thank you very much Mohadin!

I would like to understand on statement "Cells(i, 1).Offset(1).Resize(UBound(x)).EntireRow.Insert". How do we intepret this statement? I understand until Offset, from Resize onwards i have no idea what it is doing. Appreciate if you can share the logic on this?
Thanks.
 
Upvote 0
well,
let say Cells(1,1) this Range("A1") Right?
Then Cells(1,1).Resize(rows,column) this means to expand the range down number of rows and to the right number of column
eg
VBA Code:
Sub reszier()
    Cells(1, 1).Select
    MsgBox "Cells(1,1)  selected"
    Cells(1, 1).Resize(5, 4).Select
    MsgBox " from Cells(1,1) 5 rows down and 4 coulumns right selected"
End Sub
 
Upvote 0
well,
let say Cells(1,1) this Range("A1") Right?
Then Cells(1,1).Resize(rows,column) this means to expand the range down number of rows and to the right number of column
eg
VBA Code:
Sub reszier()
    Cells(1, 1).Select
    MsgBox "Cells(1,1)  selected"
    Cells(1, 1).Resize(5, 4).Select
    MsgBox " from Cells(1,1) 5 rows down and 4 coulumns right selected"
End Sub
Ah i see it is similar to dragging the cursor from cell (1,1) and dragging it 5 rows down and 4 columns right. How about the Ubound(x) statement? In this case X = column B which is required to be split?
 
Upvote 0

Forum statistics

Threads
1,215,650
Messages
6,126,014
Members
449,280
Latest member
Miahr

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