Hipervínculos VBA

Los hipervínculos son URL adjuntas a un valor que se ve cuando pasamos el mouse sobre él y cuando hacemos clic en él, se abre la URL, en VBA tenemos una propiedad incorporada para crear hipervínculos en VBA y para usar esta propiedad usamos el método Add junto con la declaración de hipervínculo para insertar un hipervínculo en una celda.

Hipervínculos en Excel VBA

A pesar de que tenemos las teclas de acceso directo Page Up & Page Down en Excel para pasar de una hoja a otra. Pero se vuelve complejo cuando tenemos que movernos entre 10 y más hojas de trabajo. Aquí es donde entra en escena la belleza de los "Hipervínculos en Excel". El hipervínculo es una URL predeterminada que lo lleva a la celda u hoja de trabajo respectiva según lo asignado.

Todos sabemos cómo crear hipervínculos en la hoja de trabajo para pasar rápidamente de una hoja a otra y también puede ir a cualquier otra hoja. Pero en el artículo de hoy, le mostraremos cómo crear hipervínculos utilizando la codificación VBA.

Fórmula de hipervínculos VBA

Veamos la fórmula de los hipervínculos en Excel VBA.

  • Ancla: en qué celda le gustaría crear un hipervínculo.
  • Dirección: ¿Cuál es la URL del hipervínculo para navegar?
  • [Subdirección]: ¿Cuál es la ubicación de la página?
  • [Sugerencia en pantalla]: ¿Cuál es el valor que se muestra cuando coloca el puntero del mouse en el nombre o la celda del hipervínculo?
  • [Texto para mostrar]: ¿Cuál es la prueba que se mostrará en la celda? Por ejemplo, Nombre de la hoja de trabajo.

¿Cómo crear hipervínculos en Excel VBA?

Puede descargar esta plantilla de hipervínculos VBA aquí - Plantilla de hipervínculos VBA

Suponga que desea crear un hipervínculo VBA a la hoja denominada "Hoja principal" de la otra hoja "Ejemplo 1".

En la hoja de trabajo “Ejemplo 1” y en la celda A1, voy a crear el hipervínculo usando Código en VBA.

Paso 1: Primero seleccione la celda A1 del Ejemplo 1 de la hoja de trabajo.

Código:

 Sub Hyperlink_Example1 () Worksheets ("Example 1"). Seleccione Range ("A1"). Seleccione End Sub 

Paso 2: Ahora, utilizando los hipervínculos abiertos del objeto Active Cell. agregar método.

Código:

 Sub Hyperlink_Example1 () Worksheets ("Ejemplo 1"). Seleccione Rango ("A1"). Seleccione ActiveCell.Hyperlinks.Add (End Sub 

Paso 3: El primer  argumento es "Anchor", es decir, en qué celda enlazaríamos para crear el hipervínculo VBA. En este caso la celda A1 y como ya hemos seleccionado la celda A1 para mencionarla como “Selección”.

Código:

 Sub Hyperlink_Example1 () Worksheets ("Ejemplo 1"). Seleccione Rango ("A1"). Seleccione ActiveCell.Hyperlinks.Add (Selection, End Sub 

Paso 4: No estamos creando ninguna dirección aquí, así que ignora la dirección a partir de ahora.

Código:

 Sub Hyperlink_Example1 () Worksheets ("Example 1"). Seleccione Range ("A1"). Seleccione ActiveCell.Hyperlinks.Add Anchor: = Selection, Address: = "", End Sub 

Paso 5: Lo siguiente es la subdirección. Aquí debemos mencionar a qué hoja nos referimos y la primera celda de esa hoja.

Código:

 Sub Hyperlink_Example1 () Worksheets ("Ejemplo 1"). Seleccione Range ("A1"). Seleccione ActiveCell.Hyperlinks.Add Anchor: = Selection, Address: = "", SubAddress: = "'Main Sheet'! A1", End Sub 

He mencionado el nombre de la hoja como "Hoja principal" y en esa hoja la dirección de celda es "A1".

Paso 6: Ignore también la sugerencia en pantalla. Para que se muestre el texto, mencione el nombre de la hoja.

Código:

 Sub Hyperlink_Example1 () Worksheets ("Ejemplo 1"). Seleccione Range ("A1"). Seleccione ActiveCell.Hyperlinks.Add Anchor: = Selection, Address: = "", SubAddress: = "'Main Sheet'! A1", TextToDisplay : = "Hoja principal" End Sub 

Ok, ejecute este código usando la tecla F5 o manualmente, luego creará un hipervínculo en la celda A1 en la hoja "Ejemplo 1".

Cuando hace clic en el hipervínculo "Hoja principal", se redirige a la hoja principal.

Hipervínculos de varias hojas con bucles

Hemos visto la creación de un hipervínculo VBA para una hoja. Cuando tenemos muchas hojas, es difícil crear un hipervínculo VBA para cada hoja con la misma línea de código para cada hoja.

Suponga que tiene 11 hojas de trabajo como se muestra en la imagen de abajo.

Desea crear un hipervínculo para cada hoja en la hoja de índice usando el código VBA.

Paso 1: Defina la variable como una hoja de trabajo.

Código:

 Sub Create_Hyperlink () Dim Ws As Worksheet End Sub 

Paso 2: Lo primero es seleccionar el índice de la hoja de trabajo y seleccionar la celda A1.

Código:

 Sub Create_Hyperlink () Dim Ws As Worksheet Worksheets ("Índice"). Seleccione Rango ("A1"). Seleccione End Sub 

Paso 3: Ahora abra For Each Loop en VBA.

Código:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets Next Ws End Sub 

Step 4: Since we have already selected the cell A1 it is now an active cell. So start the hyperlink with the active cell.

Code:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets ActiveCell.Hyperlinks.Add( Next Ws End Sub 

Step 5: Anchor is a hyperlink cell. So it is the active cell.

Code:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Next Ws End Sub 

Step 6: Address is nothing mention it as “”.

Code:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets ActiveCell.Hyperlinks.Add Anchor:=ActiveCell,Address:="", Next Ws End Sub 

Step 7: Subaddress is when we loop through the sheet it should be the sheet name. To refer the sheet name we need a single quote “” with sheet name and “! Cell Address” and close the sheet name with a single quote “”.

Code:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets ActiveCell.Hyperlinks.Add Anchor:=ActiveCell,Address:="",SubAddress:=""& Ws.Name&"!A1"&"", Next Ws End Sub 

Step 8: Ignore Screen tip and for Text to display you can enter the worksheet name.

Code:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:="", SubAddress:="" & Ws.Name & "!A1" & "", ScreenTip:="", TextToDisplay:=Ws.Name Next Ws End Sub 

Step 9: To store the hyperlink of each sheet in a different cell every time hyperlink created for one sheet we need to move down one cell from the active cell.

Code:

 Sub Create_Hyperlink() Dim Ws As Worksheet Worksheets("Index").Select Range("A1").Select For Each Ws In ActiveWorkbook.Worksheets ActiveCell.Hyperlinks.Add Anchor:=ActiveCell, Address:="", SubAddress:="" & Ws.Name & "!A1" & "", ScreenTip:="", TextToDisplay:=Ws.Name ActiveCell.Offset(1, 0).Select Next Ws End Sub 

This will create a hyperlink of all the sheets in the Index sheet. This code is dynamic, whenever there is any addition or deletion of sheets we just need to run this code to have an updated hyperlink.