VBA:根据命名范围更改文件连接

时间:2018-04-10 22:40:36

标签: excel vba excel-vba

首先是代码:

Sub Test()

Dim rng As Range
Dim cell As Range

Dim oldfilestring As String
Dim filestring As String

Set rng = Range("Files")

oldfilestring = "File A"

For Each cell In rng
    filestring = cell.Value
    ActiveWorkbook.ChangeLink Name:=oldfilestring, NewName:=filestring, Type:=xlExcelLinks

CopyPasteFilteredData
    oldfilestring = filestring
Next cell

End Sub
  • 我的范围是我导入A列的文件的设置列表:A

我遇到麻烦的地方" oldfilestring ='文件A',有时第一个文件不是"文件A" - 如何编辑它使其成为列表中的第一个文件?

1 个答案:

答案 0 :(得分:0)

如果rng与您的文件列表相对应,那么您可以将列表中的第一个单元格设置为oldfilestring字符串

Sub Test()

Dim rng As Range
Dim cell As Range

Dim oldfilestring As String
Dim filestring As String

Set rng = Range("Files")

oldfilestring = rng.Cells(1, 1).value

For Each cell In rng
    filestring = cell.Value
    ActiveWorkbook.ChangeLink Name:=oldfilestring, NewName:=filestring, Type:=xlExcelLinks

CopyPasteFilteredData
    oldfilestring = filestring
Next cell

End Sub