Copying from one sheet and pasting a parsed result into another sheet with new duplicate rows

theloveofwisdom

New Member
Joined
Nov 20, 2015
Messages
10
Salutations!!


I'm looking for a solution to this problem in vba. I'd like to take data from the chart below which is in sheet 1....





First NameLast NameNumber of TicketsTicket Numbers
Anniesmith11
Marijones22;3
Stevesmith34;5;6
Nickjones47;8;9;10
Nicksmith511;12;13;14;15

<tbody>
</tbody>




..... and parse the delimiter and paste it as such into sheet 2 as below.:confused:



First NameLast NameNumber of TicketsTicket Numbers
Anniesmith11
Marijones22
Marijones23
Stevesmith34
Stevesmith35
Stevesmith36
Nickjones47
Nickjones48
Nickjones49
Nickjones410
Nicksmith511
Nicksmith512
Nicksmith513
Nicksmith514
Nicksmith515

<tbody>
</tbody>


Any input would be much appreciated!


Godspeed.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Assuming headings already exist on Sheet2 but anything else on that sheet can be deleted to receive the new results.

Test in a copy of your workbook.

Rich (BB code):
Sub Rearrange()
  Dim a, b
  Dim i As Long, j As Long, k As Long
  
  With Sheets("Sheet1")
    a = .Range("A2", .Range("D" & .Rows.Count).End(xlUp)).Value
  End With
  ReDim b(1 To 4, 0 To 0)
  For i = 1 To UBound(a)
    ReDim Preserve b(1 To 4, 1 To UBound(b, 2) + a(i, 3))
    For j = 1 To a(i, 3)
      k = k + 1
      b(1, k) = a(i, 1)
      b(2, k) = a(i, 2)
      b(3, k) = a(i, 3)
      b(4, k) = Split(a(i, 4), ";")(j - 1)
    Next j
  Next i
  With Sheets("Sheet2")
    .UsedRange.Offset(1).ClearContents
    .Range("A2:D2").Resize(UBound(b, 2)).Value = Application.Transpose(b)
  End With
End Sub
 
Upvote 0
theloveofwisdom,

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

2. Are you using a PC or a Mac?

Here is a macro for you to consider that uses two arrays in memory, and, will adjust the varying number of raw data rows in Sheet1.

Sample raw data:


Excel 2007
ABCD
1First NameLast NameNumber of TicketsTicket Numbers
2Anniesmith11
3Marijones22;3
4Stevesmith34;5;6
5Nickjones47;8;9;10
6Nicksmith511;12;13;14;15
7
Sheet1


After the macro in Sheet2:


Excel 2007
ABCD
1First NameLast NameNumber of TicketsTicket Numbers
2Anniesmith11
3Marijones22
4Marijones23
5Stevesmith34
6Stevesmith35
7Stevesmith36
8Nickjones47
9Nickjones48
10Nickjones49
11Nickjones410
12Nicksmith511
13Nicksmith512
14Nicksmith513
15Nicksmith514
16Nicksmith515
17
Sheet2


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, 11/21/2015, ME903910
Dim a As Variant, i As Long, lr As Long
Dim o As Variant, j As Long
Dim s, t As Long, n As Long
Application.ScreenUpdating = False
With Sheets("Sheet1")
  lr = .Cells(Rows.Count, 1).End(xlUp).Row
  a = .Range("A2:D" & lr).Value
  n = Evaluate("=Sum(C2:C" & lr & ")")
  ReDim o(1 To n, 1 To 4)
End With
For i = LBound(a, 1) To UBound(a, 1)
  If InStr(a(i, 4), ";") Then
    s = Split(a(i, 4), ";")
    For t = LBound(s) To UBound(s)
      j = j + 1
      o(j, 1) = a(i, 1): o(j, 2) = a(i, 2): o(j, 3) = a(i, 3)
      o(j, 4) = s(t)
    Next t
  Else
    j = j + 1
    o(j, 1) = a(i, 1): o(j, 2) = a(i, 2)
    o(j, 3) = a(i, 3): o(j, 4) = a(i, 4)
  End If
Next i
With Sheets("Sheet2")
  .Columns("A:D").ClearContents
  With .Range("A1:D1")
    .Value = Sheets("Sheet1").Range("A1:D1").Value
    .Font.Bold = True
  End With
  .Cells(2, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns("A:D").AutoFit
  .Activate
End With
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, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the ReorgData macro.
 
Upvote 0
theloveofwisdom,

Thanks for the feedback.

You are very welcome. Glad we could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,216,523
Messages
6,131,151
Members
449,626
Latest member
Stormythebandit

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