Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sort

Status
Not open for further replies.

fishysheep

Programmer
Oct 13, 2001
54
GB
Hi
vb prog with excel reference to 2 workbooks each with multiple sheets. Have selected x amount of rows (and I can see that this is correct because they are highlighted in front of me).

If I sort the same rows in excel then all is fine, but I can't seem to sort them via vb. I think the problem is that key1 isn't referring to the correct worksheet/workbook. I've tried activating the sheet first (didn't work), and also the following:

Selection.Sort Key1:=Range("T1400"), _
Order1:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers

*also*

Selection.Sort Key1:=xlsummary.Range("T1400"), _
Order1:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers

*also* (and this returns error)
Selection.Sort Key1:=xlwb.Worksheets("Summary").Range("T1400"), _
Order1:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers


Just to recap, the selection and the cell address of key1 in the sort statement are fine, and yes the key1 cell ref is part of the selection - checked and rechecked... but it still doesn't sort!

thank you!
 

Hi,
Try referencing the SHEET...
Code:
    With Sheets("YourSheetName")
        .[T1400].Sort Key1:=.Range("T1400"), _
                    Order1:=xlAscending, _
                    Header:=xlNo, _
                    OrderCustom:=1, _
                    MatchCase:=False, _
                    Orientation:=xlTopToBottom, _
                    DataOption1:=xlSortTextAsNumbers
    End With

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 

if I try that I get an
"object does not support this property or method"
error. I have to prefix sheets/worksheets (tried both) with a ref to the workbook else I get a subscript error.

Also, I'm using a selection.

any other ideas?


 
finally...
gave up trying to work with selection - took range of selection and used that instead. Don't know if this will be of use to anyone else but from what my sleep-deprived brain can take in at this time of night, if you use selection.sort your keyX has to refer to selection.columns.index(x)

With xlwb.Sheets("summary")
.Range(selRange).Sort Key1:=.Range("T1400"), _
Order1:=xlAscending, _
Header:=xlNo, _
OrderCustom:=1, _
MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers
End With


thank you and goodnight :)

 


You do NOT need to SELECT.

This does work
Code:
    With Sheets("Sheet1")
        .[A1].Sort Key1:=.Range("A1"), _
                    Order1:=xlAscending, _
                    Header:=xlNo, _
                    OrderCustom:=1, _
                    MatchCase:=False, _
                    Orientation:=xlTopToBottom
    End With

Skip,

[glasses] [red]A palindrome gone wrong?[/red]
A man, a plan, a ROOT canal...
PULLEMALL![tongue]
 
I never said that you did need to select, I said that the piece of code I posted was based on a select.

but thank you so much for the capitals.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top