Sorting one worksheet from another worksheet

  • Thread starter Thread starter Phyllis
  • Start date Start date
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 .
 
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
 
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 .
 
Back
Top