Sorting columns are out of sync

ipbr21054

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

It sorts column A fine so now i wish to use the same code for columns BCD
I change the range code to match each column but when i run the code it allgoes out of sync but dont know why.

Basically i have a userform with 4 command buttons for each column.
When i use the command button for the column in question i wish to just sort A-Z


Rich (BB code):
Private Sub CarBikeSort_Click()
    Dim X As Long
        Application.ScreenUpdating = False
        With Sheets("KEYCODES")
        If .AutoFilterMode Then .AutoFilterMode = False
        X = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Range("A2:Z" & X).Sort Key1:=Range("A3"), Order1:=xlAscending, Header:=xlGuess
    End With
    ActiveWorkbook.Save
       Application.ScreenUpdating = True
       Unload SortForm
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
I change the range code to match each column
What exactly do you change? You should only change the Key1:=Range("A3") part to the relevant column.
 
Upvote 0
That explains then as i was also changing the front part.
So A2, Z is my range then A3 is that column i wish to sort within the mentioned range
 
Upvote 0
That's correct.

Using a table would probably make life easier.
 
Upvote 0
That's correct.

Using a table would probably make life easier.
Would you mind taking a look please.
upload to drop box
TEST FILE

Sort the column named KEY CODE.
You will see at C3 the value is 1734 BUT at C27 C28 C29 C30 shouldnt these value be BEFORE & thus at the top ?
 
Upvote 0
Some of your numbers are text others are real numbers. You also have a lot of repetitive code that should be refactored. Try this - it replaces all the code in your sortform (I am not certain whether the dataoption arguments were available in xl 2007 though):

VBA Code:
Option Explicit
Private Sub CarBikeSort_Click()
   SortCols 1
End Sub
Private Sub KeyTypeSort_Click()
   SortCols 2
End Sub
Private Sub KeyCodeSort_Click()
   SortCols 3
End Sub
Private Sub BitingSort_Click()
   SortCols 4
End Sub
Sub SortCols(ColumnNum As Long)
    Dim X As Long
        Application.ScreenUpdating = False
        With Sheets("KEYCODES")
        If .AutoFilterMode Then .AutoFilterMode = False
        X = .Cells(.Rows.Count, 1).End(xlUp).Row
            .Range("A2:D" & X).Sort Key1:=.Cells(3, ColumnNum), Order1:=xlAscending, Header:=xlGuess, dataoption1:=xlSortTextAsNumbers
    End With
    ActiveWorkbook.Save
       Application.ScreenUpdating = True
       Unload Me
       Sheets("KEYCODES").Range("A3").Select

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,110
Messages
6,123,143
Members
449,098
Latest member
Doanvanhieu

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