Advice for sort range on worksheet

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,
Im using the code shown below but when i press the command button nothing happens at all.
Do you see an issue.

Thanks

Worksheet called NAMES
Table name is Table4
Headers in row 1
Currently Range is A2:E43
Sort A-Z in column A



Rich (BB code):
Private Sub CommandButton1_Click()
Dim x As Long
Dim ws As Worksheet
Set ws = Sheets("NAMES")
Dim SortColumn As String

If Len(SortColumn) <> 0 Then
Application.ScreenUpdating = False
With ws
If .AutoFilterMode Then .AutoFilterMode = False
x = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A2:E" & x).Sort Key1:=.Cells(2, SortColumn), Order1:=xlAscending, Header:=xlGuess
End With
End If
End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You are checking the length of a variables names "SortColumn" here:
VBA Code:
If Len(SortColumn) <> 0 Then
but you haven't set that variable equal to anything, so it will ALWAYS be 0, meaning that that IF statement will never be satisfied.
 
Upvote 0
Im sorry but i was lost with that one so did what i thought was easier with less code BUT ha ha ha

This works in respect or sorting etc but where i have headers in row 1 & the list starts in row 2 after the code is run i see some headers replaced by Column 1 Column 2 & also the first customers name in cell A1

I thought that as my range was A2:E43 that is what it would sort.
I assume headers has something to do with it but what ?

Rich (BB code):
Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim Rng As Range
Set ws = Worksheets("NAMES")
Set Rng = ws.Range("A2:E43")
Rng.Sort Key1:=Range("A:A"), Order1:=xlAscending

End Sub
 
Upvote 0
How about this?
VBA Code:
Private Sub CommandButton1_Click()

Dim ws As Worksheet
Dim Rng As Range
Set ws = Worksheets("NAMES")
Set Rng = ws.Range("A1:E43")
Rng.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlYes

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,925
Messages
6,122,303
Members
449,078
Latest member
nonnakkong

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