Using VBA to clean up imported data

capefear

New Member
Joined
Aug 18, 2006
Messages
12
I receive data from an old system that does not format the data in a table format. It takes hours for me to clean up the data so I can use pivot tables, etc. to summarize the data. Does anyone know how to write VBA to do this automatically? I am new to VBA. Doing this manually takes hours. I would really appreciate some help. The data received looks like this:
SSDB example r1.xlsx
ABCD
1Query Definition
2
3Recipient:My name
4Query:salestest (302306-1)
5Period:03/08-02/09
6Results:Volume[Kg],Sales[USD]
7Break 1:By Month
8Break 2:By Product
9Break 3:By Holding
10
11Selections:
12EG only
13
14Product list
15956910ProductA
16987322ProductB
17
18Month
19ProdIDProductVolume[Kg]Sales[USD]
20HdgIDHolding12Mth-02/0912Mth-02/09
21----------------------------------------------------------------------------
222008/03
23------------------------------------------------
24956910ProductA
25------------------------------------------------
26106325Cust A10100
27109102Cust B50500
28111424Cust C12150
29119200Cust D5180
30============================================================================
312008/0377930
32
33
342008/04
35------------------------------------------------
36956910ProductA
37------------------------------------------------
38106325Cust A50200
39109102Cust B10180
40111424Cust C25300
41============================================================================
42956910ProductA85680
43
44987322ProductB
45------------------------------------------------
46134000Cust E110300
47707500Cust F19752000
48============================================================================
49987322ProductB20852300
50
51
52============================================================================
532008/0421702980
54
55
562008/05
57------------------------------------------------
58956910ProductA
59------------------------------------------------
60106325Cust A10300
61109102Cust B16150
62111424Cust C550
63119200Cust D150560
64============================================================================
65956910ProductA1811060
66
67987322ProductB
68------------------------------------------------
69109102Cust B130250
70111424Cust C520
71119200Cust D5701500
72============================================================================
73987322ProductB7051770
74
75
76============================================================================
772008/058662830
78
79============================================================================
80TOTAL31336740
RAW DATA


I would like the data to look this:
SSDB example r1.xlsx
ABCDEF
1DateProdIDCustomer IDCustomerVolume[Kg]Sales[USD]
2Feb-08956910106325Cust A10100
3Feb-08956910109102Cust B50500
4Feb-08956910111424Cust C12150
5Feb-08956910119200Cust D5180
6Apr-08956910106325Cust A50200
7Apr-08956910109102Cust B10180
8Apr-08956910111424Cust C25300
9Apr-08987322134000Cust E110300
10Apr-08987322707500Cust F19752000
11May-08956910106325Cust A10300
12May-08956910109102Cust B16150
13May-08956910111424Cust C550
14May-08956910119200Cust D150560
15May-08987322109102Cust B130250
16May-08987322111424Cust C520
17May-08987322119200Cust D5701500
Table
 
OK
1) Is the text file include quotaion marks ?
2) Can you tell me why Date is Feb-08 in your result, while your txt file shows
"Period:" "03/08-02/09"
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Yes, the text file includes the quotes. The results file is incorrect. It should be Mar-08. Sorry, for that.
 
Upvote 0
Let's see if this works for you.
Rich (BB code):
Sub test()
Dim fn As String, temp As String, x
Dim i As Long, ii As Long, iii As Long
Dim a() As String
fn = "c:\test.txt"   '<- change here (File Path)
temp = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
temp = Replace(Replace(temp, "-", ""), "=", "")
x = Split(temp, vbCrLf)
ReDim a(1 To UBound(x), 1 To 6)
With CreateObject("VBScript.RegExp")
    .IgnoreCase = True
    .Pattern = "^\s*$"
    .MultiLine = True
    temp = .replace(temp, "")
    For i = 0 To UBound(x)
        .Pattern = "\s*""(\d{2}/\d{2})""\s*$"
        .MultiLine = False
        If .test(x(i)) Then
            n = n + 1 : ii = 0
            b(n, 1) = DateValue(.execute(x(i)).(0).submatches(0) & "/1")
            .Pattern = """([^""])+""\s+""[^""]+""\s*$"
            Do
                ii = ii + 1
                If .test(x(i + ii)) Then Exit Do
                If i + ii > UBound(x) Then GoTo myEnd
            Loop
            iii = 0
            ProdID = .execute(x(i + ii))(0).subamtches(0) : iii = 1
            .Pattern = """(\d+)""\s+""([^""])+""\s+(\d*(\.\d+)?)\s+(\d(\.\d+)?)\s*$"
            Do While .test(x(i + ii + iii))
                Set m = .execute(x(i + ii + iii))(0)
                n = n + 1
                a(n, 1) = myDate : a(n, 2) = ProdID
                a(n, 3) = m.submatches(0)
                a(n, 4) = m.submatches(1)
                a(n, 5) = m.submatches(2)
                a(n, 6) = m.submatches(4)
            Loop
            i = i + ii + iii - 1
        End If
    Next
End With
With ThisWorkbook.Sheets(1).Cells(1)
    .Resize(, 6).Value = [{"Date","ProdID","CustomerID","Customer","Volume[Kg]","Sales[USD]"}]
    .Offset(1).Resize(n, 6).Value = a
End With
End Sub
 
Upvote 0
Wow! Thanks! Unfortunately, I received a compile: syntax error when I pasted the code into vba for line:
b(n, 1) = DateValue(.execute(x(i)).(0).submatches(0) & "/1")
 
Upvote 0

Forum statistics

Threads
1,216,089
Messages
6,128,760
Members
449,466
Latest member
Peter Juhnke

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