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
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Firs thing I would do is just loop through the first column and delete the ----------

I am not pro but you would want something like

Code:
Sub CleanUp()
 
Range("A1").Select

    Do Until ActiveCell.Value = "TOTAL"
        If ActiveCell.Value = "----------------" Then
        ActiveCell.EntireRow.Delete
Else
        If ActiveCell.Value = "=================" Then
        ActiveCell.EntireRow.Delete
    End If
    End If
ActiveCell.Offset(1, 0).Select
Loop
End Sub

That should atleast get you started :)

Shaun
 
Upvote 0
BTW, you want to right click on the sheet tab of your workbook and click on "View Code".
Then you will want to look on the left hand side and find the filename you are working on..Right click on that and insert "Module", and paste that code into the module.

Then go back to your workbook and you will find "cleanup" in your macros

Shaun
 
Upvote 0
Thanks Shaun for the start. It works great except for one little thing. The macro does not stop. I think it is because of the "========" right before the "Total". It deletes the line which places the cursor on the cell with Total, but then the offset command moves the cursor to the next cell below it so the macro continues to run until I ctrl break.
Rick
 
Upvote 0
Sorry...Try this instead (I do that to myself all the time)

Code:
Sub CleanUp()
 
Range("A1").Select
    Do Until ActiveCell.Value = "TOTAL"
        If ActiveCell.Value = "----------------" Then
        ActiveCell.EntireRow.Delete
        Else
        If ActiveCell.Value = "=================" Then
        ActiveCell.EntireRow.Delete
        Else
        ActiveCell.Offset(1, 0).Select
        
    End If
    End If
Loop
End Sub
 
Upvote 0
Thanks, that works. Now I just need to figure the rest out. Any more suggestions would be appreciated.
 
Upvote 0
Sorry, I am not sure I know what you mean. I copy and paste the data from the text file directly into excel. The text file looks like the excel file below, but excel divides the data into columns as shown below automatically when I paste it into excel.
 
Upvote 0
Here is part of it. Is this enough?

"Query Definition"

"Recipient:" "My Name"
"Query:" "salestest (302306-1)"
"Period:" "03/08-02/09"
"Results:" "Volume[Kg],Sales[USD]"
"Break 1:" "By Month"
"Break 2:" "By Product"
"Break 3:" "By Holding"

"Selections:"
"EG only"

"Product list"
"915165" "product a"
"913166" "product b"

"Month" " "
"ProdID" "Product" "Volume[Kg]" "Sales[USD]"
"HdgID" "Holding" "12Mth-02/09" "12Mth-02/09"
"----------------" "--------------------------------" "-------------" "---------------"
"2008/03"
"----------------" "--------------------------------"
"915165" "product a"
"----------------" "--------------------------------"
"106325" "International Essence SA De Cv" 2 1369.04
"109102" "S utive Ltd" 50 27692.95
"111424" "Wes Soya" .5 367.03
"119200" "International Development Limited" 5 2717.2
"123438" "Pizzza Spa" 10 5885.
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,747
Members
448,989
Latest member
mariah3

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