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....





[TABLE="width: 610"]
<tbody>[TR]
[TD][/TD]
[TD]First Name[/TD]
[TD]Last Name[/TD]
[TD]Number of Tickets[/TD]
[TD]Ticket Numbers[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Annie[/TD]
[TD]smith[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Mari[/TD]
[TD]jones[/TD]
[TD]2[/TD]
[TD]2;3[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Steve[/TD]
[TD]smith[/TD]
[TD]3[/TD]
[TD]4;5;6[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Nick[/TD]
[TD]jones[/TD]
[TD]4[/TD]
[TD]7;8;9;10[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Nick[/TD]
[TD]smith[/TD]
[TD]5[/TD]
[TD]11;12;13;14;15[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]




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



[TABLE="width: 513"]
<tbody>[TR]
[TD]First Name[/TD]
[TD]Last Name[/TD]
[TD]Number of Tickets[/TD]
[TD]Ticket Numbers[/TD]
[/TR]
[TR]
[TD]Annie[/TD]
[TD]smith[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]Mari[/TD]
[TD]jones[/TD]
[TD]2[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]Mari[/TD]
[TD]jones[/TD]
[TD]2[/TD]
[TD]3[/TD]
[/TR]
[TR]
[TD]Steve[/TD]
[TD]smith[/TD]
[TD]3[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]Steve[/TD]
[TD]smith[/TD]
[TD]3[/TD]
[TD]5[/TD]
[/TR]
[TR]
[TD]Steve[/TD]
[TD]smith[/TD]
[TD]3[/TD]
[TD]6[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]jones[/TD]
[TD]4[/TD]
[TD]7[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]jones[/TD]
[TD]4[/TD]
[TD]8[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]jones[/TD]
[TD]4[/TD]
[TD]9[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]jones[/TD]
[TD]4[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]smith[/TD]
[TD]5[/TD]
[TD]11[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]smith[/TD]
[TD]5[/TD]
[TD]12[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]smith[/TD]
[TD]5[/TD]
[TD]13[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]smith[/TD]
[TD]5[/TD]
[TD]14[/TD]
[/TR]
[TR]
[TD]Nick[/TD]
[TD]smith[/TD]
[TD]5[/TD]
[TD]15[/TD]
[/TR]
</tbody>[/TABLE]


Any input would be much appreciated!


Godspeed.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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,223,099
Messages
6,170,112
Members
452,302
Latest member
TaMere

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