+ Reply to Thread
Results 1 to 5 of 5

Userform with dropdown to select worksheet

  1. #1
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Userform with dropdown to select worksheet

    I am seeking some help and advice on how to set up a userform with a combobox to enable the user to select a specific worksheet from within another workbook.

    To give some context, I have a master workbook which holds data on apprentices and tracks their progress. Every month, each of around 30 training providers sends me an update on their learners in the form of a workbook (in a standard format). To make things more efficient, I have written some VBA which automatically imports the data from the updated cells in each workbook into my master workbook. My code makes a number of checks on each worksheet prior to import to make sure the data is valid and this all works perfectly. However, I occasionally receive workbooks which have additional sheets and I would like to add the ability to select the correct worksheet to import. What I'm aiming for is, when multiple sheets are detected in a workbook, a userform pops up with a combobox populated with the worksheet names and asking the user to select which worksheet to import data from. I'm having difficulty understanding how to structure this within my code, which bits of code need to go where and, in particular, how I pass arguments to and from the userform from my main code (I'd prefer to avoid global variables). I believe I need to pass the workbook to the userform so that it can populate the combobox with worksheet names and then pass the selected worksheet back to my main code if that's possible.

    I hope this makes sense and any advice would be gratefully received.

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

    Re: Userform with dropdown to select worksheet

    I use public function/subs in the second userform to pass information between it and main userform.

    The example contains 2 userforms.

    Userform1 has a button to display userform2, which lists worksheets
    Please Login or Register  to view this content.
    Userform2 has combobox and 2 buttons (Cancel/OK)

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Re: Userform with dropdown to select worksheet

    Thanks Andy, that looks to be along the lines of what I need to do. One thing I'm still struggling with is variable types. I appreciate that the combobox needs to be populated with string variables (ie. sht.Name) and it therefore returns a string variable too. In my existing code, I'm referencing the source workbook with a workbook variable (SourceWB). I'm probably missing something obvious, but I don't know how to correlate the two. In my code, I count the number of worksheets. If only one, then I set SourceWS = SourceWB.Sheets(1) but if more than one, I want to Set SourceWS to the worksheet selected in the combobox.

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

    Re: Userform with dropdown to select worksheet

    The worksheets collection can return a sheet using either a index or key reference.

    You are currently using an index, where as in my code I am using a key. The key is the same as the worksheet name

    Please Login or Register  to view this content.
    Note that I have used the worksheets collection, which only contains worksheets. Where as you use the Sheets collection which includes all sheet types, such as worksheet and chartsheet.

  5. #5
    Registered User
    Join Date
    05-31-2020
    Location
    Norfolk, England
    MS-Off Ver
    365
    Posts
    22

    Re: Userform with dropdown to select worksheet

    Ah of course... as I guessed, I was overlooking the obvious. I've incorporated the relevant bits of your UserForm1 code into my main code module (as I don't require the first userform) and all seems to be working perfectly. UserForm2 pops up whenever a multi sheet workbook is selected for import... Perfect!! Many thanks again for your help... it's really appreciated

+ 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. Replies: 1
    Last Post: 01-17-2018, 01:26 PM
  2. [SOLVED] Userform with Dropdown list and the ability to select more than one
    By katiedee1625 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-23-2016, 03:13 PM
  3. Replies: 5
    Last Post: 04-23-2016, 11:34 AM
  4. Replies: 4
    Last Post: 09-10-2013, 02:11 PM
  5. Replies: 2
    Last Post: 04-22-2012, 01:44 PM
  6. Userform SelectRange - Select from different worksheet
    By Jessica.Bush in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-20-2010, 05:44 PM
  7. save data to worksheet dependent on userform dropdown
    By mhill in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2009, 03:57 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