一次更新所有链接

时间:2017-10-06 07:56:05

标签: excel

我有一张包含多张纸的工作簿,几乎所有工作簿都在一个名为“HK 2017”的特定工作表中有一个指向单元格A1的超链接。我想将超链接表的名称从“HK 2017”更改为“HK”。并同时更新所有链接,以便他们可以使用工作表的新名称。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

这是我的解决方案。在当前工作簿的所有工作表中,我只是将公式中的给定字符串替换为另一个字符串

Public Sub edit_links()
    Dim iSheet As Worksheet
    Dim old_text as string
    Dim new_text as string

    old_text = "='\\C\client\XYZ\[old_excel_file.xlsm]Sheet1'"
    new_text = "=Sheet1"

    For Each iSheet In ThisWorkbook.Sheets
        'Debug.Print isheet.Name
        Call update_all_cell_formulas_in_sheet( _
            isheet, _
            old_text, _
            new_text)
    Next iSheet
End Sub

private Sub update_all_cell_formulas_in_sheet(in_sheet As Worksheet, in_search As String, in_replace As String)
    Dim counter As Integer
    Dim iCell As Range

    counter = 0
    For Each iCell In in_sheet.UsedRange
        If InStr(iCell.Formula, in_search) > 0 Then
            counter = counter + 1
            'Debug.Print iCell.Parent.Name, iCell.Address, iCell.Formula
            iCell.Formula = Replace(iCell.Formula, in_search, in_replace)
            'Debug.Assert counter Mod 100 <> 0 ' stop the code every 100 changes
        End If
    Next iCell

    update_all_cell_formulas_in_sheet = counter

End Sub