Need VBA to arrange data in cells using delimiters? I'm stumped.

andyfox1979

New Member
Joined
Dec 5, 2012
Messages
32
So I have some data exported from an ecommerce store. Tabe delimited file. I've created two delimiters, ^ and ##:

^ is before each order number. This indicates notes are over, or new order number

## indicates notes begin. If there is nothing after this across the row (say, 5 rows over) then notes are empty, output should just be order number and blank notes.

Want to join all associated notes after the ## and 5 rows across and before the next ^ in a cell below--- join all of this in a single corresponding cell on an seperate output page.

Example Data


OrderNotesNotes2
^485 ## 2004 Jun 26 18:51: Marked Cancelled
> 2004 Jun 26 18:51: Cancelled; Reason: Customer changed mind
> 2004 Jul 6 10:41: Modified by Wayland
^486## CancelledByTom
^487##

<tbody>
</tbody>

Desired Output

OrderNotesNotes2
4852004 Jun 26 18:51: Marked Cancelled by Wayland > 2004 Jun 26 18:51: Cancelled; Reason: Customer changed mind > 2004 Jul 6 10:41: Modified by Wayland
486Cancelled By Tom
487

<tbody>
</tbody>


Any hints on where to go from here would be greatly appreciated!
 
Give this a try in a copy of your workbook.
It assumes ..
- Data is on 'Sheet1' in columns A:F.
- Results go on 'Sheet2'.
- 'Sheet2' already exists but any data on it can be removed.

Rich (BB code):
Sub ArrangeData()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim Order As String, Notes As String
  
  With Sheets("Sheet1")
    a = .Range("A1", .Range("A" & .Rows.Count).End(xlUp).Offset(1)).Resize(, 6).Value
  End With
  ReDim b(1 To UBound(a), 1 To 2)
  a(UBound(a), 1) = "^##"
  Order = a(1, 1): Notes = a(1, 2)
  For i = 2 To UBound(a)
    If Left(a(i, 1), 1) = "^" Then
      k = k + 1: b(k, 1) = Order: b(k, 2) = Application.Trim(Notes)
      Order = Trim(Mid(Left(a(i, 1), InStr(1, a(i, 1), "#") - 1), 2))
      Notes = Split(Join(Application.Index(a, i, 0)), "##")(1)
    Else
      Notes = Notes & Mid(Join(Application.Index(a, i, 0)), 2)
    End If
  Next i
  With Sheets("Sheet2")
    .UsedRange.ClearContents
    With .Range("A1").Resize(k, 2)
      .Value = b
      .Columns.AutoFit
    End With
  End With
End Sub


Sample Data & Results

Excel Workbook
ABC
1OrderNotesNotes2
2^485 ## 2004 Jun 26 18:51: Marked Cancelled
3> 2004 Jun 26 18:51: Cancelled; Reason: Customer changed mind
4> 2004 Jul 6 10:41: Modified by Wayland
5^486## CancelledByTom
6^487##
Sheet1




Excel Workbook
AB
1OrderNotes
24852004 Jun 26 18:51: Marked Cancelled 2004 Jun 26 18:51: Cancelled; Reason: Customer changed mind 2004 Jul 6 10:41: Modified by Wayland
3486Cancelled By Tom
4487
Sheet2
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
In your sample file I ..
- Renamed the 'sample' sheet to 'Sheet1'
- Added a blank sheet and named it 'Sheet2'
- Ran the code exactly as posted in post #11

The result was that the code ran without error & the first part of the results are shown below.

Excel Workbook
AB
1OrderNotes
24852004 Jun 26 18:51: Marked Cancelled by Wayland 2004 Jun 26 18:51: Cancelled; Reason: Customer changed mind 2004 Jul 6 10:41: Modified by Wayland
3486372883268032017, Exp. 8/2005"
4487
5488
6489
7490
8491
94922004 Jul 2 08:56: Marked OK by Wayland 2004 Jul 2 08:56: Marked OK:
10493
11494
124952004 Jul 6 08:12: Marked OK by Wayland 2004 Jul 6 08:12: Marked OK:
13496
Sheet2




