Data Text to Column

decent_boy

Board Regular
Joined
Dec 5, 2014
Messages
130
Office Version
  1. 2016
Platform
  1. Windows
Hi
I copy below data from a text file and issue is that when I convert/copy multiple rows to Result Sheet, it takes too much time. Please provide any VBA solution to save time

Date Copied from Text File
Sheet1


A
1Order No = A25/CP/0123/00333
2Order Amount = 12351
3SH_ID = TA5
4
5-------------------------------------------------------------------------------------------------------------------------------------------------------------
6| SP |Type |Denomination |From |To | Quantity| |
7-------------------------------------------------------------------------------------------------------------------------------------------------------------
8|DS1|EU |ITEM-232 |0101250300225100 |0101250300225199 | 100|SMITH |
9|DS1|EU |ITEM-324 |0600528600210000 |0600528600210199 | 200|SMITH |
10|DS1|EU |ITEM-524 |0005084901091890 |0005084901091919 | 30|SMITH |
11
12Order No = A32/CP/0152/00352
13Order Amount = 726
14SH_ID = TA9
15
16-------------------------------------------------------------------------------------------------------------------------------------------------------------
17| SP |Type |Denomination |From |To | Quantity| |
18-------------------------------------------------------------------------------------------------------------------------------------------------------------
19|DS2|EU |ITEM-232 |0101250300225200 |0101250300225299 | 100|JHON |
20
21Order No = A33/CP/0192/00395
22Order Amount = 8252
23SH_ID = TA2
24
25-------------------------------------------------------------------------------------------------------------------------------------------------------------
26| SP |Type |Denomination |From |To | Quantity| |
27-------------------------------------------------------------------------------------------------------------------------------------------------------------
28|DS3|EU |ITEM-232 |0101250300225300 |0101250300225399 | 100|Phenny |
29|DS3|EU |ITEM-324 |0600528600210200 |0600528600210399 | 200|Phenny |

Required result after removing unnecessary space

Result


ABCDEFG
1DateSH_IDOrder NoQuantityDenominationFrom To
220-May-20TA5 A25/CP/0123/00333100ITEM-23201012503002251000101250300225199
320-May-20TA5 A25/CP/0123/00333200ITEM-32406005286002100000600528600210199
420-May-20TA5 A25/CP/0123/0033330ITEM-5240005084901091890 0005084901091919
520-May-20TA9A32/CP/0152/00352100ITEM-23201012503002252000101250300225299
620-May-20TA2A33/CP/0192/00395100ITEM-23201012503002253000101250300225399
720-May-20TA2A33/CP/0192/00395200ITEM-32406005286002102000600528600210399


Excel tables to the web >> Excel Jeanie HTML 4
 
It is not surprising that my earlier code does not work with that data - it is entirely different!

For those two sample rows, this works for me.

VBA Code:
Sub Rearrange_v2()
  Dim a As Variant, b As Variant
  Dim RX As Object, M As Object
  Dim OrdNum As String, FranchiseID As String
  Dim i As Long, k As Long

  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = "(DS3\|.*?\|)([^\|]+?)(\|)([^\|]+)(\|)([^\|]+)(\|)([^\|]+)(\|)"
  With Sheets("Sheet2")
    a = .Range("A1", .Range("A" & Rows.Count).End(xlUp)).Value
  End With
  ReDim b(1 To UBound(a) * 100, 1 To 6)
  For i = 1 To UBound(a)
    FranchiseID = Trim(Split(Split(Split(a(i, 1), "Franchise ID")(1), vbLf)(0), "=")(1))
    OrdNum = Trim(Split(Split(a(i, 1), vbLf)(0), "=")(1))
    For Each M In RX.Execute(a(i, 1))
      k = k + 1
      b(k, 1) = FranchiseID
      b(k, 2) = OrdNum
      b(k, 3) = Val(Trim(M.SubMatches(7)))
      b(k, 4) = Trim(M.SubMatches(1))
      b(k, 5) = Trim(M.SubMatches(3))
      b(k, 6) = Trim(M.SubMatches(5))
    Next M
  Next i
  With Sheets("Sheet3").Range("A2").Resize(k, 7)
    .Rows(0).Value = Array("Date", "Franchise ID", "Order No", "Quantity", "Denomination", "From", "To")
    .Columns(1).Value = Date
    With .Offset(, 1).Resize(, 6)
      .Columns(5).Resize(, 2).NumberFormat = "@"
      .Value = b
    End With
    .EntireColumn.AutoFit
  End With
End Sub
 
Upvote 0
Thanks for your reply
I have not checked it yet but my question is that if following line next row is DS4 or DS5 or DS6 then won't it work ?
RX.Pattern = "(DS3\|.*?\|)([^\|]+?)(\|)([^\|]+)(\|)([^\|]+)(\|)([^\|]+)(\|)"
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I have checked now and when I changed DS3 into DS4, code has not copied data of DS4
I would like to say that this line can be different
Maybe be start with DS or UF or QT
 
Upvote 0

Similar threads

Forum statistics

Threads
1,215,637
Messages
6,125,963
Members
449,276
Latest member
surendra75

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