First a recommendation...
When asking for Excel help, specify the Excel version, the Operating System and
if you are referencing an error, provide the error number. error description and
the code line on which it occurs.
The answer to your question is yes. If the sheet name doesn't exist in the workbook
Excel will throw an error. If the layout of the sheets is similar then substituting
"ActiveSheet" for specific sheet names should work. Give the following a try...
(Note that it sort on columns P and columns C)
'--
Sub PossibleSortCode()
Dim rngSort As Excel.Range
Set rngSort = ActiveSheet.Range("A1", ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell))
rngSort.Sort key1:=ActiveSheet.Columns("P
![Stick Out Tongue :p :p](data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7)
"), order1:=xlAscending, _
key2:=ActiveSheet.Columns("C:C"), order2:=xlAscending, _
Header:=xlYes, MatchCase:=False, Orientation:=xlTopToBottom
Set rngSort = Nothing
End Sub
--
Jim Cone
Portland, Oregon USA
http://www.contextures.com/excel-sort-addin.html
(worth a look)
"JCO" <
[email protected]>
wrote in message
Okay then I did it correctly. I just selected the column.
Can I ask you another question?
I have a sort routine but when I run it on another file (similar data) I get
an error. This code was generated by using the Recorder. I selected the
whole data by taping the upper right corner (just left of the "A" Column).
I'm wondering if that is part of the issue. This sort does a double sort.
It needs to select the entire data (always a different size report
everyday). Sort on column "Assistance Category" (column P). Then with in
that it needs to Sort on Column "Last Name" (column C). I do need to check
the box that tells the sort that my data contains header so as to eliminate
the first row from the sort.
Cells.Select
ActiveWorkbook.Worksheets("NEEDWest_Errors_2010-Jan-Jun7").Sort.SortFields.
_
Clear
ActiveWorkbook.Worksheets("NEEDWest_Errors_2010-Jan-Jun7").Sort.SortFields.Add
_
Key:=Range("G2:G3869"), SortOn:=xlSortOnValues, Order:=xlAscending,
_
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("NEEDWest_Errors_2010-Jan-Jun7").Sort.SortFields.Add
_
Key:=Range("B2:B3869"), SortOn:=xlSortOnValues, Order:=xlAscending,
_
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("NEEDWest_Errors_2010-Jan-Jun7").Sort
.SetRange Range("A1:I3869")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
As you can see, the actual filename is embedded in the code and I'm
wondering if that is causing the issue too.