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
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:
ActiveWorkbook.Worksheets("TempData").Sort.SortFields.Add Key:=Range("A1:A6") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
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