Sub ListAllHyperlinks()
Dim ws As Worksheet
Dim hl As Hyperlink
Dim outputWs As Worksheet
Dim row As Long
' 結果出力用シートを作成
Set outputWs = Worksheets.Add
outputWs.Name = "リンク一覧"
' ヘッダー行を作成
outputWs.Cells(1, 1).Value = "シート名"
outputWs.Cells(1, 2).Value = "セル位置"
outputWs.Cells(1, 3).Value = "表示テキスト"
outputWs.Cells(1, 4).Value = "リンク先"
outputWs.Cells(1, 5).Value = "種類"
row = 2
' 全シートを検索
For Each ws In ActiveWorkbook.Worksheets
For Each hl In ws.Hyperlinks
outputWs.Cells(row, 1).Value = ws.Name
outputWs.Cells(row, 2).Value = hl.Range.Address
outputWs.Cells(row, 3).Value = hl.TextToDisplay
outputWs.Cells(row, 4).Value = hl.Address
outputWs.Cells(row, 5).Value = GetLinkType(hl.Address)
row = row + 1
Next hl
Next ws
' 列幅を自動調整
outputWs.Columns.AutoFit
MsgBox "ハイパーリンク一覧を作成しました"
End Sub
Function GetLinkType(linkAddress As String) As String
If InStr(linkAddress, "http") > 0 Then
GetLinkType = "Webサイト"
ElseIf InStr(linkAddress, "mailto:") > 0 Then
GetLinkType = "メール"
ElseIf InStr(linkAddress, "\") > 0 Then
GetLinkType = "ファイル"
Else
GetLinkType = "内部リンク"
End If
End Function
外部リンクの詳細分析
Sub AnalyzeExternalLinks()
Dim links As Variant
Dim i As Integer
Dim outputWs As Worksheet
' 外部リンクの取得
links = ActiveWorkbook.LinkSources
If IsEmpty(links) Then
MsgBox "外部リンクは見つかりませんでした"
Exit Sub
End If
' 結果出力用シートを作成
Set outputWs = Worksheets.Add
outputWs.Name = "外部リンク分析"
' ヘッダー行
outputWs.Cells(1, 1).Value = "No."
outputWs.Cells(1, 2).Value = "リンク先ファイル"
outputWs.Cells(1, 3).Value = "更新方法"
outputWs.Cells(1, 4).Value = "状態"
' 外部リンクの詳細情報を出力
For i = 1 To UBound(links)
outputWs.Cells(i + 1, 1).Value = i
outputWs.Cells(i + 1, 2).Value = links(i)
outputWs.Cells(i + 1, 3).Value = GetUpdateType(links(i))
outputWs.Cells(i + 1, 4).Value = CheckLinkStatus(links(i))
Next i
outputWs.Columns.AutoFit
MsgBox UBound(links) & "個の外部リンクを分析しました"
End Sub
Function GetUpdateType(linkSource As String) As String
On Error Resume Next
If ActiveWorkbook.UpdateLink(linkSource, xlUpdateStateChart) = xlAutomatic Then
GetUpdateType = "自動更新"
Else
GetUpdateType = "手動更新"
End If
On Error GoTo 0
End Function
Function CheckLinkStatus(linkSource As String) As String
If Dir(linkSource) <> "" Then
CheckLinkStatus = "ファイル存在"
Else
CheckLinkStatus = "ファイル不明"
End If
End Function
【応用編】条件別リンク検索テクニック
特定条件でのリンク検索
ドメイン別Web リンク検索
特定ドメインのリンクのみ抽出
Sub FindLinksbyDomain()
Dim ws As Worksheet
Dim hl As Hyperlink
Dim targetDomain As String
Dim resultList As String
targetDomain = InputBox("検索するドメインを入力してください", "ドメイン検索", ".com")
For Each ws In ActiveWorkbook.Worksheets
For Each hl In ws.Hyperlinks
If InStr(hl.Address, targetDomain) > 0 Then
resultList = resultList & ws.Name & "!" & hl.Range.Address & ": " & hl.Address & vbCrLf
End If
Next hl
Next ws
If resultList = "" Then
MsgBox "該当するリンクが見つかりませんでした"
Else
MsgBox "見つかったリンク:" & vbCrLf & resultList
End If
End Sub
更新日時による検索
最近変更されたリンクの検索
Sub FindRecentlyModifiedLinks()
Dim ws As Worksheet
Dim cell As Range
Dim cutoffDate As Date
cutoffDate = DateAdd("d", -7, Now) ' 1週間以内
For Each ws In ActiveWorkbook.Worksheets
For Each cell In ws.UsedRange
If cell.Hyperlinks.Count > 0 Then
If FileDateTime(cell.Hyperlinks(1).Address) > cutoffDate Then
cell.Interior.Color = RGB(255, 255, 0) ' 黄色でハイライト
End If
End If
Next cell
Next ws
End Sub
フィルター機能との組み合わせ
リンク属性によるフィルタリング
リンク種類別のフィルター
リンク一覧を作成(前述のVBAを使用)
フィルター機能を適用
種類列でフィルタリング
実用的なフィルター条件
Webサイトリンクのみ表示
ローカルファイルリンクのみ表示
エラーリンクのみ表示
特定シートのリンクのみ表示
【トラブル対策】リンク関連の問題解決
リンク切れ(#REF!エラー)の対処
エラーの自動検出
条件付き書式でエラーを可視化
対象範囲を選択
「ホーム」→「条件付き書式」→「新しいルール」
「数式を使用してセルを書式設定」
数式:=ISERROR(A1)
書式:赤色の背景
VBAによるエラー検出
Sub FindBrokenLinks()
Dim ws As Worksheet
Dim cell As Range
Dim brokenCount As Integer
brokenCount = 0
For Each ws In ActiveWorkbook.Worksheets
For Each cell In ws.UsedRange
If IsError(cell.Value) Then
If cell.Value = CVErr(xlErrRef) Then
cell.Interior.Color = RGB(255, 0, 0) ' 赤色
cell.AddComment "リンク切れを検出: " & Now
brokenCount = brokenCount + 1
End If
End If
Next cell
Next ws
MsgBox brokenCount & "個のリンク切れを発見しました"
End Sub
リンクの一括修復
パス変更によるリンク修復
Sub RepairBrokenLinks()
Dim oldPath As String
Dim newPath As String
Dim ws As Worksheet
Dim hl As Hyperlink
Dim updatedCount As Integer
oldPath = InputBox("変更前のパス(部分一致)", "リンク修復", "C:\OldFolder\")
newPath = InputBox("変更後のパス", "リンク修復", "C:\NewFolder\")
updatedCount = 0
For Each ws In ActiveWorkbook.Worksheets
For Each hl In ws.Hyperlinks
If InStr(hl.Address, oldPath) > 0 Then
hl.Address = Replace(hl.Address, oldPath, newPath)
updatedCount = updatedCount + 1
End If
Next hl
Next ws
MsgBox updatedCount & "個のリンクを修復しました"
End Sub
外部リンクの問題解決
自動更新の停止
手動での停止
「データ」→「リンクの編集」
該当リンクを選択
「自動更新しない」を選択
VBAでの一括停止
Sub DisableAutoUpdate()
Dim links As Variant
Dim i As Integer
links = ActiveWorkbook.LinkSources
If Not IsEmpty(links) Then
For i = 1 To UBound(links)
ActiveWorkbook.ChangeLink links(i), links(i), xlLinkTypeExcelLinks, xlUpdateStateManual
Next i
MsgBox "すべての外部リンクの自動更新を停止しました"
End If
End Sub
リンクの値への変換
外部リンクを値に変換
Sub ConvertLinksToValues()
Dim ws As Worksheet
Dim cell As Range
Dim convertedCount As Integer
convertedCount = 0
For Each ws In ActiveWorkbook.Worksheets
For Each cell In ws.UsedRange
If InStr(cell.Formula, "[") > 0 And InStr(cell.Formula, "]") > 0 Then
cell.Value = cell.Value ' 数式を値に変換
convertedCount = convertedCount + 1
End If
Next cell
Next ws
MsgBox convertedCount & "個の外部リンクを値に変換しました"
End Sub
【管理編】リンクの効率的な管理方法
リンク管理台帳の作成
自動生成される管理台帳
Sub CreateLinkInventory()
Dim inventoryWs As Worksheet
Dim ws As Worksheet
Dim hl As Hyperlink
Dim cell As Range
Dim row As Long
' 管理台帳シートの作成
Set inventoryWs = Worksheets.Add
inventoryWs.Name = "リンク管理台帳"
' ヘッダーの設定
With inventoryWs
.Cells(1, 1).Value = "種類"
.Cells(1, 2).Value = "シート名"
.Cells(1, 3).Value = "セル位置"
.Cells(1, 4).Value = "表示文字"
.Cells(1, 5).Value = "リンク先"
.Cells(1, 6).Value = "最終確認日"
.Cells(1, 7).Value = "状態"
.Cells(1, 8).Value = "備考"
' ヘッダーの書式設定
.Range("A1:H1").Font.Bold = True
.Range("A1:H1").Interior.Color = RGB(200, 200, 200)
End With
row = 2
' ハイパーリンクの収集
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "リンク管理台帳" Then
For Each hl In ws.Hyperlinks
With inventoryWs
.Cells(row, 1).Value = "ハイパーリンク"
.Cells(row, 2).Value = ws.Name
.Cells(row, 3).Value = hl.Range.Address
.Cells(row, 4).Value = hl.TextToDisplay
.Cells(row, 5).Value = hl.Address
.Cells(row, 6).Value = Now
.Cells(row, 7).Value = CheckLinkValidity(hl.Address)
End With
row = row + 1
Next hl
End If
Next ws
' 外部リンクの収集
Dim links As Variant
links = ActiveWorkbook.LinkSources
If Not IsEmpty(links) Then
Dim i As Integer
For i = 1 To UBound(links)
With inventoryWs
.Cells(row, 1).Value = "外部リンク"
.Cells(row, 2).Value = "(複数シート)"
.Cells(row, 3).Value = "(複数セル)"
.Cells(row, 4).Value = "数式参照"
.Cells(row, 5).Value = links(i)
.Cells(row, 6).Value = Now
.Cells(row, 7).Value = CheckFileExistence(links(i))
End With
row = row + 1
Next i
End If
' 列幅の自動調整
inventoryWs.Columns.AutoFit
MsgBox "リンク管理台帳を作成しました"
End Sub
Function CheckLinkValidity(linkAddress As String) As String
If InStr(linkAddress, "http") > 0 Then
CheckLinkValidity = "要確認(Web)"
ElseIf InStr(linkAddress, "mailto:") > 0 Then
CheckLinkValidity = "要確認(メール)"
ElseIf Dir(linkAddress) <> "" Then
CheckLinkValidity = "有効"
Else
CheckLinkValidity = "無効"
End If
End Function
Function CheckFileExistence(filePath As String) As String
If Dir(filePath) <> "" Then
CheckFileExistence = "ファイル存在"
Else
CheckFileExistence = "ファイル不存在"
End If
End Function
定期メンテナンスの自動化
リンクチェックの自動実行
Sub ScheduledLinkCheck()
Dim lastCheck As Date
Dim checkInterval As Integer
' 最終チェック日の確認(シートの名前で保存)
On Error Resume Next
lastCheck = CDate(ActiveWorkbook.Names("LastLinkCheck").RefersTo)
On Error GoTo 0
If lastCheck = 0 Then lastCheck = DateAdd("d", -30, Now)
checkInterval = 7 ' 1週間間隔
If DateDiff("d", lastCheck, Now) >= checkInterval Then
Call CreateLinkInventory
' 最終チェック日を更新
ActiveWorkbook.Names.Add "LastLinkCheck", "=" & """" & CStr(Now) & """"
MsgBox "定期リンクチェックを実行しました"
Else
MsgBox "前回のチェックから" & checkInterval & "日経過していません"
End If
End Sub
【セキュリティ】リンクのセキュリティ対策
悪意のあるリンクの検出
危険なリンクのスキャン
Sub ScanSuspiciousLinks()
Dim ws As Worksheet
Dim hl As Hyperlink
Dim suspiciousCount As Integer
Dim suspiciousList As String
suspiciousCount = 0
For Each ws In ActiveWorkbook.Worksheets
For Each hl In ws.Hyperlinks
If IsSuspiciousLink(hl.Address) Then
hl.Range.Interior.Color = RGB(255, 0, 0)
suspiciousList = suspiciousList & ws.Name & "!" & hl.Range.Address & ": " & hl.Address & vbCrLf
suspiciousCount = suspiciousCount + 1
End If
Next hl
Next ws
If suspiciousCount > 0 Then
MsgBox "疑わしいリンクが見つかりました:" & vbCrLf & suspiciousList
Else
MsgBox "危険なリンクは見つかりませんでした"
End If
End Sub
Function IsSuspiciousLink(linkAddress As String) As Boolean
Dim suspiciousPatterns As Variant
Dim i As Integer
' 危険なパターンのリスト
suspiciousPatterns = Array("bit.ly", "tinyurl.com", "javascript:", "data:", "file://", "ftp://anonymous")
For i = 0 To UBound(suspiciousPatterns)
If InStr(LCase(linkAddress), LCase(suspiciousPatterns(i))) > 0 Then
IsSuspiciousLink = True
Exit Function
End If
Next i
IsSuspiciousLink = False
End Function
リンクのアクセス制御
パスワード保護されたリンクの管理
Sub ProtectSensitiveLinks()
Dim ws As Worksheet
Dim hl As Hyperlink
Dim password As String
password = InputBox("リンクアクセス用パスワードを入力してください", "セキュリティ")
If password <> "AdminPassword123" Then ' 実際のパスワードに変更
MsgBox "パスワードが正しくありません"
Exit Sub
End If
For Each ws In ActiveWorkbook.Worksheets
For Each hl In ws.Hyperlinks
If InStr(hl.Address, "confidential") > 0 Or InStr(hl.Address, "secret") > 0 Then
hl.Range.Locked = True
hl.Range.AddComment "要認証リンク: " & Now
End If
Next hl
Next ws
' シートを保護
For Each ws In ActiveWorkbook.Worksheets
ws.Protect password, AllowFormattingCells:=True
Next ws
MsgBox "機密リンクを保護しました"
End Sub
【効率化】リンク作業の自動化
一括リンク作成
CSVからのリンク自動生成
Sub CreateLinksFromCSV()
Dim filePath As String
Dim fileContent As String
Dim lines As Variant
Dim parts As Variant
Dim i As Integer
Dim ws As Worksheet
' CSVファイルの選択
filePath = Application.GetOpenFilename("CSV Files (*.csv), *.csv")
If filePath = "False" Then Exit Sub
' ファイル読み込み
Open filePath For Input As #1
fileContent = Input$(LOF(1), 1)
Close #1
lines = Split(fileContent, vbCrLf)
Set ws = ActiveSheet
' CSVデータからリンク作成
For i = 1 To UBound(lines)
If Trim(lines(i)) <> "" Then
parts = Split(lines(i), ",")
If UBound(parts) >= 1 Then
ws.Hyperlinks.Add Anchor:=ws.Cells(i + 1, 1), _
Address:=parts(1), _
TextToDisplay:=parts(0)
End If
End If
Next i
MsgBox "CSVからリンクを作成しました"
End Sub
バックアップとリストア
リンク設定のバックアップ
Sub BackupLinkSettings()
Dim backupWs As Worksheet
Dim ws As Worksheet
Dim hl As Hyperlink
Dim row As Long
' バックアップシート作成
Set backupWs = Worksheets.Add
backupWs.Name = "LinkBackup_" & Format(Now, "yyyymmdd_hhmmss")
' ヘッダー設定
With backupWs
.Cells(1, 1).Value = "シート名"
.Cells(1, 2).Value = "セル位置"
.Cells(1, 3).Value = "表示テキスト"
.Cells(1, 4).Value = "リンク先"
.Cells(1, 5).Value = "サブアドレス"
.Cells(1, 6).Value = "バックアップ日時"
End With
row = 2
' 全リンクをバックアップ
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> backupWs.Name Then
For Each hl In ws.Hyperlinks
With backupWs
.Cells(row, 1).Value = ws.Name
.Cells(row, 2).Value = hl.Range.Address
.Cells(row, 3).Value = hl.TextToDisplay
.Cells(row, 4).Value = hl.Address
.Cells(row, 5).Value = hl.SubAddress
.Cells(row, 6).Value = Now
End With
row = row + 1
Next hl
End If
Next ws
backupWs.Columns.AutoFit
MsgBox "リンクバックアップを作成しました"
End Sub
コメント