Hyperlink Macro!

MichaelRSnow

Active Member
Joined
Aug 3, 2010
Messages
409
Hello All, help please!

I would like to create a hyperlink via a macro, basically I have two workbooks, when I save and exit one workbook called ‘Workbook A’ I would like that save addressed to then be linked to cell C3 in ‘Workbook B’<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
This is my save and exit code<o:p></o:p>
<o:p> </o:p>
Private Sub CommandButton1_Click()<o:p></o:p>
Dim sFile As String<o:p></o:p>
ThisFile = Range("B3").Value<o:p></o:p>
ActiveWorkbook.SaveAs Filename:="L:XXXX\" & ThisFile<o:p></o:p>
Application.Quit<o:p></o:p>
End Sub<o:p></o:p>
<o:p> </o:p>
Can you help?<o:p></o:p>
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hello MichaelRSnow;

You can give a swing @ the below function:
[Workbook B is expected to be open in this case.]

Code:
Function RecordHyperlinkToExternal(ThisWB$, ThatWB$, ThatWS$, ThatRangeAdd$)
'---------------------------------------------------------------------------------------
' Procedure : RecordHyperlinkToExternal
' Author    : tweedle
' Date      : 2011-08-19
' Citation  : [URL]http://www.mrexcel.com/forum/showthread.php?t=572957[/URL]
' Purpose   : Creates Hyperlinks in Workbooks other than ThisWorkbook
' Usage     :
'---------------------------------------------------------------------------------------
'Application.ScreenUpdating = False
'retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "Book1.xlsx", "Sheet1", "C1")
'Application.ScreenUpdating = True
'Parm1 = ThisWorkbook.Name
'Parm2 = String Name of Target Workbook
'Parm3 = Sheet Name in Target Workbook
'Parm4 = Cell Address in Target Workbook to place Hyperlink
'---------------------------------------------------------------------------------------
'
Application.ScreenUpdating = False
retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "ThisErrors.blah", "Hyperlinks", "C5")
Application.ScreenUpdating = True
   On Error GoTo RecordHyperlinkToExternal_Error
 
    RecordHyperlinkToExternal = False
    Dim newhlink As Hyperlink
    Dim Anch As Object
 
    ThatIsOpen = False
    For Each Window In Application.Windows
        If Window.Caption = ThatWB$ Then
            ThatIsOpen = True
            Window.Activate
        End If
    Next
 
    If ThatIsOpen Then
        Set Anch = Workbooks(ThatWB$).Sheets(ThatWS$).Range(ThatRangeAdd$)
        Set newhlink = Workbooks(ThatWB$).Sheets(ThatWS$).Hyperlinks.Add( _
                       Anch, _
                       Workbooks(ThisWB$).FullName, _
                       TextToDisplay:=ThisWB$)
 
        RecordHyperlinkToExternal = True
    Else
        RecordHyperlinkToExternal = False
        MsgBox "Could not find " & ThatWB$ & " to create Hyperlinks ", vbCritical, "RecordHyperlinkToExternal-Automation Error"
    End If
   On Error GoTo 0
   Exit Function
RecordHyperlinkToExternal_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure RecordHyperlinkToExternal of Module Module1"
 
End Function
 
Upvote 0
Thank you for this, looks complicated, how do I call this function. Never done anything with functions before!

Hello MichaelRSnow;

You can give a swing @ the below function:
[Workbook B is expected to be open in this case.]

