+ Reply to Thread
Results 1 to 15 of 15

How Call Sub within a Listbox Click Event

  1. #1
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    How Call Sub within a Listbox Click Event

    I'm wanting to put the highlighted section in a Sub to be called from within the With listAccounts section to help make my code more readable later on. This section changes labels to Bold depending on which item in the Listbox (listAccounts) is selected. If another item in the Listbox is chosen, then those non-related labels would toggle back to non-Bold and the related labels would Bold.

    The section after the Dim's is what resets those labels to non-Bold. If I pull the Se lect Case section out and call it from within the If .Selected(i) section, it works just fine, but the reset to non-Bold doesn't want to work.
    (Note: I had to insert an extra space character in Select so the forum firewall wouldn't block this post.)
    Please Login or Register  to view this content.
    This is what the labels look like. bold labels.png
    TW? and NTW should never be Bold at the same time, yet that is what happens if I put the Se lect Case section into it's own Sub. I'm sure I'm simply not putting the Reset to non-Bold section in the right place in the Sub.

    Here is my attempt at moving the Se lect Case out by itself:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Also, why won't the List Click event run the Reset to non-Bold when a new Listboxk item is clicked even if it is in both the Click procedure and the TrialWorthiness_Labels subroutine?

    Thank you,
    Delain
    (Note: I had to insert an extra space character in Select so the forum firewall wouldn't block this post.)
    I say this optimistically . . . One day I'll understand it.
    But today is not that day!

  2. #2
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    As far as I can see from just the code there are 2 things that are probably not right:

    The call to TrialWorthiness_Labels is in the loop going thru the item of listAccounts.
    Since TrialWorthiness_Labels resets all labels back to non-bold each label that is set to bold in listAccounts_Click is immediately reset to non-bold.
    So I think you should remove the Reset-to-non-Bold part in TrialWorthiness_Labels.

    Secondly in TrialWorthiness_Labels the index i is used, but that index never gets a value.
    If you want to use the value of i as set in listAccounts_Click pass it as a parameter.
    Cheers!
    Tsjallie




    --------
    If your problem is solved, pls mark the thread SOLVED (see Thread Tools in the menu above). Thank you!

    If you think design is an expensive waste of time, try doing without ...

  3. #3
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: How Call Sub within a Listbox Click Event

    Thanks for the quick reply, Tsjallie.
    I've tried to implement your suggestions as best as I can determine and we're not quite there. I appreciate your suggestion on passing the value of i since I'm still learning and haven't incorporated passing arguments methodology into my repertoire too much yet.

    Let's back up a bit. Here is the code that does exactly what I need. When an item is selected, the corresponding (TW, TW?, NTW, etc.) labels will Bold. When another list item is chosen, those will non-Bold and the related label will Bold. Good to go.
    Please Login or Register  to view this content.
    When I move the Bold'ing section out to its own Sub, and pass i to it, then not only do the labels not get reset to non-Bold, but the Bold'ing in TrialWorthiness_Labels won't function correctly. Whichever item is selected first, then it's label remains Bold and any subsequently chosen item's label will not Bold at all. The toggling effect is ineffective.
    I highlighted the Call statement above to show how I am passing the index value.
    Is this correct?
    It's commented out for the moment, in that procedure, to show the correctly functioning procedure. I comment out the Bold'ing section and un-comment the Call to test that method.

    Please Login or Register  to view this content.
    Thoughts on this?

    Thank you,
    Delain

  4. #4
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    Can you upload the workbook so I can take a closer look with the debugger to see what's going (and not).

  5. #5
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: How Call Sub within a Listbox Click Event

    This will take some time to get back to you. This is an add-in and has a bunch of other data and forms that I cannot publish. I'll have to figure out how to strip it down to a shareable form. Not going to be fun.

    Thanks,
    Delain

  6. #6
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    I say this optimistically . . . One day VBA will be fun.
    But today is not that day!


    Are the subs in the userform's codemodule or in a common module?
    Last edited by Tsjallie; 11-07-2016 at 05:05 PM. Reason: Question edited

  7. #7
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: How Call Sub within a Listbox Click Event

    Tally ho and pip pip!! Whatever that means . . .

  8. #8
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    Maybe you can post a screenshot of the listbox and the data that goes in it (if not too much and not confidential).

    BTW I see that in the TrialWorthiness_Label you have named the parameter Listindex.
    That should be called i as that's name you're referring to.
    And remove the Dim i as long line.

  9. #9
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: How Call Sub within a Listbox Click Event

    I knew you could do it! You didn't need no stinkin' workbook to figure this out.
    Oh I see you edited your post after I read it and with a thought I was wondering how it might affect things.
    Are the subs in the userform's codemodule or in a common module?
    Everything that you and I have been working on is in the Userform module. What do I need to know about this that might affect things?

    I changed the name of the argument to " i " and removed the Dim and now we have working modular procedures.
    Your valuable and appreciated time was taken up by someone still new to VBA and especially to passing arguments. I thought I read somewhere that it didn't matter what the name of the argument in the receiving procedure is. Okay, so if I'm passing say "Rngrow", then the called Sub needs to have
    Please Login or Register  to view this content.
    Correct?

    This is cool. VBA is very fun when you can use its magic and put it to work.

    Thank you,
    Delain

  10. #10
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    Not sure if I'm even close to the relevant part of your workbook, but did some trial.
    Run the userform and step thru the code. See if gets you any closer.
    Attached Files Attached Files

  11. #11
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    Indeed it doesn't matter how you call the parameter as long as it has the same datatype as the value you're passing and as long you're using that name in the sub receiving the parameter.
    And preferably the name is meaningfull to you. That also improves readability of your code.

  12. #12
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    Everything that you and I have been working on is in the Userform module. What do I need to know about this that might affect things?
    Important to know about this is that everything you put in the userform's codemodule (constants, variables, subs and functions) or only accessible to that userform.
    If need to access thing outside it you need to put them in a common module and declare them public.

  13. #13
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: How Call Sub within a Listbox Click Event

    Now I'm a bit confused. You saw that I (we) solved the problem of the TrialWorthiness_Labels not working by doing what you suggested: changing the argument to " i "??
    After that, the labels behave just as needed and as in the workbook you set up in #10. This was in a Solved condition prior to you sending the workbook.
    Is there something additional or different that I need to see in that? If so, please share so I won't be so handicapped.

    Delain

  14. #14
    Forum Expert Tsjallie's Avatar
    Join Date
    09-15-2012
    Location
    NL
    MS-Off Ver
    2010, 2013, 2016
    Posts
    2,077

    Re: How Call Sub within a Listbox Click Event

    No there's nothing extra.
    While you were solving the problem in your workbook I was trying to create a demo version of it.
    Conclusion must be that the parameter was the culprit.

  15. #15
    Forum Contributor delaing's Avatar
    Join Date
    07-16-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    192

    Re: How Call Sub within a Listbox Click Event

    Very good work; thanks.

+ 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. Call a commandbutton click event from a module
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-19-2015, 01:10 PM
  2. [SOLVED] Call userform control click event indirectly
    By ryan.wherry in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 03-26-2015, 11:42 AM
  3. Prevent ListBox Click Event Until Mouse Click
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-08-2011, 12:23 PM
  4. Listbox Click event
    By scottintexas in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-18-2008, 11:11 AM
  5. [SOLVED] call a function on control click event
    By tkraju via OfficeKB.com in forum Excel General
    Replies: 4
    Last Post: 08-23-2006, 12:55 AM
  6. [SOLVED] Click event on listbox
    By IanC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2005, 02:08 AM
  7. [SOLVED] click event on listbox
    By CStephenson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-22-2005, 03:06 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