+ Reply to Thread
Results 1 to 13 of 13

Scroll through ListBox using CommandButton

  1. #1
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Scroll through ListBox using CommandButton

    Is there any way that a CommandButton can be used to scroll a selection of ListBox items?

    All are contained within a UserForm and the ListBox items are drawn from a dymanic list in a specified folder.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Scroll through ListBox using CommandButton

    What are you trying to achieve?

    Excel inserts Horizontal and Vertical scroll Bars into the Listbox as needed.

    Are you saying that you want to highlight a specific entry as you scroll down?
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Scroll through ListBox using CommandButton

    Hi mehmetcik,

    Yes, When I click a button I would like to select the 1st entry in the ListBox and then with each subsequent click I would like the ListBox to select the next entry, and so on, in a loop.


    Can that be achieved?

    Thank you,

    Andy

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Scroll through ListBox using CommandButton

    Hi there,

    See if the attached workbook does what you need.

    It uses the following code in the VBA CodeModule of the UserForm:

    Please Login or Register  to view this content.
    and the following code in a standard VBA CodeModule:

    Please Login or Register  to view this content.

    Hope this helps - please let me know how you get on.

    Regards,

    Greg M
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,004

    Re: Scroll through ListBox using CommandButton

    Quote Originally Posted by AndyE View Post
    Hi mehmetcik,

    Yes, When I click a button I would like to select the 1st entry in the ListBox and then with each subsequent click I would like the ListBox to select the next entry, and so on, in a loop.
    But what you're describing doesn't involve scrolling, it just looping the selection from one item to next item. Am I correct?

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Scroll through ListBox using CommandButton

    Try this Code

    Please Login or Register  to view this content.




    A Spinbutton or a Scrollbar are better for this:=


    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by mehmetcik; 02-19-2019 at 02:06 PM.

  7. #7
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Scroll through ListBox using CommandButton

    Thanks for the replies...ukuini, you are correct, lost in translation there I think, apologies...

    I have had a look through the responses and I agree that a spinbutton or similar would likely be most suitable, but have the following observations if someone can assist?

    I have attached an example workbook and some images within a folder structure replicating what I have.

    The relevant filepath for the images will need to be replicated in the code.

    I am encountering the following problems at the moment,

    1 - Upon clicking the right spinbutton the first entry in Listbox1 is not selected, and,
    If image#2 "sid" is selected in Listbox1, where "bird" is selected in Combobox1, then Combobox1 is changed to "cat", clicking the right spinbutton loads image#3 "spider", and so on...it's like the listbox value needs to be reset each time combobox1 is changed.

    2 - If the last entry in Listbox1 is selected with the Combobox1 selections "cat" or "dog" and the Combobox1 selection is changed to the preceding value, i.e. "bird" or "cat" respectively, and the left spinbutton is clicked, all is well, however if the right spinbutton is clicked the following error is produced,


    Run-time error '380':
    Could not set the Selected property. Invalid property value.
    Any ideas would be most welcome...

    Thank you
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Scroll through ListBox using CommandButton

    *** Bump ***

  9. #9
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: Scroll through ListBox using CommandButton

    Try this:

    Please Login or Register  to view this content.
    Note that I moved the root folder path to a constant at the top so you only need to alter it in one place.
    Rory

  10. #10
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Scroll through ListBox using CommandButton

    Hi rorya

    I was getting the same error message when I selected an entry in the ComboBox I couldn't understand why. Then I realised that in the version I am running one of the ComboBox entries didn't have any images attached which would populate the ListBox, hence the ListIndex would have to be -1 rather than 0, so it was crashing out at the line
    Please Login or Register  to view this content.
    .

    I've fixed it by wrapping that section in an If command, checking that the ListBox is populated before continuing, I'm guessing that is the most convenient way to solve my particular problem?

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,915

    Re: Scroll through ListBox using CommandButton

    Yes, that seems quite reasonable.

  12. #12
    Forum Contributor
    Join Date
    05-08-2009
    Location
    england
    MS-Off Ver
    Excel 2007
    Posts
    223

    Re: Scroll through ListBox using CommandButton

    Ha! Cool Thank you all for your help. I've got another question but that's for another post I think...

  13. #13
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Scroll through ListBox using CommandButton

    Hi again,

    Many thanks for your private message and also for the Reputation increase - much appreciated!

    You're welcome - glad to hear that everything seems to be working correctly now.

    Best regards,

    Greg M

+ 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. Commandbutton to copy range of cells as image into a Userform or Listbox
    By chrisellis250 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-26-2017, 12:39 PM
  2. How do I get the scroll distance of a userform listbox scroll bar...
    By Rob Frankham in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-31-2014, 04:23 PM
  3. [SOLVED] Disable commandbutton when listbox is Empty!
    By Petter120 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-30-2014, 02:08 AM
  4. [SOLVED] Show ListBox based on CommandButton
    By WimpieOosthuizen in forum Excel General
    Replies: 5
    Last Post: 06-17-2014, 08:46 AM
  5. Print ListBox data with a commandbutton
    By Doctor_H in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-05-2013, 04:09 PM
  6. [SOLVED] Populating ListBox using value from Combobox via CommandButton
    By Doctor_H in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 04-04-2013, 05:56 PM
  7. Listbox to textbox via commandbutton
    By himanshu83 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-14-2010, 03:14 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