VBA sort ws without selecting

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
552
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
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,530
Office Version
  1. 365
Platform
  1. Windows
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
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
6,102
Office Version
  1. 2019
Platform
  1. Windows
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
 

Big Lar

Well-known Member
Joined
May 19, 2002
Messages
552
Thanks guys.
As usual, I was trying to make things more complicated than necessary.
Cheers!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
46,530
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback
 

Watch MrExcel Video

Forum statistics

Threads
1,109,360
Messages
5,528,233
Members
409,809
Latest member
VICKRAM

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top