QP School

Full Version: Create a hyperlink with VBA
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.