+ Reply to Thread
Results 1 to 19 of 19

Assigning macros to a dynamically created userform

  1. #1
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Assigning macros to a dynamically created userform

    Hi there.

    I'm trying to make a userform which will come up with a list of all sheets in the workbook (both hidden and unhidden) such that when you click on the name of a sheet the sheet is then selected.

    Essentially I'm trying to create a contents page.

    The code I have so far will populate the Userform with the names of the sheets:

    Please Login or Register  to view this content.

    This code works and the spacing is good. Now I want to add a macro to each label which goes along the lines of :

    Please Login or Register  to view this content.
    Suggestions on adding this code dynamically?

  2. #2
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Assigning macros to a dynamically created userform

    it would be much easier to add a listbox and button to the form at design time and add code to select whichever sheet is selected in the listbox list (which can also autopopulate when the form is loaded)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  3. #3
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by JosephP View Post
    it would be much easier to add a listbox and button to the form at design time and add code to select whichever sheet is selected in the listbox list (which can also autopopulate when the form is loaded)
    Hi, thanks for your reply.

    Whilst this would probably be easier, I'm still keen on working under my original idea (I think it's neater to have a single click than two clicks).

    Do you have any ideas on how to create a macro for each "click" event?

    Thanks,

    Tom

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

    Why would you need a double click with a listbox?

    Just use the click(or change) event of the listbox.
    If posting code please use code tags, see here.

  5. #5
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by Norie View Post
    Tom

    Why would you need a double click with a listbox?
    Well you'd have to select the sheet you wanted (click the list box) then you'd have to click the command button. It would be easier to just scroll through the list of sheets (there could be 40 lets say) and then just click the name of the sheet and it takes you to the sheet.

  6. #6
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Assigning macros to a dynamically created userform

    Whilst I agree with Joseph and Norie, here's the alternative you were asking about:

    Userform Initialize:
    Please Login or Register  to view this content.
    Class cNavigation:
    Please Login or Register  to view this content.
    I still think you'd be better off with a listbox though
    Last edited by Kyle123; 04-19-2013 at 06:41 AM.

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

  8. #8
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by Kyle123 View Post
    Whilst I agree with Joseph and Norie, here's the alternative you were asking about:

    I still think you'd be better off with a listbox though

    P.S this raises a question I hadn't thought about before, why is xlSheetVisible -1, shouldn't it be 1 for true - actually just realised that true is -1 in VBA
    Thanks Kyle!

    When adding this code to my UserForm, I haven't come across Class cNavigation before (I'm a bit of a novice still). Where should I put the code that your have written? See picture attached:

    UserForm code.jpg

    Thanks again!

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Assigning macros to a dynamically created userform

    You need to create a class module and name it cNavigation, the code then goes in there

  10. #10
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by Kyle123 View Post
    You need to create a class module and name it cNavigation, the code then goes in there

    omg exciting!

    This is most excellent.

    I am in your debt good Sir.

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

    Re: Assigning macros to a dynamically created userform

    Here's the code you would need for a listbox.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by Kyle123 View Post
    You need to create a class module and name it cNavigation, the code then goes in there
    Hello again,

    I might have made a mistake but the code doesn't appear to update the sheets to more than just the standard "Sheet 1", "Sheet 2" and "Sheet 3". Therefore when there are different named sheets, it bugs out because it is trying to access a sheet named "Sheet 1" when such a sheet doesn't exist...

    Does the code include memory at any stage so that it remembers the sheet setup of the first time it was activated and then keeps that? (Note that I'm trying to make this into an AddIn, will this not work in this set up?)

    See pic attached.

    Can you check this?

    issue.jpg

    Thanks,

    Tom
    Last edited by moosemaster23; 04-19-2013 at 07:04 AM.

  13. #13
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Assigning macros to a dynamically created userform

    Kyle's code lists the sheets in the workbook where the form is. perhaps you want to use activeworkbook instead of thisworkbook

  14. #14
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Assigning macros to a dynamically created userform

    IT works fine on mine, I suspect you have switched workbooks? Are you expecting the code to work on any workbook without updating it?

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Assigning macros to a dynamically created userform

    Variation on Norie's code to work for the active workbook:

    Please Login or Register  to view this content.

  16. #16
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by Kyle123 View Post
    IT works fine on mine, I suspect you have switched workbooks? Are you expecting the code to work on any workbook without updating it?
    Yes. I wanted to make an AddIn.

  17. #17
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Assigning macros to a dynamically created userform

    Does my variation on Norie's code do what you want? it's far more simple than adapting my original code

  18. #18
    Registered User
    Join Date
    02-01-2013
    Location
    Haywards Heath
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Assigning macros to a dynamically created userform

    Quote Originally Posted by Kyle123 View Post
    Does my variation on Norie's code do what you want? it's far more simple than adapting my original code
    Thanks Kyle, it does work.

    The code you provided does what I need. I don't really understand why there's such an advance in difficulty when looking at the problem as originally stated but that doesn't matter.

    Thanks a lot everyone!

  19. #19
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: Assigning macros to a dynamically created userform

    It's more difficult since you need to remove the controls (labels) that you've added each time the workbook is changed. This adds complexity, whereas a listbox has a clear method that handles it.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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