Excel Macro to copy data in cell to multiple cells in the same row

xlstarter

New Member
Joined
Sep 23, 2014
Messages
2
Hi I am a beginner in VBA scripting but I am working on a sheet where I want to copy data in one cell to multiple cells in the same row as long as it is not empty but separate the two data by a comma and do this through out the table

Before Macro

ABCD

<tbody>
</tbody>
ad
bd
cdp
deo
efgl
fexf

<tbody>
</tbody>

Here is what I mean: I want the data in Column A (if there is data) to be duplicated through the row separated by a comma and possibly delete column A. I tried to use do-while loop with a formula but my issue is that the sheet is not fixed, meaning the length of rows and columns might vary depending on when the report is generated and I do this task weekly, some help in automating this will be a great help.

After Macro

<s>A</s>BCD
<s>a</s>a,d
<s>b</s>b,d
<s>c</s>c,dc,p
<s>d</s>d,ed,o
<s>e</s>e,fe,ge,l
<s>f</s>f,ef,xf,f

<tbody>
</tbody>
 
Last edited:

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Try this:

Code:
Option Explicit


Sub xlstarter()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Dim lc As Long
Dim i As Long
Dim j As Long


For i = 1 To lr
lc = Cells(i, Columns.Count).End(xlToLeft).Column
    For j = 2 To lc
    Cells(i, j) = Cells(i, 1) & "," & Cells(i, j)
    Next j
Next i
MsgBox "complete"






End Sub
 
Upvote 0
Try this:

Code:
Option Explicit


Sub xlstarter()
Dim lr As Long
lr = Range("A" & Rows.Count).End(xlUp).Row
Dim lc As Long
Dim i As Long
Dim j As Long


For i = 1 To lr
lc = Cells(i, Columns.Count).End(xlToLeft).Column
    For j = 2 To lc
    Cells(i, j) = Cells(i, 1) & "," & Cells(i, j)
    Next j
Next i
MsgBox "complete"






End Sub


Thank you very much, works!!! You are the best!
 
Upvote 0
xlstarter,

Welcome to the MrExcel forum.

1. What version of Excel and Windows are you using?

2. Are you using a PC or a Mac?


The below macro will adjust for a varying number of rows, and, columns.

The screenshot of after the macro is not displaying the strikethrough in column A.

Sample raw data:


Excel 2007
ABCD
1ad
2bd
3cdp
4deo
5efgl
6fexf
7
Sheet1


After the macro using an array in memory:


Excel 2007
ABCD
1aa,d
2bb,d
3cc,dc,p
4dd,ed,o
5ee,fe,ge,l
6ff,ef,xf,f
7
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub ReorgData()
' hiker95, 09/23/2014, ME807385
Dim a As Variant, i As Long
Dim lr As Long, lc As Long, c As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
lc = Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
a = Range(Cells(1, 1), Cells(lr, lc))
For i = 1 To lr
  For c = 2 To lc
    If a(i, c) <> "" Then
      a(i, c) = a(i, 1) & "," & a(i, c)
    End If
  Next c
Next i
Range(Cells(1, 1), Cells(lr, lc)) = a
Range(Cells(1, 1), Cells(lr, 1)).Font.Strikethrough = True
Columns(1).Resize(, lc).AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the ReorgData macro.
 
Upvote 0

Forum statistics

Threads
1,216,774
Messages
6,132,649
Members
449,740
Latest member
Stevejhonsy

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