Code:
Function RecordHyperlinkToExternal(ThisWB$, ThatWB$, ThatWS$, ThatRangeAdd$)
'---------------------------------------------------------------------------------------
' Procedure : RecordHyperlinkToExternal
' Author    : tweedle
' Date      : 2011-08-19
' Citation  : [URL]http://www.mrexcel.com/forum/showthread.php?t=572957[/URL]
' Purpose   : Creates Hyperlinks in Workbooks other than ThisWorkbook
' Usage     :
'---------------------------------------------------------------------------------------
'Application.ScreenUpdating = False
'retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "Book1.xlsx", "Sheet1", "C1")
'Application.ScreenUpdating = True
'Parm1 = ThisWorkbook.Name
'Parm2 = String Name of Target Workbook
'Parm3 = Sheet Name in Target Workbook
'Parm4 = Cell Address in Target Workbook to place Hyperlink
'---------------------------------------------------------------------------------------
'
Application.ScreenUpdating = False
retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "ThisErrors.blah", "Hyperlinks", "C5")
Application.ScreenUpdating = True
   On Error GoTo RecordHyperlinkToExternal_Error
 
    RecordHyperlinkToExternal = False
    Dim newhlink As Hyperlink
    Dim Anch As Object
 
    ThatIsOpen = False
    For Each Window In Application.Windows
        If Window.Caption = ThatWB$ Then
            ThatIsOpen = True
            Window.Activate
        End If
    Next
 
    If ThatIsOpen Then
        Set Anch = Workbooks(ThatWB$).Sheets(ThatWS$).Range(ThatRangeAdd$)
        Set newhlink = Workbooks(ThatWB$).Sheets(ThatWS$).Hyperlinks.Add( _
                       Anch, _
                       Workbooks(ThisWB$).FullName, _
                       TextToDisplay:=ThisWB$)
 
        RecordHyperlinkToExternal = True
    Else
        RecordHyperlinkToExternal = False
        MsgBox "Could not find " & ThatWB$ & " to create Hyperlinks ", vbCritical, "RecordHyperlinkToExternal-Automation Error"
    End If
   On Error GoTo 0
   Exit Function
RecordHyperlinkToExternal_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure RecordHyperlinkToExternal of Module Module1"
 
End Function
 
Upvote 0
To call it:
Workbook B is expected to be open.


Code:
Application.ScreenUpdating = False
retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "Workbook B.xlsx", "Sheet1", "C4")
Application.ScreenUpdating = True


This bit keeps the screen from any flickering during update
Code:
Application.ScreenUpdating = False

This bit actually calls the function:
Code:
retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "Workbook B.xlsx", "Sheet1", "C4")

retval is just a variable to which the function returns its result [True/False of success]
--ThisWorkbook.Name is used literally 'as is'
--"Workbook B.xlsx" is the name of your Workbook B
--"Sheet1" is the name of the worksheet on which to put the hyperlink
--"C4" is the address of the cell to put the hyperlink


This turns the screens updates back on [normal]
Code:
Application.ScreenUpdating = True




I see I had extranious test lines in the original post-this is clean[er].

Code:
Function RecordHyperlinkToExternal(ThisWB$, ThatWB$, ThatWS$, ThatRangeAdd$)
'---------------------------------------------------------------------------------------
' Procedure : RecordHyperlinkToExternal
' Author    : tweedle
' Date      : 2011-08-19
' Citation  : [URL]http://www.mrexcel.com/forum/showthread.php?t=572957[/URL]
' Purpose   : Creates Hyperlinks in Workbooks other than ThisWorkbook
' Usage     :
'---------------------------------------------------------------------------------------
'Application.ScreenUpdating = False
[COLOR=blue]'retval = RecordHyperlinkToExternal(ThisWorkbook.Name, "Book1.xlsx", "Sheet1", "C1")[/COLOR]
'Application.ScreenUpdating = True
'Parm1 = ThisWorkbook.Name
'Parm2 = String Name of Target Workbook
'Parm3 = Sheet Name in Target Workbook
'Parm4 = Cell Address in Target Workbook to place Hyperlink
'---------------------------------------------------------------------------------------
'
   On Error GoTo RecordHyperlinkToExternal_Error
 
    RecordHyperlinkToExternal = False
    Dim newhlink As Hyperlink
    Dim Anch As Object
 
    ThatIsOpen = False
    For Each Window In Application.Windows
        If Window.Caption = ThatWB$ Then
            ThatIsOpen = True
            Window.Activate
        End If
    Next
 
    If ThatIsOpen Then
        Set Anch = Workbooks(ThatWB$).Sheets(ThatWS$).Range(ThatRangeAdd$)
        Set newhlink = Workbooks(ThatWB$).Sheets(ThatWS$).Hyperlinks.Add( _
                       Anch, _
                       Workbooks(ThisWB$).FullName, _
                       TextToDisplay:=ThisWB$)
 
        RecordHyperlinkToExternal = True
    Else
        RecordHyperlinkToExternal = False
        MsgBox "Could not find " & ThatWB$ & " to create Hyperlinks ", vbCritical, "RecordHyperlinkToExternal-Automation Error"
    End If
   On Error GoTo 0
   Exit Function
RecordHyperlinkToExternal_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure RecordHyperlinkToExternal of Module Module1"
 
End Function
 
Upvote 0

Forum statistics

Threads
1,224,548
Messages
6,179,451
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top