Get a sort to work from a userform

rpaulson

Well-known Member
Joined
Oct 4, 2007
Messages
1,428
ANy Guru know why does this not work?

Code:
Private Sub UserForm_initialize() 
...
ComboBox6.List = Array("xlAsending", "xlDescending") 
....

Code:
Private Sub CommandButton2_Click()
Range("B10:T100").Sort Key1:=Range("B10"), Order1:=ComboBox6.Value, Header:=xlYes

thanks,

Ross
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
ANy Guru know why does this not work?

Code:
Private Sub UserForm_initialize() 
...
ComboBox6.List = Array("xlAsending", "xlDescending") 
....

Code:
Private Sub CommandButton2_Click()
Range("B10:T100").Sort Key1:=Range("B10"), Order1:=ComboBox6.Value, Header:=xlYes
First off, you misspelled xlAscending. Second, your values in the ComboBox are text string, not numerical values... xlAscending and xlDescending are pre-defined constants for the numbers 1 and 2 respectively... the Sort method expects the numbers, not the text. Assuming you correct the spelling as noted above, change your CommandButton Click event code to this and it should work...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub CommandButton2_Click()
  Range("B10:T25").Sort Key1:=Range("B10"), Order1:=IIf(ComboBox1.Value = "xlAscending", 1, 2), Header:=xlYes
End Sub[/td]
[/tr]
[/table]
By the way, why not put this code in the ComboBox's Change event instead of the CommandButton's Click event... that way the sort would happen immediately upon the selection from the ComboBox?
 
Last edited:
Upvote 0
Rick,

Thanks for the quick response

I looked at the spelling 30 times. My dislexia is acting up. I guess it would have hot helped until I use the IIf anyway.

I never knew there was a ComboBox's Change event, ill look into it.

Thanks,
Ross
 
Upvote 0

Forum statistics

Threads
1,224,891
Messages
6,181,614
Members
453,057
Latest member
LE102024

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