VBA sort ws without selecting

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
557
My code adds data to worksheet (“Members”) without selecting the worksheet.
In order to sort the worksheet I have to select the ws and then select the range. Otherwise, the code sorts the active worksheet.
How can I modify my code to sort worksheet ("Members”) without selecting it?
Additionally, worksheet ("Members") will eventually exceed the selection range (JZ500). Can this be revised to make the range dynamic?
Thanks for looking!

Rich (BB code):
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
 
Sheets("Members").Unprotect
 
    Dim MemberName As String
    Dim iRow As Long
    Dim ws As Worksheet
   
 Set ws = Worksheets("Members")
 
    MemberName = Me.TextBox2.Value & " " & Me.TextBox1.Value
 
    Set CLoc = ws.Columns("C:C").Find(What:=MemberName, after:=ws.Cells(1, 3), LookIn:= _
                            xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:= _
                            xlNext, MatchCase:=False, SearchFormat:=False)
                           
      If CLoc Is Nothing Then
        iRow = ws.Cells(Rows.Count, 1) _
               .End(xlUp).Offset(1, 0).Row
        Else
 
        iRow = CLoc.Row
        End If
 
With ws
    ws.Cells(iRow, 1) = StrConv(TextBox1.Text, vbProperCase)
    ws.Cells(iRow, 2) = StrConv(TextBox2.Text, vbProperCase)
    ws.Cells(iRow, 3) = StrConv(TextBox2.Text, vbProperCase) & " " & StrConv(TextBox1.Text, vbProperCase)
    ws.Cells(iRow, 4) = TextBox7.Value & Me.TextBox8.Value & Me.TextBox9.Value
    ws.Cells(iRow, 5) = StrConv(TextBox10.Text, vbLowerCase)
    ws.Cells(iRow, 7) = StrConv(TextBox3.Text, vbProperCase)
    ws.Cells(iRow, 10) = Me.TextBox6.Value   
    ws.Cells(iRow, 13) = Me.TextBox14.Value
    ws.Cells(iRow, 8) = StrConv(ComboBox2.Text, vbProperCase)
    ws.Cells(iRow, 9) = StrConv(ComboBox3.Text, vbUpperCase)
 
ws.Select
 [A2:JZ500].Select
Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("A2"), Order1:=xlAscending, Header:= _
                               xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                               DataOption1:=xlSortNormalHeader
[A2].Select
 
‘More code follows
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try
Code:
ws.Range("A2:JZ500").Sort Key1:=ws.Range("B2"), Order1:=xlAscending, Key2:=ws.Range("A2"), Order1:=xlAscending, Header:= _
                               xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                               DataOption1:=xlSortNormalHeader
 
Upvote 0
Hi,
try

Rich (BB code):
ws.UsedRange.Sort Key1:=ws.Range("B2"), Order1:=xlAscending, Key2:=ws.Range("A2"), Order1:=xlAscending, Header:= _
                               xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                               DataOption1:=xlSortNormalHeader

note the qualification of the ranges with your worksheet object variable.

Dave
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top