Attaching Hyperlink To Dynamically-Created Command Button Within a Userform

  • I am trying to attach a hyperlink (or its equivalent) to a dynamically-created command button (or other clickable object) within a Userform using Excel 2003.


    For clarification, I would like to dynamically create a clickable button that takes you to a specific URL.
    The idea is to have the main code create a list of buttons on a userform (I know how to do this), but each one having its own hyperlink equivalent (for example, clicking on it calls a specific subroutine found elsewhere).


    I can accomplish this with pre-generated buttons, but I cannot seem to do this with dynamically-created ones.


    Any suggestions?


    Thanks for any help you may have to offer.
    (I realized I violated the rules for hijacking a thread, so I created a new thread. My apologies.)

  • Re: Attaching Hyperlink To Dynamically-Created Command Button Within a Userform


    I have used classes before, but I cannot claim to understand them fully. I created a class in the past to generate buttons like this before, but for other means, and the way I did it is not exactly applicable AFAIK. If I knew more, maybe that would be the answer, but I'm not educated enough on the subject of Classes to ask the right questions. If I knew how to apply classes for this particular application, I would do it. Perhaps I'll try to figure it out, all the while hoping for a different (yet probably less elegant) solution.


    To answer how hyperlinks would be determined, I read the URLs from the destination from a source, an html-based website (this part is done). What will be created is a list of "Topics" and buttons for editing and updating these topics, but the data resides on a web server elsewhere (make sense?). So the question becomes how to 'deliver' the URLs to the buttons/labels/whatever for the user to click on?

  • Re: Attaching Hyperlink To Dynamically-Created Command Button Within a Userform


    Like this:


    Form Code:


    Class1 Code:

  • Re: Attaching Hyperlink To Dynamically-Created Command Button Within a Userform


    WOW. Now that's what I call elegant.


    I took the suggestion of using classes, resurrecting my old Class code, and I got it to work. After looking at it for a while (I've learned a lot since incorporating it a couple years ago), I understand it a bit better.
    However, the way you've done it is MUCH smoother. I will definitely have to examine this a bit more! I've never done custom Properties, as you have here. I can't say I understand the Let vs. Get; as it appears, it seems to be recursive. I just have to study up some more.


    In my new class/WithEvents code, I just passed the URL for each button using the forms.commandbutton's .Tag property to the Class code.


    Thank you so much for the example, this helps a TON.


    You all rock.

  • Re: Attaching Hyperlink To Dynamically-Created Command Button Within a Userform


    No problem, there's nothing recursive in there. For this example the letting and getting could be removed and the variables made public. All the Get and Let do is assign/return values from private variables.


    This would have been just as valid:

    Code
    Public WithEvents btn As msforms.CommandButton 
    Public hyperlink As String 
    
    
    
    
    Private Sub p_btn_Click() 
        ThisWorkbook.FollowHyperlink hyperlink 
    End Sub

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!