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
Bookmarks