Results 1 to 7 of 7

Thread: Dynamic Event Handling in Excel VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    3

    Dynamic Event Handling in Excel VB

    Hi

    I'm a VBforums newbie and VB amateur. I've search the www and this forum extensively but still can't find the answer to my question (although I'm sure it's there). (Great forum BTW).

    The problem:

    In Excel VB I am dynamically creating a form with sliders (from the MSComctlLib.Slider library), the number of which is varyable (5 to 100). I can name the sliders and create textboxes with corresponding numbers to display the value of the slider (i.e. slider01 : textbox01, slider02 : textbox02...slider98 : textbox98 etc).

    But I cannot get a compact code that will basically say "when you click a slider, find the number of the slider and do stuff".

    I've tried:
    Private WithEvents newSlider As Slider
    but can't get it to work. Similarly when I try Private WithEvents newlbl As Label (for instance), I get the error "Object does not source automation events".

    I've fiddled with class modules but failed.

    Can anyone out there give me any pointers?

    Thanks - sorry if this is a repeated question

    TW

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Dynamic Event Handling in Excel VB

    Welcome to the Forums.

    Can you not just pass the name of the slider to a sub to tell you how to act:

    VB Code:
    1. Sub Slidecode(ByVal slider as string)
    2.   Select Case slider
    3.    Case "slider1"
    4.     'Do something
    5.    Case "slider2"
    6.     'Do something else
    7.   End Select
    8.  End Sub

    and

    VB Code:
    1. Call Slidecode("Slider1")

    or something along those lines?

    Or is the issue around attaching a generic slider handling code to an indeterminate slider? How are you generating the sliders?


    zaza
    Last edited by zaza; Dec 14th, 2005 at 06:03 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    3

    Re: Dynamic Event Handling in Excel VB

    Thanks for your prompt reply. I probably didn't explain correctly - I'm not having a problem managing the slider, I'd just rather not have to write hundreds of :
    Sub slider1_Click()
    ...
    Sub slider2_Click()
    ...
    Sub slider3_Click()
    ...
    with the ... code being virtually the same.

    Is there a way to have a:
    Sub genericslider_Click(carry the object and its value)
    ?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Dynamic Event Handling in Excel VB

    I don't think you can use control arrays in VBA (which is what Excel etc use instead of VB), which takes the best option away.

    What you can do tho is write your generic sub with a header like this:
    VB Code:
    1. Sub genericslider_Click(theSlider as Slider)
    2.   'you can now use [b]theSlider[/b] as whichever slider was passed.
    ..and you can call it like this:
    VB Code:
    1. Sub slider1_Click()
    2.   genericslider_Click slider1
    3. End Sub

    While it's not a perfect solution, it should be a significant improvement.

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    3

    Re: Dynamic Event Handling in Excel VB

    Yep - I'd already thought of that. It does mean writing a hundred Sub sliderNN_Click() routines and I was hoping to be a little more elegant than that!!

  6. #6
    New Member
    Join Date
    Jan 2006
    Location
    Québec (Québec), CANADA
    Posts
    4

    Re: Dynamic Event Handling in Excel VB

    I'm having the same problem. Did you find a solution?

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Dynamic Event Handling in Excel VB

    I don't think there is any way (in VBA) of improving on what I posted above.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width