+ Reply to Thread
Results 1 to 3 of 3

Creating a macro and form control button

Hybrid View

  1. #1
    Registered User
    Join Date
    02-27-2013
    Location
    Rancho Cucamonga, California
    MS-Off Ver
    Excel 2010
    Posts
    4

    Creating a macro and form control button

    Hello all! I am currently having an issue trying to create a macro and having it work through the form control button. I am trying to sort a list alphabetically, where certain items may appear more than once in a sheet, these items also have information in the cells next to them that need to be sorted when these items get sorted.... (if that makes sense..?). I know how to record the macro and assign it to the button, but once these items are listed and I push my form control button, nothing happens.. I'm beginning to think I'm missing a step here.... Help!

    Example of what I need this button to do...

    I would enter the information as :

    Banana 2
    Strawberry 6
    Apple 3
    Orange 1
    Peach 7
    Banana 4
    Apple 1

    When I push the button I need it to read:

    Apple 1
    Apple 3
    Banana 2
    Banana 4
    Orange 1
    Peach 7
    Strawberry 6
    Last edited by kaye.roberts393; 04-09-2013 at 01:49 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Creating a macro and form control button

    Private Sub CommandButton1_Click()
        Me.Hide
        Range("A1:B7").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A7"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:B7"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:B7")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        UserForm1.Show
    End Sub
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    02-27-2013
    Location
    Rancho Cucamonga, California
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Creating a macro and form control button

    Quote Originally Posted by AndyLitch View Post
    Private Sub CommandButton1_Click()
        Me.Hide
        Range("A1:B7").Select
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A7"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:B7"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet1").Sort
            .SetRange Range("A1:B7")
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        UserForm1.Show
    End Sub

    Would this need to be referenced in a new sheet? Sorry, still getting the hang of excel

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1