QP School
Create a hyperlink with VBA - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: Excel VBA Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=48)
+--- Thread: Create a hyperlink with VBA (/showthread.php?tid=5267)



Create a hyperlink with VBA - Qomplainerz - 07-27-2023

Example:

Sub CreateHyperlink()
    Dim ws As Worksheet

    'Set the worksheet where the hyperlink will be created
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Create a hyperlink in cell A1
    ws.Hyperlinks.Add _
        Anchor:=ws.Range("A1"), _
        Address:="https://www.example.com", _
        TextToDisplay:="Visit Example Website"
End Sub

Explanation:

The code defines a subroutine named CreateHyperlink.
It declares a variable for the worksheet (ws) where the hyperlink will be created.
Set ws = ThisWorkbook.Sheets("Sheet1") specifies the worksheet ("Sheet1") where the hyperlink will be added.
The ws.Hyperlinks.Add method is used to create a hyperlink.
Anchor:=ws.Range("A1") sets cell A1 as the anchor for the hyperlink (where the link will be displayed).
Address:="https://www.example.com" specifies the URL or address to which the hyperlink points.
TextToDisplay:="Visit Example Website" sets the text that will be displayed for the hyperlink in cell A1.