+ Reply to Thread
Results 1 to 9 of 9

Creating a hyperlink with a macro

Hybrid View

  1. #1
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525
    Hi,
    Let's see what you have so far, and go one step at a time.
    Zip an copy of what you have and attach it to your next post,so somebody will be able to look at it

  2. #2
    Forum Expert Simon Lloyd's Avatar
    Join Date
    03-02-2004
    Location
    locked in the cage
    MS-Off Ver
    All the ones my homepage shows
    Posts
    3,161
    Try this:
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim IB As String
    If Target.Row = 1 Then Exit Sub
            If Target.Cells.Count > 1 Then Exit Sub
                If Target.NumberFormat = "[$$-409]#,##0.00" Then
                IB = InputBox("Enter Destination Cell In This Format A1", "Hyperlink Destination", "Type Here")
        ActiveSheet.Hyperlinks.Add Anchor:=Target.Offset(0, 1), Address:="", SubAddress:= _
            "" & IB, TextToDisplay:=Range(IB).Text
                    Else: Exit Sub
                End If
    End Sub
    EDITED AS PREVIOUS CODE DIDN'T WORK CODE ABOVE WORKS FINE!
    Last edited by Simon Lloyd; 11-17-2007 at 01:38 PM.
    Not all forums are the same - seek and you shall find

  3. #3
    Registered User
    Join Date
    11-17-2007
    Posts
    5

    Here is a sample file

    Simon, thanks for the code but I can't get it to work.

    Attached is a sample of the file as i need it to look (it is just a rough format and will change for sure...).
    In any case, you can see "my" macro (which does not work) as well as Simon's macro (in sheet1).

    Thanks for your time,

    Dror
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Dror,

    This macro uses the ActiveCell's text as the text for the Hyperlink. The Hyperlink will go to any cell on any Worksheet the user chooses with the InputBox.
    Sub Link2Cell()
    
      Dim LinkCell As Range
      
        Set LinkCell = Application.InputBox( _
            Prompt:=Prompt, _
            Title:=Title, _
            Default:=ActiveCell.Address, _
            Type:=8)
        
        SubAddx = LinkCell.Parent.Name & "!" & LinkCell.Address
        LinkCell.Hyperlinks.Add Anchor:=ActiveCell, Address:="", _
                                SubAddress:=SubAddx, _
                                TextToDisplay:=ActiveCell.Text
          
    End Sub
    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    11-17-2007
    Posts
    5

    Thumbs up

    Works like a charm.
    Thank you all for your help!

  6. #6
    Registered User
    Join Date
    11-17-2007
    Posts
    5

    Red face Another linking question

    Hi there,
    It’s me again… Can I possibly ask for another favor?

    Now that this hyperlink macro works so nicely, I thought it would be a great idea if while linking cell X to cell Y, the macro will create a “back link” from Y to X.

    Since all links (in this specific file) will always be 1:1 (and unique), I want the user to be able to navigate easily between the origin cell and the target cell and BACK.
    If there is an automatic back-link, the user will be able to click on the origin cell, jump to the target cell (and check whatever he wants) and then click the same cell and jump back to the origin cell (sort of like a “back” feature).

    I think the best way will be to do it a two-step action. The first will be to create the initial link (this is what the macro is doing now) and the second step will be to popup another box and ask the user if he wants to create a back-link (if “yes” – do the back-link to the origin cell. If “no” – exit the macro).

    I thank you in advance for your time and support,

    Regards,

    Dror

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Dror,

    This macro creates a hyperlink to the cell of the user's choosing, and then asks to create a backward hyperlink.
    'Written: November 18, 2007
    'Author: Leith Ross
    'Summary: Create a hyperlink to a cell of the user's choosing, and ask
    '         to create a hyperlink back to the first hyperlink.
    
    Sub Link2CellAndBack()
    
      Dim Answer As Variant
      Dim FirstAddx As String
      Dim LinkCell As Range
      Dim Prompt As String
      Dim SubAddx As String
      Dim Title As String
        
        Title = "Hyperlink Cells"
        Prompt = "Select the cell you want to hyperlink to, and click OK." & vbCrLf _
               & vbLf _
               & "You will then be asked if you want to create a link back" & vbCrLf _
               & "to the first hyperlink."
        FirstAddx = ActiveCell.Parent.Name & "!" & ActiveCell.Address
           
        On Error Resume Next
          Set LinkCell = Application.InputBox( _
              Prompt:=Prompt, _
              Title:=Title, _
              Default:=ActiveCell.Address, _
              Type:=8)
         'User selected Cancel Button
          If Err.Number = 424 Then Exit Sub
        On Error GoTo 0
        
       'Create the first Hyperlink
        SubAddx = LinkCell.Parent.Name & "!" & LinkCell.Address
        LinkCell.Hyperlinks.Add Anchor:=ActiveCell, Address:="", _
                                SubAddress:=SubAddx, _
                                TextToDisplay:=ActiveCell.Text
                                
         'Ask to create a Backward Hyperlink
          Answer = MsgBox(Prompt:="Do you want to create a backward link?", _
                          Buttons:=vbInformation + vbYesNo + vbDefaultButton2, _
                          Title:="Hyperlink Back")
            If Answer = vbYes Then
               LinkCell.Hyperlinks.Add Anchor:=LinkCell, Address:="", SubAddress:=FirstAddx
            End If
        
    End Sub
    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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