更改所有工作表中的超链接地址(使用Mac 2011)

时间:2016-09-07 05:17:21

标签: excel vba excel-vba

我有一个可用的Excel VBA代码,用于替换每个工作表中的超链接文本。 检查下面的代码

' Only change Hyperlinks in the active sheet
Sub FixHyperlinks()

    Dim wks As Worksheet
    Dim hl As Hyperlink
    Dim sOld As String
    Dim sNew As String

    Set wks = ActiveSheet
    sOld = "%5C"
    sNew = "/"
    For Each hl In wks.Hyperlinks
        hl.Address = Replace(hl.Address, sOld, sNew)
    Next hl

End Sub

问题是工作簿大约有30张!!

如何在活动工作簿的所有工作表中运行此宏?

2 个答案:

答案 0 :(得分:1)

使用a for each循环遍历ThisWorkbook.Worksheets中的所有工作表:

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    'do something
Next ws

所以在你的情况下:

For Each ws In ThisWorkbook.Worksheets

    For Each hl In ws.Hyperlinks

        hl.Address = Replace(hl.Address, sOld, sNew)
    Next hl
Next ws

答案 1 :(得分:1)

您需要添加的唯一内容是循环所有表格的工作簿,请参阅下面的修改代码:

Sub FixHyperlinks()

Dim wb As Workbook
Dim wks As Worksheet
Dim hl As Hyperlink
Dim sOld As String
Dim sNew As String

Set wb = ThisWorkbook

' loop ThisWorkbook all Sheets
For Each wks In wb.Worksheets
    sOld = "%5C"
    sNew = "/"
    For Each hl In wks.Hyperlinks
        hl.Address = Replace(hl.Address, sOld, sNew)
    Next hl
Next wks

End Sub