Raw data--cleansing macro?

tc88

Board Regular
Joined
Jul 6, 2011
Messages
80
Hello all-<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
This is my first post here, but I have been browsing for the past couple of weeks at my new job. I am an intern in the accounting/finance department at a publicly traded company here in the US and we are currently working on reducing waste in business processes. One of those processes revolves around exporting data from SAP into Excel, fixing some of the data, and then removing irrelevant data. I am using Excel 2010 on Windows XP SP 3.
The first step I have set up is to check if any of the data needs to be reformatted (it is missing a “0” in the middle of the number). I have the formula as follows, and it works correctly (Column M in dummy data below)
Code:
=IF(OR(LEFT(C2,6)="104001",LEFT(C2,6)="104003",LEFT(C2,6)="104004"),CONCATENATE("CTR ",LEFT(C2,2),"0",MID(C2,3,4)),D2)
<o:p></o:p>

From there, I use a nested IF based on the column for the above formula. That formula is as follows (Column N in dummy data below)
Code:
=IF(OR(M2="CTR 1004001",M2="CTR 1004003",M2="CTR 1004004"),M2,IF(AND(RIGHT(D2,5)>="12475",RIGHT(D2,5)<="12739"),D2,"IRRELEVANT"))
<o:p></o:p>

I have that column conditionally formatted to show “IRRELEVANT” with a red fill, and up to this point I have absolutely no problems and the formulas appear to work correctly. However, from there, I must manually delete all “IRRELEVANT” rows to get out the data for our specific facility. If this were a few rows it wouldn’t be a big deal but the raw data is ~2000 rows and my manually cleansed data is ~600 rows, so as you can see I am manually clearing out quite a bit of data.<o:p></o:p>
This is a process that is done monthly, and while I believe a macro is the best way to do this, I don’t have any experience writing macros/using VBA. I consider myself a strong Excel user, but I am not an expert.<o:p></o:p>

Below is some dummy data so you have a better idea of what I am talking about:

Excel Workbook
BCDEFGHIJKLMN
1Cost ElementNameAuxAcctAs1Name of offsetting accountOffst.acctOrderOffActCoCdVal.in rep.cur.Postg DateCreated onFormatting FixRelevant?
2Facilities10357E EN DUMMY DATA13967010009,956.0005/31/201106/02/2011IRRELEVANT
3Facilities104004-PRJ DUMMY DATA15629510002,645.0005/31/201106/01/2011CTR 1004004CTR 1004004
4Fringe10384 BD DUMMY DATA115746100021.5605/31/201106/02/2011IRRELEVANT
5Fringe10436 RD DUMMY DATA1157461000545.2105/31/201106/02/2011IRRELEVANT
6GeneralJCTE000204193CTR 12708A/P AMCL210036154710S10005.8905/04/201105/05/2011CTR 12708CTR 12708
7GeneralJCTE000204193CTR 1004003A/P AMCL210036156295S100016.6705/04/201105/05/2011CTR 1004003CTR 1004003
8GeneralJCTE000204193CTR 1004003A/P AMCL210036157010S10006.2805/04/201105/05/2011CTR 1004003CTR 1004003
9Other104003_DSTDUMMYDATA1564301000-2,215.6505/31/201106/02/2011CTR 1004003CTR 1004003
10Other12335_DST DISTRDUMMY DATA1569501000-312.4505/31/201106/02/2011IRRELEVANT
Sheet1
Excel 2010
Cell Formulas
RangeFormula
M2=IF(OR(LEFT(C2,6)="104001",LEFT(C2,6)="104003",LEFT(C2,6)="104004"),CONCATENATE("CTR ",LEFT(C2,2),"0",MID(C2,3,4)),D2)
N2=IF(OR(M2="CTR 1004001",M2="CTR 1004003",M2="CTR 1004004"),M2,IF(AND(RIGHT(D2,5)>="12475",RIGHT(D2,5)<="12739"),D2,"IRRELEVANT"))



Thanks in advance for any assistance!
<o:p></o:p>
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to MrExcel.

Can't you just AutoFilter your data for column N equals IRRELEVANT and delete the visible rows?
 
Upvote 0
Welcome to MrExcel.

