Can you check my sort code please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I have this code shown below.

Its supposed to sort the data list using column D
When i use the command button nothing happens / changes/
I dont even get an error message.

Do you see ?
Thanks


VBA Code:
Private Sub SortListA_Z_Click()
    Dim x As Long
    
    Application.ScreenUpdating = False
    
    With Sheets("KEY CODE LIST")
        If .AutoFilterMode Then .AutoFilterMode = False
        x = .Cells(.Rows.Count, 1).End(xlUp).Row
        
        .Range("D2:F" & x).Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlGuess
    
    End With
                      
    ActiveWorkbook.Save
    
    Application.ScreenUpdating = True
    Sheets("KEY CODE LIST").Range("A1").Select
End Sub
 

Attachments

  • 7159.jpg
    7159.jpg
    241.4 KB · Views: 5

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
x = .Cells(.Rows.Count, 1).End(xlUp).Row

You must use a column with data, in your code you are using column 1, in your image column 1 is empty, then you can use D

VBA Code:
x = .Cells(.Rows.Count, "D").End(xlUp).Row
 
Upvote 0
If you don't have anything in col A, then x = 1 and you are sorting the range D1:F2 only. With headers in row 1 that leaves only one row to sort so it appears nothing is happening. If you want to sort col D then change this:

x = .Cells(.Rows.Count, 1).End(xlUp).Row
to this:

x = .Cells(.Rows.Count, 4).End(xlUp).Row
 
Upvote 0
Many thanks.
I did change the code from 1 to 4 before i posted but received a syntax error.

I also changed 1 to D which also failed.

Changing it to "D" like Dante advised worked.

I forget ""
 
Upvote 0
Hi,
Ive noticed for some reason that once i run the sort code a blank row is shown at row 2
The value A1 should be first ?

See screen shot
 

Attachments

  • 7160.jpg
    7160.jpg
    88 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,592
Members
449,089
Latest member
Motoracer88

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