+ Reply to Thread
Results 1 to 13 of 13

VBA listbox auto scroll to selected item

  1. #1
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    VBA listbox auto scroll to selected item

    I am looking for a code to auto scroll on a VBA listbox based on Current date value or next available date.

    So example if I have on my worksheet with dates :
    26/07/2017
    27/07/2017
    27/07/2017
    28/07/2017
    29/07/2017

    And if the current system date is = 28/07/2017 then it my scroll to the first 28/07/2017 (first record in the listbox) without selecting the row. I can still scroll up for previous dates or go down for future dates record.
    If in a example where the system date is Today (28/07/2017) and there is no 28/07/2017 in the excel sheet, then is must display the next available date ( maybe: 29/07/2017)

    Below are the names on my UserForm

    My ListBox name is: Listbox1
    My Sheet name is: Sheet4
    The Date column is: D (also know as: .Column(3) )
    The Current Code I have on UserForm_Initialize:
    Listbox1.List = Sheets("Sheet4").Range("A4:l" & [a65536].End(3).Row).Value

    Example Screenshot
    UQb0wYL.png

    Regards
    Shabeersa

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: VBA listbox auto scroll to selected item

    you can use the TopIndex property to set which items appears at the top.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    Re: VBA listbox auto scroll to selected item

    Hi Andy

    Thank you for your reply

    I am new to VBA can do provide code for me to apply

    Regards
    SHabeersa

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: VBA listbox auto scroll to selected item

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    Re: VBA listbox auto scroll to selected item

    Hi Andy

    I am putting the code in the Private Sub UserForm_Initialize()

    Getting error " Method or Data member not found"
    See screenshot
    autoscroll error.png


    Also why you say "Sheet1.Listbox1.List" when Sheet4 is the main sheet

    Please assist

    Shabeersa

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: VBA listbox auto scroll to selected item

    Because I was testing on my own workbook. Just modify the sheet reference to your worksheet name.

  7. #7
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    Re: VBA listbox auto scroll to selected item

    Quote Originally Posted by Andy Pope View Post
    Because I was testing on my own workbook. Just modify the sheet reference to your worksheet name.
    Please find the excel sheet below. I changed to to Sheet4 which is the main data sheet

    HTML Code: 

    Open VBA developer mode to debug

    Or login as Guest and Guest

    User " Userform1" on the UserForm_Initialize

    Regards
    ShabeerSA

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: VBA listbox auto scroll to selected item

    If you have an example workbook use the forum to attach it.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

  9. #9
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    Re: VBA listbox auto scroll to selected item

    Quote Originally Posted by Andy Pope View Post
    If you have an example workbook use the forum to attach it.

    Attach a sample workbook. Make sure there is just enough data to demonstrate your need. Include a BEFORE sheet and an AFTER sheet in the workbook if needed to show the process you're trying to complete or automate. Make sure your desired results are shown, mock them up manually if necessary.

    Remember to desensitize the data.

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.

    Kindly find attachment
    Click here
    Attached Files Attached Files

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: VBA listbox auto scroll to selected item

    I did not pickup from your image that the listbox was on a userform. I thought it was on the worksheet.

    Please Login or Register  to view this content.
    Insert the extra code in to your current code in the initialize event

  11. #11
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    Re: VBA listbox auto scroll to selected item

    Quote Originally Posted by Andy Pope View Post
    I did not pickup from your image that the listbox was on a userform. I thought it was on the worksheet.

    Please Login or Register  to view this content.
    Insert the extra code in to your current code in the initialize event
    HI Andy

    Thank you, However this time no errors but its not auto scrolling to today's date..

    See my video showing you
    See Video

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,430

    Re: VBA listbox auto scroll to selected item

    It works for me, bring entry for 31/7/2017 to the top of the viewable area of the listbox
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    04-22-2013
    Location
    AA
    MS-Off Ver
    Word 2010
    Posts
    26

    Re: VBA listbox auto scroll to selected item

    Ok Thank you Andy so much, its works

+ 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. [SOLVED] Do something for each selected item in a listbox
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2017, 06:38 AM
  2. Filter using Listbox selected item(s)
    By dgbunny in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2016, 09:17 AM
  3. Get the name of a listbox where an item is selected
    By Jerbinator in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-22-2016, 03:16 PM
  4. Selected item in first listbox.
    By waka in forum Access Tables & Databases
    Replies: 1
    Last Post: 02-09-2015, 11:45 PM
  5. [SOLVED] Selected Listbox Item to Textbox
    By TheRobsterUK in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-17-2014, 04:07 PM
  6. [SOLVED] Macro to run when item is selected from listbox
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-30-2014, 09:58 PM
  7. [SOLVED] Run 1 macro when any item from listbox is selected
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-30-2014, 09:52 PM

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