Comments:
1. You originally talked about having data split across several (5 or 6?) columns. Your sample had nothing outside of column A. Which scenario is your real situation?

2. "My data starts on A2, i switched that." But you have headings in row 1. My code was using that fact so I suggest you switch it back to A1 again - unless you sample file is not realistic.

3. Run the code again on your real data. When you get the error, click 'Debug' then hover your cursor over the variable "i" and see what number appears in the pop-up box. This is the row number on the sheet where the problem is (assuming the change back to row 1 mentioned above). Have a look at what is in that row. If a problem is not obvious, please post that data from that row in a table like you did in post #1. Copy/Paste the data from your sheet, don't manually type it into your post.
 
Upvote 0
Peter I really appreciate your help and patience.

To simplify things, I joined any text in the other columns to column A, so now there's only one column with data that needs to be extracted/rearranged.

Essentially order number is in between ^ and ##. After ## is notes that I want to put into corresponding cell on sheet 2, column B (Notes). Sometimes there are several rows of notes. See below in red.

Here's a sample of my data:


Order & Notes
^300000 ##
^300001 ##
^300002 ##
^300003 ## eb-address was approved by google. 10/28/13"
^300004 ##
^300005 ## th-10/28/13-cancelled/discontinued. refunded and informed customer.
> 2013 Oct 28 08:30: Marked Cancelled by Travis
> 2013 Oct 28 08:30: Cancelled; Reason: Could not be fulfilled
Credit 230.88
Card xxxxxxxxxxxx8579 xx/xxxx
Result OK
Details Invoice = 2627
> 2013 Oct 28 08:32: Modified by Travis"
^300006 ##
^300007 ##
^300008 ##
^300009 ## Marked OK by Travis
^300010 ##
^300011 ##
^300012 ##



<colgroup><col></colgroup><tbody>
</tbody>

On Page Two I'd like my output to be:

Orders Notes
300000
300001
300002
300003eb-address was approved by google. 10/28/13"
300004
300005th-10/28/13-cancelled/discontinued. refunded and informed customer.&"> 2013 Oct 28 08:30: Marked Cancelled by Travis > 2013 Oct 28 08:30: Cancelled; Reason: Could not be fulfilled Credit 230.88 Card xxxxxxxxxxxx8579 xx/xxxx Result OK Details Invoice = 2627 > 2013 Oct 28 08:32: Modified by Travis"
300006
300007
300008
300009Marked OK by Travis
300010
300011
300012

<colgroup><col><col></colgroup><tbody>
</tbody>
 
Last edited by a moderator:
Upvote 0
First, I have removed the final comment from your post. Refer to #5 of the Forum Rules.

Try
Code:
Sub ArrangeData_v2()
  Dim a As Variant, b As Variant
  Dim i As Long, k As Long
  Dim Order As String, Notes As String
  
  With Sheets("Sheet1")
    a = .Range("A1", .Range("A" & .Rows.Count).End(xlUp).Offset(1)).Value
  End With
  ReDim b(1 To UBound(a), 1 To 2)
  a(UBound(a), 1) = "^##"
  Order = "Order": Notes = "Notes"
  For i = 2 To UBound(a)
    If Left(a(i, 1), 1) = "^" Then
      k = k + 1: b(k, 1) = Order: b(k, 2) = Application.Trim(Notes)
      Order = Trim(Mid(Left(a(i, 1), InStr(1, a(i, 1), "#") - 1), 2))
      Notes = Mid(a(i, 1), InStr(1, a(i, 1), "#") + 3)
    Else
      Notes = Notes & " " & a(i, 1)
    End If
  Next i
  With Sheets("Sheet2")
    .Columns("A:B").ClearContents
    With .Range("A1").Resize(k, 2)
      .Value = b
      .Columns.AutoFit
    End With
  End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,170
Messages
6,129,277
Members
449,498
Latest member
Lee_ray

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