使用VBA宏在公式中编辑图书链接

时间:2018-12-06 10:53:46

标签: excel vba

我有一本在单元格中有一些公式的模板书。此公式链接到一些书,例如 ='\ path \ DATA [wsName.xlsm]说明'!$ E $ 13

我想将此链接“ \ path \ DATA [wsName.xlsm]”更改为另一个链接。但这不起作用。

For Each wb In Application.Workbooks 'Getting path from another opened workbook
    If wb.name Like "1.0*" Then
        newAddress = wb.path & "\[" & wb.name & "]" 'GET NEW LINK
    End If
Next

Dim ttt As String
ttt = "\\path\DATA\[wsName.xlsm]"
With ThisWorkbook.Worksheets(1).UsedRange
    .Replace What:=ttt, _
    Replacement:=newAddress, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End With

此代码正常工作:

With ThisWorkbook.Worksheets(1).UsedRange
    .Replace What:=ttt, _
    Replacement:="TEST STRING", _  'Change to text, instead of newAddress
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, ReplaceFormat:=False
End With

我的newAddress怎么了?我认为这样就可以了,因为我想得到的名字的书已经打开了…… 谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

newAddress = "[" & wb.name & "]" 

这对我有帮助。