📘 Excel逆引き事典

【VBA】ハイパーリンクを配列を使って高速に値をクリアする方法

日々の業務で大量のハイパーリンクを管理していると、その更新や削除に時間がかかることがあります。この記事では、配列を使用して高速にハイパーリンク情報をクリアする方法を紹介します。

サンプルコード

VBA
Option Explicit
Sub ClearHyperlinks()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("シート1")
    Application.ScreenUpdating = False
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Dim hyperLinks() As Variant
    ReDim hyperLinks(1 To lastRow)
    For i = 1 To lastRow
        If Not IsEmpty(ws.Range("A" & i)) Then
            If ws.Hyperlinks(i) Is Nothing Then Exit Sub
            hyperLinks(i) = ws.Hyperlinks(i).Address
        End If
    Next i
    ' 配列をクリア
    Dim j As Long
    For j = LBound(hyperLinks) To UBound(hyperLinks)
        If Not IsEmpty(ws.Range("A" & j)) Then
            ws.Hyperlinks(j).Delete
        End If
    Next j
    Application.ScreenUpdating = True
End Sub

よくある質問

Q 元に戻せますか?

A.
VBAの実行結果は「元に戻す」が効きません。必ずバックアップを取ってから実行してください。

Q エラーが出たら?

A.
シート名や列番号が正しいか確認してください。