Sorting one worksheet from another worksheet

P

Phyllis

I am wanting to sort data in a worksheet from a command button on a different
worksheet. I want the focus to stay (or at least return) to the worksheet
containing the command button. Is this even possible? If so, could someone
supply a sample of code? Both worksheets are in the same workbook. Using
excel 2003 .
 
D

Dave Peterson

Yes. The trick is to not select the other sheet.

Option Explicit
Private Sub CommandButton1_Click()

Dim wks As Worksheet
Dim myRng As Range

Set wks = Me.Parent.Worksheets("Sheet2") 'some other sheet

With wks
Set myRng = .Range("A1:x99") 'some range
End With

With myRng
.Cells.Sort _
key1:=.Columns(1), order1:=xlAscending, _
key2:=.Columns(3), order1:=xlAscending, _
header:=xlYes
End With
End Sub
 
D

Dave Peterson

Check your other post.
I am wanting to sort data in a worksheet from a command button on a different
worksheet. I want the focus to stay (or at least return) to the worksheet
containing the command button. Is this even possible? If so, could someone
supply a sample of code? Both worksheets are in the same workbook. Using
excel 2003 .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top