+ Reply to Thread
Results 1 to 4 of 4

Create buttons to sort

  1. #1
    Registered User
    Join Date
    09-24-2006
    Posts
    2

    Unhappy Create buttons to sort

    At my work we have sheets that have buttons, & when you click on the buttons it alphabetizes the row. There are a few buttons, on a few different rows. I have been all over the internet trying to figure out how to do this. I figured out how to get the button on the sheet, just not how to make it sort.
    Any help would be appreciated. I Am also pretty new at this, so please be clear. Thanks

  2. #2
    Forum Contributor stevebriz's Avatar
    Join Date
    09-07-2006
    Location
    Santiago Chile
    Posts
    389
    if youe on the excel sheet and press ALT F11 then you will open the vbeditor
    You can then look in the modules though the project expolor and look at the code that does this ...providing that you the project is not behind a password
    VBA - The Power Behind the Grid

    Posting a sample of your workbook makes it easier to look at the Issue.

  3. #3
    Registered User
    Join Date
    09-24-2006
    Posts
    2

    Didn't help

    Like I said, I'm pretty new at this. I did what you said, but I couldn't figure out what was what.

  4. #4
    Forum Contributor
    Join Date
    09-04-2006
    Location
    Yorkshire, England
    Posts
    267
    Hi


    Are you using autoshapes from the drawing toolbar to create a button?

    View > Toolbars > Drawing

    If so you can right click on it and select assign macro. It willl bring up a userform menu and highlighted in black should be the name of the macro your button runs. (what makes it sort). Now you know the name of the assigned macro do the following

    Tools > Macro > Macros...

    Left click once on the name, so it is highlighted in blue and then click 'step into' from the side menu and you will see a load of code that sorts your rows when the button is clicked. It might look something like the below (depending on the specifics of your macro:

    Sub Macro3()
    '
    ' Macro3 Macro
    ' Macro recorded 02/10/2006
    '

    '
    Columns("B:B").Select
    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    Range("C1").Select
    End Sub

    This looks complicated (and it is if you write it yourself). But thankfully for the beginner to VBA (Visual Basic for Applications, macros and stuff) you can simply create a macro by recording one.

    To create a macro

    Tools > Macro > Record new macro...

    Here the little userform menu pops up and asks you to name your new macro( no spaces,numbers or symbols as far as i can remember) You will also get the option to assign a shortcut key to your new macro, but since you have a button you dont really need to bother with the rest of the stuff it asks you to do. By clicking OK you start recording so remember it will record everything you do so dont make any mistakes!. So you can select a column or row or whatever you want to do and sort

    Data > Sort > Accending/ decending etc

    Then when you are done if a toolbar has appeared there should be a button on it that looks like a blue/red square, click it to stop or alternativly go

    Tools > Macro > Stop recording

    Now that you are done you can select your button, right click and assign your macro to it.

    As Stevebriz said if you press Alt and F11, this is a shortcut key to open the VBA Editor. By pressing it you should have openned a new window. Down the left hand side there should be another window called project - vba project. Underneath it should be 3 drop down menus.

    1. Vba project(name of your workbook)
    2. Microsoft excel objects
    3. Modules

    if you sheet has usersforms (think the record new macro on screen menu thing ) that the person who created the workbook has made there might be a fourth one called forms. No need to worry now about userforms)

    Macros are automatically stored in modules and by openning the drop down menu there should be some e.g. module 1 / module 2 depending on the volume of macros. Double click on the module name and you will open it up. In the middle of the VBAeditor window there should be a white screen with loads of writing (like the example macro above). these are you macros.
    Macros are seperated by lines. However if someone has password protected their macros you wont be able to do the above. If you accidently record something you dont want included in the macros you can delete it here

    say if you selected the wrong columns you can find the code e.g.

    Columns("B:B").Select

    and either edit it or delete it completely. However if you delete something your not sure what it does, it might stop your macro from working properly so for a new user it is best to avoid deleting things that dont stand out as obvious. By closing this down you can run your macros again.

    Hope it helps
    JR
    Versions
    Mac OS X 'Leopard'
    Mac MS Office Excel 2004
    Windows XP
    MS Excel 2002
    MS Excel 2003

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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