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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use excel vba to sort data on a hidden sheet

Status
Not open for further replies.

sumgirl

IS-IT--Management
Mar 19, 2003
55
US
Hey all, I need to sort a hidden worksheet, and unhiding it to allow the sort is not really an option. I have some vba code that works great for a visible worksheet, but of course makes in its pants when ran against a hidden one...can someone help?:

----------------------------
Sheets(3).Select
With Selection
.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End With
----------------------------

Thanks!


 



Hi,

Do not use the Select Method
Code:
'[s]Sheets(3).Select [/s]
    With Sheets(3)
        .Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _ 
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, _ 
            Orientation:=xlTopToBottom 
    End With
I would also be careful about using numbers to index sheets. Changing the sheet order will cause an error. Check out the Sheet.CodeName property.

Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Hi Skip,
I got seriously excited for a second, but when I copy and paste that right into my vba, I get an error:

Run-time error '438':
Object doesnt support this property or method

With this higlighted:
.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

:(
 



When you hide your sheet, IF the correct cell was not selected (a cell in the sort range), your original code would not have worked even if the sheet was not hidden.

Sorry I did not catch that.
Code:
    With Sheets(3)[b]
        .Cells(2, 1)[/b].Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _ 
            Header:=xlYes, OrderCustom:=1, MatchCase:=False, _ 
            Orientation:=xlTopToBottom 
    End With



Skip,

[glasses] When a group touring the Crest Toothpaste factory got caught in a large cooler, headlines read...
Tooth Company Freeze a Crowd! and
Many are Cold, but Few are Frozen![tongue]
 
Hang on, this worked:

With Sheets(3).Columns("A:H")
.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End With

Thanks for getting me on the track.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top