+ Reply to Thread
Results 1 to 8 of 8

Move listbox item on list with arrow keys

  1. #1
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Move listbox item on list with arrow keys

    I'm attempting to have items in a listbox move up or down on the list if the up/down arrow keys are pressed. When the user clicks on the "arrows" icon, they are prompted if they really want to move the item, and if they choose yes, then the up/down arrow keys is supposed to move the item, but it isn't working as intended; basically moving the arrow keys has no effect other than moving up and down the list as it would regularly. I have a feeling I'm using Application.OnKey incorrectly. Here is my code so far:

    Please Login or Register  to view this content.

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Move listbox item on list with arrow keys

    Take a look at the KeyPress and/or KeyDown events of the listbox itself.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Move listbox item on list with arrow keys

    That particular code makes it so every time the arrow keys are pressed, the selected listbox item moves up or down on the list. I was hoping to make it so that the arrow keys only perform this function after the user clicks the "arrows" button I've set up. You've given me something to work with; I'll see what I can figure out.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Move listbox item on list with arrow keys

    You never really mentioned having your own buttons.

    I assumed you were referring to the keyboard arrows, especially since you had this code,
    Please Login or Register  to view this content.
    Anyway, if you do have up/down buttons all you should need to do is cell MoveItem, with the appropriate argument, when they are clicked.

  5. #5
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Move listbox item on list with arrow keys

    I can see the confusion, however I do in fact mean the keyboard arrow keys as you initially presumed. The "button" asks users if they want to rearrange items in the list box, and if so, to use the arrow keys to move the listbox item up or down on the list. The user then presses the "button" again to save the changes made which is vital to my program because that part of the code actually matches the listbox with the database on a hidden sheet. So, I need the button to do what it does, and the arrow keys to do what they do when the button is pressed. I don't want the arrow keys to keep doing this function after the "button" is saved, and I was trying to avoid having to create two buttons (up and down) to do what you mention in your second post due to aesthetics and the nature of the save mechanic.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Move listbox item on list with arrow keys

    Can you upload an example workbook?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

  7. #7
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Move listbox item on list with arrow keys

    Norie, thank you for the response. I ended up altering my code due to several reasons. Right now, I'm so confused and agitated! I discovered two errors. First, when I used the code I had initially listed, I kept getting errors because I was using a RowSource and altering the ListBox.Index with vba without altering the RowSource gave me errors. So I remodified my code to use two buttons, one with an UP arrow and one with a DOWN arrow. On my first attempt, it kept moving my named range "Vendor" only which kept my data in VendorList unmoved. I had to make Vendor a separate named range from VendorList or the listbox couldn't see it properly. So, I needed the arrow buttons to move the item in the named range VendorList up and down. A little edit later and this code is working fine:

    Works like a champ:

    Please Login or Register  to view this content.
    This completely identical code does not work at all! The ONLY difference is I replaced xlDown with xlUp so the cut/paste would end up in the correct place. The error STOPS at =xlUp and says Run-time error '1004': Insert method of Range class failed. *If* I change xlUp to xlDown just to experiment, it works fine but performs the Up Arrow function. I have no clue why xlUp does not work in vba. Am I somehow using it incorrectly? From every example I've seen on the net, I seem to be using it correctly, so I'm at a real loss here.

    Please Login or Register  to view this content.
    Last edited by Taemex; 01-27-2015 at 10:42 PM.

  8. #8
    Forum Contributor
    Join Date
    08-29-2012
    Location
    United States
    MS-Off Ver
    Excel 2003
    Posts
    103

    Re: Move listbox item on list with arrow keys

    I still have no clue why Shift:=xlUp will not work with rows, but I devised an archaic solution if anyone's interested; it involves inserting a row, copying a row, and deleting a row... as opposed to just inserting the row where you want it.

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Hpw do you move a chart with the arrow keys?
    By ChrisNor in forum Excel Charting & Pivots
    Replies: 10
    Last Post: 10-29-2013, 12:52 AM
  2. Is it possible to move legends with arrow keys?
    By Economystudent89 in forum Excel General
    Replies: 4
    Last Post: 10-08-2012, 09:13 AM
  3. [SOLVED] Can't move between cells using arrow keys
    By nisamniko in forum Excel - New Users/Basics
    Replies: 3
    Last Post: 08-15-2012, 03:44 AM
  4. [SOLVED] Arrow keys to move cell
    By Richard in forum Excel General
    Replies: 2
    Last Post: 05-01-2006, 05:25 AM
  5. [SOLVED] Arrow keys won't move cursor
    By Walterius in forum Excel General
    Replies: 2
    Last Post: 06-09-2005, 06:05 PM

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