Hi Luke - here's the modified code to cater for however many columns of data you have. I'm afraid I can't guess why you got the dataoption1:= error .. anyone? In any case, it's not necessary, so I've removed it.
Sub CompareAndSort()
Dim cel As Range
Dim lngOriginalRows As Long
Dim intOriginalCols As Integer
Dim lngMaxPossibleRows As Long
Dim i As Long
Dim j As Integer
Dim k As Integer
Dim l As Integer
Dim strValue As String
Dim lngCurrentRow As Long
Dim booAllFilled As Boolean
Dim intSheetRef As Integer
intSheetRef = ActiveSheet.Index
lngOriginalRows = Range("A1").CurrentRegion.Rows.Count
intOriginalCols = Range("A1").CurrentRegion.Columns.Count
lngMaxPossibleRows = (lngOriginalRows - 1) * intOriginalCols
Application.ScreenUpdating = False
Range(Cells(2, 1), Cells(lngOriginalRows, intOriginalCols)).Select
For Each cel In Selection
If cel.Value <> Empty Then
i = i + 1
Cells(i, intOriginalCols + 2).Value = Format(cel.Value, 0)
End If
Next cel
lngMaxPossibleRows = i
Cells(1, intOriginalCols + 2).EntireColumn.Sort Key1:=Range(Cells(1, intOriginalCols + 2), Cells(lngMaxPossibleRows, intOriginalCols + 2)), header:=xlNo
For i = 1 To lngMaxPossibleRows
If IsNumeric(Cells(i, intOriginalCols + 2).Value) Then
'nothing:
Else
'first text instance found:
Range(Cells(1, intOriginalCols + 2), Cells(i - 1, intOriginalCols + 2)).Cut Destination:=Cells(lngMaxPossibleRows, intOriginalCols + 2)
GoTo TextFound
End If
Next i
TextFound:
Range(Cells(1, intOriginalCols + 2), Cells(i - 1, intOriginalCols + 2)).Delete shift:=xlUp
Worksheets.Add after:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = Worksheets(intSheetRef).Name & "-SORT"
j = 1
For i = 1 To lngMaxPossibleRows
Worksheets(intSheetRef).Activate
strValue = Cells(i, intOriginalCols + 2).Value
strNextValue = Cells(i + 1, intOriginalCols + 2).Value
If strNextValue = strValue Then
'nothing.
Else
Sheets(intSheetRef).Range(Cells(j, intOriginalCols + 2), Cells(i, intOriginalCols + 2)).Copy
Sheets(Sheets.Count).Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
ActiveCell.Offset(1, 0).Select
Application.CutCopyMode = False
j = i + 1
End If
Next i
Sheets(intSheetRef).Cells(1, intOriginalCols + 2).EntireColumn.Delete
k = Sheets(Sheets.Count).Range("A1").CurrentRegion.Rows.Count
l = Sheets(Sheets.Count).Range("A1").CurrentRegion.Columns.Count
Sheets(Sheets.Count).Activate
For i = 1 To k 'for each row
booAllFilled = True
For j = 1 To l 'for each col
If Cells(i, j).Value = Empty Then
Cells(i, j).Interior.Color = vbBlack
booAllFilled = False
End If
Next j
If booAllFilled = True Then Range(Cells(i, 1), Cells(i, j - 1)).Interior.Color = vbGreen
Next i
Application.ScreenUpdating = True
Sheets(Sheets.Count).Range("A1").EntireRow.Insert shift:=xlDown
Sheets(intSheetRef).Range("A1").EntireRow.Copy Destination:=Range("A1")
End Sub
I've also attached the workbook I did the testing in with your data. As I mentioned in the original, it's not a particularly elegant solution and is actually a bit inefficient, but I hope it helps.
Cheers, MM.
Bookmarks