When it 'jumps to the code' which line is highlighted?
Also does it stop at the same point every time (i.e. the same number of columns have been moved?
Lastly, I had a quick look at the code, and it seems that you are cycling through every cell in the data range (from first row - last row, from columns 1 to the last column), and comparing the values between the two sheets, so not only are you comparing the headings, but every individual data point!
Actually, LASTLY, what is the point of the line "NextCol = ..."? You don't use NextCol anywhere in the code?
My guess is that the error occurs on the comparison line ( "<>" ) and it is a result of you comparing DATA points that are not of the same type, or that contain an error value in the cell.
My suggestion would be to lose the outer loop and cut and paste ranges, rather than individual cells... Something like this (untested):
*** EDIT: Ok, I've determined that your macro doesn't successfully sort the columns anyway (I tested it) In order for it to work, the inner IF should be a WHILE - so that it keeps shuffling the current column to the end until it finds the one it's looking for. Of course if your MASTER sheet contains a column that isn't in the Subset sheet, it will loop forever (it won't find the column. ***
Now the tested version:
Public Sub Test2()
'Modified by Rick Williams 19/8/2010
Dim FirstRow As Long 'Long, just to be consistent
Dim LastRow As Long
FirstRow = 5
LastRow = Sheets("Master").Cells(Rows.Count, 1).End(xlUp).Row
'Dropped the outer i loop, just compare the headings, that are on 'FirstRow'
For j = 1 To Sheets("Master").Cells(FirstRow, Columns.Count).End(xlToLeft).Column
'While the column heading isn't the one I'm looking for...
While Sheets("Master").Cells(FirstRow, j).Value <> Sheets("Subset").Cells(FirstRow, j).Value
'Move the COLUMN to the right of the last column
Sheets("Subset").Range(Sheets("Subset").Cells(FirstRow, j), Sheets("Subset").Cells(LastRow, j)).Cut
Sheets("Subset").Cells(FirstRow, Columns.Count).End(xlToLeft).Offset(0, 1).Insert Shift:=xlToRight
'(cut-insert takes just one action, rather than two with cut-paste-delete)
Wend
Next j
End Sub
Rather than look at every single value it just shifts the columns as a whole (improving the speed by a factor of (lastrow-firstRow-1) which is presumably a lot for your data. Also, the data will now sort properly, by sticking on each column until the correct one is found. Note that this is still an in efficient algorithm, as the time taken varies as the square of the number of columns. I think on average, for your 250 columns, you will cut/paste about (250+1)/2*(250/2) ~16000 times, and the worst case will be double that - 32k cut/pastes. A better solution would be to search for the right column to cut/paste in each location, thereby only moving each column once. But there are entire computer science subjects on sorting algorithms...
Ah hell, I wrote it for you: :-P
Public Sub InsertionSort()
'Created by Rick Williams 19/8/2010
Dim FirstRow As Long 'Long, just to be consistent
Dim LastRow As Long
FirstRow = 5
LastRow = Sheets("Master").Cells(Rows.Count, 1).End(xlUp).Row
'for each column J...
For j = 1 To Sheets("Master").Cells(FirstRow, Columns.Count).End(xlToLeft).Column
'Find the right column heading
For check = j To Sheets("Subset").Cells(FirstRow, Columns.Count).End(xlToLeft).Column
If Sheets("Master").Cells(FirstRow, j).Value = Sheets("Subset").Cells(FirstRow, check).Value Then
'Move the COLUMN to the correct location...
If Not j = check Then '... if it's not already in the right place
Sheets("Subset").Range(Sheets("Subset").Cells(FirstRow, check), Sheets("Subset").Cells(LastRow, check)).Cut
Sheets("Subset").Cells(FirstRow, j).Insert Shift:=xlToRight
End If
Exit For 'and stop looking for a match - move on!
End If
Next check
Next j
End Sub
The number of comparisons is still proportional to the square of the number of columns, but comparisons are much faster than cut-copy operations.
If you have the time, I'd really like to get an idea from you of how much faster these run for you, that your old macro, and the difference in speed between the two...
Goodluck!