First, thanks for helping! I am learning how to use VBA and having fun doing so, but now I've hit a bump, I hope your expertise can aid me in my time of need.
Background: I have data in a table, where I compare two columns J and T.
If these values are equal, then copy that row, row i, into the sheet which has the name of the cells just checked, Cell(i, J) or Cell (i, T).
If these values are not equal, then copy that row, row i, into the sheet which has the name of the cells just checked, Cell(i, J) and Cell (i, T).
The values which J and T can take include A2B, APL, BGF, CMA, among others (see code).
Example: Compare J2 and T2,
Suppose J2=T2=BGF then copy row 2 and paste into sheet(BGF)
Next, compare J3 and T3
Suppose J3=BGF and T3=CMA, copy row 3 and paste into sheet(BGF) and sheet(CMA)
CODE
Sub Sortdata()
'step 1 clear all data
Sheets("A2B").Cells.ClearContents
Sheets("APL").Cells.ClearContents
Sheets("BGF").Cells.ClearContents
Sheets("CMA").Cells.ClearContents
Sheets("K Line").Cells.ClearContents
Sheets("MacAndrews").Cells.ClearContents
Sheets("Maersk").Cells.ClearContents
Sheets("OOCL").Cells.ClearContents
Sheets("OPDR").Cells.ClearContents
Sheets("Samskip").Cells.ClearContents
Sheets("Unifeeder").Cells.ClearContents
' Look at rows J and T, if the values are equal then copy row i to sheet "Cell( i , J )".
' Else, copy row i to sheet(Cell( i , J )) and sheet(Cell ( i , T )) [Value.Ji and Value.Ti]
Sheets("All Data").Select
Dim i As Integer
For i = 2 To 10000
If Worksheets("All Data").Range("J" & i) = Worksheets("All Data").Range("T" & i) Then 'if two cells are equal
Worksheets("All Data").Rows(i).Select 'then select the row
Selection.Copy
Worksheets("All Data").Rows(i).Copy 'copy the data
Worksheets(Sheets("All Data").Range("J" & i).Value).End(xlUp).Select 'open the new worksheet using the cell value Ji as the sheet name
Selection.Offset(1, 0).Select
ActiveSheet.PasteSpecial xlPasteValuesAndNumberFormats ' paste the value at the end of the row.
Else
Worksheets("All Data").Rows(i).Select
Selection.Copy
Worksheets("All Data").Rows(i).Copy
Worksheets(Sheets("All Data").Range("J" & i).Value).End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.PasteSpecial xlPasteValuesAndNumberFormats
Worksheets("All Data").Rows(i).Select
Selection.Copy
Worksheets("All Data").Rows(i).Copy
Worksheets(Sheets("All Data").Range("T" & i).Value).End(xlUp).Select
Selection.Offset(1, 0).Select
ActiveSheet.PasteSpecial xlPasteValuesAndNumberFormats
End If
Next i
End Sub