+ Reply to Thread
Results 1 to 2 of 2

Looping issue for csv clean-up macro

Hybrid View

  1. #1
    Registered User
    Join Date
    08-10-2012
    Location
    NY
    MS-Off Ver
    Excel 2010
    Posts
    78

    Looping issue for csv clean-up macro

    Hi All-

    I toiled with getting a macro together to handle an issue I have with line breaks occurring in the middle of a "comments" field in the several csv files I am emailed every week. Often when the data is exported by various users, these breaks cause the fields to be misaligned.

    The code I have works perfectly if I have my cursor on col A of an affected row (in each variation of the problem), but I am having a hard time getting it to loop through line by line. When I run the full macro as is, it winds up deleting much more than intended.

    Here are the facts:

    The break always occurs in column P, resulting in data that should be in columns Q-AQ winds up in the next row, column A or half of the comment winds up in column P below. Because the comments are always split in half, I wrote code to concatenate the data before correcting it with the following steps:

    1.First it needs to delete all completely blank rows
    2.There are only 9 correct entries for column A. As long as the row begins with one of these, it should be left alone. ("HQ (Crew)", "EVANN", "EWRTH", "ERYE", "EEAST", "CWBIB", "CWPER", "CXPER", "CXWJL")
    3.Then apply the fix I have worked out when a line break occurs (shifting, concatenating, moving 2-3 rows of data into one)
    4.Finally I want it to return a message box letting the user know if any cleaning was necessary (which I accomplish by comparing the number of rows at the beginning of the macro and at the end.

    Attached is a shortened sample version of the a problem file. Below is the code I am using.

    I have it pretty thoroughly commented regarding what needs to happen when and where the issues are occuring.

    I really would sincerely appreciate any guidance you might have.

    Thanks,
    J

    Sub CleanData() 
         'FIRST STEP IS TO DELETE ALL BLANK ROWS:
        Dim R As Long 
        Dim C As Range 
        Dim N As Long 
        Dim Rng As Range 
        On Error Goto EndBlankRow 
        Application.ScreenUpdating = False 
        Application.Calculation = xlCalculationManual 
        If Selection.rows.Count > 1 Then 
            Set Rng = Selection 
        Else 
            Set Rng = ActiveSheet.UsedRange.rows 
        End If 
        N = 0 
        For R = Rng.rows.Count To 1 Step -1 
            If Application.WorksheetFunction.CountA(Rng.rows(R).EntireRow) = 0 Then 
                Rng.rows(R).EntireRow.Delete 
                N = N + 1 
            End If 
        Next R 
         ' AFTER BLANKS ARE DELETED RUN THIS CODE ON ALL ROWS THAT DON'T MEET THE FOLLOWING CRITERIA:
         ' IF COLUMN "A" DOES NOT CONTAIN "HQ (Crew)", "EVANN", "EWRTH", "ERYE", "EEAST", "CWBIB", "CWPER", "CXPER", "CXWJL"
    EndBlankRow: 
        Dim dontDelete 
        dontDelete = Array("HQ (Crew)", "EVANN", "EWRTH", "ERYE", "EEAST", "CWBIB", "CWPER", "CXPER", "CXWJL") 
        Dim i As Long, j As Long 
        Dim isThere As Boolean 
         
         ' I PUT THIS LINE TO RETURN THE USER WITH A MESSAGE AT THE END COMPARING THE NUMBER OF ROWS AT THE BEGINNGING VS THE END ALERTING THEM IF ANY CLEANING WAS NECESSARY
        botRow = ActiveSheet.UsedRange.rows.Count 
         
         'THIS IS WHERE I THINK IT GETS MESSED UP.
         '==========================================
        For i = Range("A" & rows.Count).End(xlUp).Row To 1 Step -1 
            For j = LBound(dontDelete) To UBound(dontDelete) 
                If StrComp(Range("A" & i), dontDelete(j), vbTextCompare) = 0 Then 
                    isThere = True 
                End If 
            Next j 
            If Not isThere Then 
                 '==========================================
                 
                 'THIS CODE RUNS PERFECTLY WHEN THE ACTIVE CELL DOES NOT MEET THE "dontDelete" criteria.
                 ' But when combined with the above, it just seems to delete everything that doesn't rather than doing this:
                 
                 'The first condition is that the cell in "A" contains data that incorrectly got a line break and needs to be joined with the previous row
                If IsEmpty(ActiveCell) Then Goto SecondCondit 
                If Not IsEmpty(ActiveCell) Then 
                     'shift 15 cells right
                    Range(ActiveCell, ActiveCell.EntireRow.Cells(1, Columns.Count).End(xlToLeft)).Cut 
                    ActiveCell.Offset(0, 15).Range("A1").Select 
                    ActiveSheet.Paste 
                End If 
                 
                ActiveCell.Offset(1, -15).Range("A1").Select 
                 
                 
                 'check to see if row below also needs to be concatenated if there were 2 line breaks, if it does then:
                 
                If IsEmpty(ActiveCell) Then 
                     
                    ActiveCell.Offset(0, 14).Range("A1").Select 
                     
                    ActiveCell.FormulaR1C1 = "=CONCATENATE(R[-2]C[1],"" "",R[-1]C[1],"" "",RC[1])" 
                    Application.CutCopyMode = False 
                    Selection.Copy 
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
                    :=False, Transpose:=False 
                    Application.CutCopyMode = False 
                     
                    Selection.Copy 
                    ActiveCell.Offset(-2, 1).Range("A1").Select 
                    ActiveSheet.Paste 
                     
                    ActiveCell.Offset(2, 1).Range("A1").Select 
                     
                    Range(ActiveCell, ActiveCell.EntireRow.Cells(1, Columns.Count).End(xlToLeft)).Cut 
                     
                    ActiveCell.Offset(-2, 0).Range("A1").Select 
                    ActiveSheet.Paste 
                     
                    ActiveCell.Offset(1, 0).Range("A1").Select 
                     
                    ActiveCell.EntireRow.Delete 
                End If 
                 
                 'If the row does not need the additional concatenation then:
    
                    If Not IsEmpty(ActiveCell) Then 
                    ActiveCell.Offset(-1, 14).Range("A1").Select 
                     
                    ActiveCell.FormulaR1C1 = "=CONCATENATE(R[-1]C[1],"" "",RC[1])" 
                     
                    Application.CutCopyMode = False 
                    Selection.Copy 
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
                    :=False, Transpose:=False 
                    Application.CutCopyMode = False 
                     
                    Selection.Copy 
                     
                    ActiveCell.Offset(-1, 1).Range("A1").Select 
                    ActiveSheet.Paste 
                    ActiveCell.Offset(1, 1).Range("A1").Select 
                     
                    Range(ActiveCell, ActiveCell.EntireRow.Cells(1, Columns.Count).End(xlToLeft)).Cut 
                     
                    ActiveCell.Offset(-1, 0).Range("A1").Select 
                    ActiveSheet.Paste 
                     
                    ActiveCell.Offset(1, 0).Range("A1").Select 
                     
                    ActiveCell.EntireRow.Delete 
                     
                End If 
    
                 
                 'the second possible condition is that the cell in "A" is empty but there is data in other cells that incorrectly got a line break
    
    SecondCondit: 
                
                If IsEmpty(ActiveCell) Then 
                    ActiveCell.Offset(0, 14).Range("A1").Select 
                    ActiveCell.FormulaR1C1 = "=CONCATENATE(R[-1]C[1],"" "",RC[1])" 
                    Application.CutCopyMode = False 
                    Selection.Copy 
                    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
                    :=False, Transpose:=False 
                    Application.CutCopyMode = False 
                     
                    Selection.Copy 
                     
                    ActiveCell.Offset(-1, 1).Range("A1").Select 
                    ActiveSheet.Paste 
                    ActiveCell.Offset(1, 1).Range("A1").Select 
                     
                    Range(ActiveCell, ActiveCell.EntireRow.Cells(1, Columns.Count).End(xlToLeft)).Cut 
                     
                    ActiveCell.Offset(-1, 0).Range("A1").Select 
                    ActiveSheet.Paste 
                     
                    ActiveCell.Offset(1, 0).Range("A1").Select 
                     
                    ActiveCell.EntireRow.Delete 
                     
                     
                    Application.ScreenUpdating = True 
                    Application.Calculation = xlCalculationAutomatic 
                End If 
                 
                 
            End If 
             
             
             ' HERE IS WHERE I WANT TO PICK UP FROM WHERE THE PROBLEM BEGAN
            isThere = False 
        Next i 
         ' THIS COMPARES THE ORIGINAL NUMBER OF ROWS TO THE CURRENT AND GIVES THE USER A STATUS MESSAGE
        botRow2 = ActiveSheet.UsedRange.rows.Count 
        If botRow = botRow2 Then Goto AllGood 
        If Not botRow = botRow2 Then Goto AllDone 
    AllGood: 
        MsgBox "Data already appears to be formatted correctly. No cleaning was necessary." 
        Exit Sub 
         
         
    AllDone: 
        MsgBox "Data has been cleaned. You're good to go!" 
        Exit Sub 
         
         
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,962

    Re: Looping issue for csv clean-up macro

    Your code uses Activecell a lot, but you are not selecting the cell of interest first. Just add a line after your looping start line to set the activecell properly - that should fix a lot. Can't say anything about the rest of your logic, but you should be able to sort it out from there.

        For i = Range("A" & Rows.Count).End(xlUp).Row To 1 Step -1
            Range("A" & i).Select
    Bernie Deitrick
    Excel MVP 2000-2010

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Looping vlookup,If,clean cell contens VSA
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-11-2013, 09:44 PM
  2. Looping issue
    By wani in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-30-2012, 02:23 AM
  3. Looping Macro Issue - Infinate Loops
    By frodo987 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2012, 03:39 AM
  4. [SOLVED] VBA Code to optimize and clean data- clean out numerical/ or symbol
    By tracylsr in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-05-2012, 04:21 PM
  5. Need help with looping issue
    By SDBoca in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-08-2011, 08:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1