在 Excel 中使用 VBA 更改多个链接的源

问题描述 投票:0回答:0

我是 VBA 新手,负责为我的团队创建一个宏,以更新单个工作簿中的多个 (26) 外部链接。我想要一个新链接和旧链接的表格。宏应该与它们匹配,新的应该替换旧的,“更改源”。

我最终得到了这个,但它需要 1 个新链接,并用这个新链接替换所有旧链接。

Sub Update_Links()
    Dim varNewLink As Variant
    Dim lnk As Variant
    
    Application.Goto Reference:="Links_Sheet"
    ' get all links
    lnk = ActiveWorkbook.LinkSources(xlExcelLinks)
    
    If Not IsEmpty(lnk) Then
        ' prompt for the new file for the link
        varNewLink = Application.GetOpenFilename("J:\Capital Production\01 2021\04 Q4 Reporting\03 Assets\08 Counterparty_Concentration\02_Counterparty\01_SH\01 Counterparty Exposure Record\Q321 Counterparty exposure record (*****) v0.2_no links.xlsm")
        ' if user didn't cancel, refresh the link
        If varNewLink <> False Then
            
            ActiveWorkbook.ChangeLink Name:=lnk(1), NewName:=varNewLink, _
                                      Type:=xlExcelLinks
            
        End If
    End If
End Sub
excel vba excel-2007
© www.soinside.com 2019 - 2024. All rights reserved.