PDA

View Full Version : Solved: Userform combobox macro to make autosort



hunsnowboard
01-17-2009, 05:25 AM
Hi there Everyone. I have the following problem. I would like to create a userform with a dropdown combolist. The list should be not dynamic, which means that it is not allowed the user to enter new data. The list has 4 items which are on the worksheet named "List" in the range B2-5. When the userform is open the combolist should display the first item on the list (B2) which is the default entry. The whole point of the macro should be to make an autosort in the worksheet ("Sheet1"). The criteria of the autosort should be the item choosen in the dropdown combobox on the userfom. This way the userform should have an OK button which should starts the macro. For example I choose one item on dropdown combobox then press ok, then excel will make an autosort in column 5 with the criteria which was given in the combobox. Please help me if you can! Thank you very much in advance!

mikerickson
01-17-2009, 08:58 AM
What do you mean by "Criteria" for a sort? Ascending or Descending are the most common options.
What kinds of custom sorts are you looking for?
Could you post a sample list and the four ways they could be sorted?

hunsnowboard
01-18-2009, 03:23 AM
HI! I attached the sample file. Criteria for sort: I mean AUTOSORT. In case of autosorting you can select one item, and then by autosorting you will see the records containing only that item in the relevant column.
In the sample file you can see the following:
in sheet1 there is a big button which will start the userform
in sheet2 you can see the table
in the sheet 3 which is named "Lista" you can see the list for the combobox.

In sheet2 you can see that I already enabled the autosort. The sorting should be made in the column "E".
The combobox also contains the sorting items. What I would like the macro to do is (when selected manually one item in the combobox by the user) once OK is pressed to sort the given item (selected in combobox) in the sheet2 column "E". This way only the relevant records will be shown. I hope this way is more understandble. Thank you in advance!

hunsnowboard
01-18-2009, 10:34 AM
Can someone help me please? I would need this macro by tomorrow! Thank you in advance!

georgiboy
01-18-2009, 12:02 PM
Try this...

Option Explicit
Private Sub CommandButton1_Click()
Dim str1 As String
Dim NameCount As Integer

NameCount = Munka2.Range("E" & Rows.Count).End(xlUp).Row
str1 = UserForm1.ComboBox1.Value

Munka2.Range("A1:G" & NameCount).AutoFilter Field:=5, Criteria1:=str1

End Sub

Hope this helps

hunsnowboard
01-18-2009, 12:15 PM
Hi Georgiboy! Thank you a lot! This code works great! There is one more issue about the combobox, I hope you can help. How can I set the combobox to have the item "Maros" as default value when the userform is initialized? Thank you in advance!

lucas
01-18-2009, 03:22 PM
Select the combobox in the vbe and put the word Maros in the blank to the right of the "value" property.

hunsnowboard
01-20-2009, 11:18 AM
Thank you very much for your help! Now it is working! :)