Can't you just AutoFilter your data for column N equals IRRELEVANT and delete the visible rows?

Thanks for your response. That occurred to me as well but they appear to want to automate things when possible, as this is just one task in a list of tasks for the end of month close process. If the time it takes to write a macro exceeds the benefit relative to just using AutoFilter, then I will make that case.
 
Upvote 0
Why not put the autofilter in a macro:

See post #4 here:

http://www.xtremevbtalk.com/showthread.php?t=300757

As I said, I have no experience writing VBA so that might as well be Greek to me! I appreciate the link though, I am meeting with the local "Excel guru" early next week to discuss this further so maybe he will have an idea of how to tailor that to my specific needs.

If it is possible to put AutoFilter in a macro, could there be a macro that does the following basic steps?

1. Insert/autofill formula in column M to the last row
2. Insert/autofill formula in Column N to last row
3. Autofilter to show "IRRELEVANT" data
4. Delete all rows with "IRRELEVANT" in column N
 
Upvote 0
You should be able to use the macro recorder to get most of that code. You will need to change the AutoFilter code to offset by one row and use SpecialCells(xlCellTypeVisible). That's covered in Colin Legg's code in the link that was posted.
 
Upvote 0
On a copy of your workbook, try this:

Code:
Sub Foo()

Dim lr As Long
Dim rng As Range

Application.ScreenUpdating = False

lr = Cells.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set rng = Range("B1:N" & lr)

With rng
    .AutoFilter Field:=13, Criteria1:= _
        "IRRELEVANT"
            On Error Resume Next
    .Offset(1, 0).Resize(.Rows.Count - 1, 1). _
            SpecialCells(xlCellTypeVisible).EntireRow.Delete
        On Error GoTo 0

    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub

Also, I would qualify the range reference fully, meaning it should apply to a specific sheet and not just whatever range of the active sheet which is how it's currently set up. What's the sheet name where you'd like the macro to run?
 
Upvote 0
On a copy of your workbook, try this:

Code:
Sub Foo()
 
Dim lr As Long
Dim rng As Range
 
Application.ScreenUpdating = False
 
lr = Cells.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set rng = Range("B1:N" & lr)
 
With rng
    .AutoFilter Field:=13, Criteria1:= _
        "IRRELEVANT"
            On Error Resume Next
    .Offset(1, 0).Resize(.Rows.Count - 1, 1). _
            SpecialCells(xlCellTypeVisible).EntireRow.Delete
        On Error GoTo 0
 
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub

Also, I would qualify the range reference fully, meaning it should apply to a specific sheet and not just whatever range of the active sheet which is how it's currently set up. What's the sheet name where you'd like the macro to run?

It's just a default "Sheet1" right now. I will try this out in a bit, thanks!
 
Upvote 0
OK, try this then:

Code:
Sub Foo()

Dim lr As Long
Dim rng As Range

Application.ScreenUpdating = False

lr = Sheet1.Cells.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set rng = Sheet1.Range("B1:N" & lr)

With rng
    .AutoFilter Field:=13, Criteria1:= _
        "IRRELEVANT"
            On Error Resume Next
    .Offset(1, 0).Resize(.Rows.Count - 1, 1). _
            SpecialCells(xlCellTypeVisible).EntireRow.Delete
        On Error GoTo 0

    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
OK, try this then:

Code:
Sub Foo()
 
Dim lr As Long
Dim rng As Range
 
Application.ScreenUpdating = False
 
lr = Sheet1.Cells.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
Set rng = Sheet1.Range("B1:N" & lr)
 
With rng
    .AutoFilter Field:=13, Criteria1:= _
        "IRRELEVANT"
            On Error Resume Next
    .Offset(1, 0).Resize(.Rows.Count - 1, 1). _
            SpecialCells(xlCellTypeVisible).EntireRow.Delete
        On Error GoTo 0
 
    .AutoFilter
End With
Application.ScreenUpdating = True
End Sub

That worked great! Thank you so much for your help.

Is it possible to have the macro automatically insert the formulas from Columns M and N before looking for the "IRRELEVANT" and deleting the rows?
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,716
Members
452,939
Latest member
WCrawford

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