Sort code not sorting correctly

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,226
Office Version
  1. 2007
Platform
  1. Windows
Morning,

I am using the code supplied below but noticed it doesnt sort correctly.
Do you see why or can you advise another code.

I have supplied some screen shot example of its sorting issue.


VBA Code:
Private Sub CommandButton1_Click()
Dim x As Long
Dim ws As Worksheet
Set ws = Sheets("MC LIST")
Dim SortColumn As String

Select Case ComboBox1
Case "CUSTOMER"
SortColumn = "A"

Case "VIN"
SortColumn = "B"

Case "MAKE"
SortColumn = "C"

Case "MODEL"
SortColumn = "D"

Case "YEAR"
SortColumn = "E"

Case "ITEM BOUGHT"
SortColumn = "F"

Case "CHIP"
SortColumn = "G"

Case "COUNTRY"
SortColumn = "H"

Case "ORIGINAL PN"
SortColumn = "I"

End Select
If Len(SortColumn) <> 0 Then
Application.ScreenUpdating = False
With ws
If .AutoFilterMode Then .AutoFilterMode = False
x = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A7:I" & x).Sort Key1:=.Cells(2, SortColumn), Order1:=xlAscending, Header:=xlGuess
End With
End If
Unload AtoZMcList
End Sub
Private Sub UserForm_Initialize()
Dim rheadings As Range
Dim cl As Range
Set rheadings = Worksheets("MC LIST").Range("A6:I6")
For Each cl In rheadings
Me.ComboBox1.AddItem cl.Value
Next cl
End Sub
 

Attachments

  • 6522.jpg
    6522.jpg
    41.2 KB · Views: 3
  • 6523.jpg
    6523.jpg
    75.3 KB · Views: 3
  • 6524.jpg
    6524.jpg
    23.8 KB · Views: 5
  • 6525.jpg
    6525.jpg
    32.9 KB · Views: 3
  • 6526.jpg
    6526.jpg
    22.8 KB · Views: 4

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this

VBA Code:
' INSERT THIS LINE
     .Sort.SortFields.Clear
' ABOVE
     .Range("A7:I" & x).Sort Key1:=.Cells(2, SortColumn), Order1:=xlAscending, Header:=xlGuess
 
Upvote 0
do some of your data fields have spaces before the text, though I doubt that is the only answer

If you change
Case "YEAR"
SortColumn = "E"
to
Case "YEAR"
SortColumn = 5

does that make a difference
 
Upvote 0
It looks as if you're passing a string (SortColumn) to this line:
VBA Code:
.Range("A7:I" & x).Sort Key1:=.Cells(2, SortColumn), Order1:=xlAscending, Header:=xlGuess
...where I think SortColumn might need to be an integer - to pass the column number to the sortKey.
 
Upvote 0
Well i cant explain,but after making a change & nothing happened a pc restart now shows it all ok ?

many thanks for the replies.
 
Upvote 0
Well i cant explain,but after making a change & nothing happened a pc restart now shows it all ok ?

many thanks for the replies.
may be related to other code like application.enablevents or application.screenupdating or application.calculate that could have been turned off, and a restart will clear those down. I often add a recover sub somewhere in my modules

VBA Code:
sub recover
with application
.screenupdating = true
.enableevents = true
end with
end sub

just to quickly reset what i have been trying
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,793
Members
449,048
Latest member
greyangel23

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