Another Sorting Question

scottwilkes

New Member
Joined
Oct 9, 2017
Messages
1
I have the following code which starts off by pasting into cells A1 through B6 the following data. All of the data pasted into column "A" is numeric. I ran the macro recorder and then copied and pasted that into the bottom of my code. When it gets to the line
Code:
        ActiveWorkbook.Worksheets("TempData").Sort.SortFields.Add Key:=Range("A1:A6") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
it fails with a runtime error of 1004, Application-defined or object-defined error. I have spent about 5 hours on this so far, researching and am not able to come up with an answer. So, I am turning the community for help. This is Windows 10, Office 2010, VBA 7.0. Any help would be greatly appreciated!
Code:
    For i = 2 To 6
        Sheets("TempData").Cells(1, 1).Value = Sheets("NewList").Cells(i, UpdateDistanceBetweenPlace_1andPlace_2).Value
        Sheets("TempData").Cells(1, 2).Value = "Place_1, Place_2"
    
        Sheets("TempData").Cells(2, 1).Value = Sheets("NewList").Cells(i, UpdateDistanceBetweenPlace_1andPlace_4).Value
        Sheets("TempData").Cells(2, 2).Value = "Place_1, Place_4"
        
        Sheets("TempData").Cells(3, 1).Value = Sheets("NewList").Cells(i, UpdateDistanceBetweenPlace_1andPlace_3).Value
        Sheets("TempData").Cells(3, 2).Value = "Place_1, Place_3"
        
        Sheets("TempData").Cells(4, 1).Value = Sheets("NewList").Cells(i, UpdateDistanceBetweenPlace_4andPlace_2).Value
        Sheets("TempData").Cells(4, 2).Value = "Place_4, Place_2"
        
        Sheets("TempData").Cells(5, 1).Value = Sheets("NewList").Cells(i, UpdateDistanceBetweenPlace_4andPlace_3).Value
        Sheets("TempData").Cells(5, 2).Value = "Place_4, Place_3"
        
        Sheets("TempData").Cells(6, 1).Value = Sheets("NewList").Cells(i, UpdateDistanceBetweenPlace_3andPlace_2).Value
        Sheets("TempData").Cells(6, 2).Value = "Place_3, Place_2"
        
        FinalDistanceAverage = (Sheets("TempData").Cells(1, 1).Value + Sheets("TempData").Cells(2, 1).Value + Sheets("TempData").Cells(3, 1).Value + Sheets("TempData").Cells(4, 1).Value + Sheets("TempData").Cells(5, 1).Value + Sheets("TempData").Cells(6, 1).Value) / 6
        
        Range("A1:B6").Select
        
        ActiveWorkbook.Worksheets("TempData").Sort.SortFields.Clear
        
        ActiveWorkbook.Worksheets("TempData").Sort.SortFields.Add Key:=Range("A1:A6") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            
        With ActiveWorkbook.Worksheets("TempData").Sort
            .SetRange Range("A1:B6")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Next i
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top