View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
[email protected] benmcclave@gmail.com is offline
external usenet poster
 
Posts: 29
Default Run time error specifying range for sort code vba excel 2010

Mark,

The second line is giving you trouble because the ".Range (.Cells(..." combined with the "With" statement effectively set the "Setrange" argument to:
"ActiveWorkbook.Worksheets("Results").Sort.Range(A ctiveWorkbook.Worksheets("Results").Sort.Cells(1,. ..".

To fix it, add the sheet name in front of ".Range" and ".Cells". This would read:

ActiveWorkbook.Worksheets("Results").Range(ActiveW orkbook.Worksheets("Results").Cells(1, 1), ActiveWorkbook.Worksheets("Results").Cells(Variabl e, 1)).

To make it easier to read, you could add a "Dim wsResults as Worksheet" statement to the top of your code, then "Set wsResults = ActiveWorkbook.Worksheets("Results")" just below it. Then, instead of the lengthy SetRange statement you could write

wsResults.Range(wsResults.Cells(1, 1), wsResults.Cells(Variable, 1)).

Here is a completed sub to illustrate:

Dim wsResults As Integer
Set wsResults = ActiveWorkbook.Worksheets("Results")
With wsResults.Sort
.SetRange wsResults.Range(wsResults.Cells(1, 1), wsResults.Cells(variable, 1))
.Header = xlYes
.MatchCase = False
.Orientation = xlLeftToRight
.SortMethod = xlPinYin
.Apply
End With


Hope this helps,
Ben