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

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
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,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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