用VBA保存其他打开文件的链接时,如何保持它们的链接?

时间:2019-01-01 15:52:14

标签: excel vba excel-vba

我正在尝试打开几个文件并在它们之间建立连接,并用不同的名称保存它们,因此链接将更新为新名称。 我的代码打开文件并将其保存为新名称,但不要更改指向新名称的链接。

Sub PullFromFile()

Dim wkb As Workbook, wkbFrom As Workbook, wbkto As Workbook
Dim openfile As String
Dim openPatch As String
Dim savefile As String
Dim savePatch As String
Dim openWin As String
Dim closeWin As String
Dim tbl As ListObject
Dim x As Long
Dim y As Long
Dim z As Long

Application.DisplayAlerts = False

Set wkb = ThisWorkbook
Set tbl = ActiveSheet.ListObjects("RoeeTbl")

For x = 1 To tbl.Range.Rows.Count - 1

'   Get path from cell A1 on Report tab
openPatch = tbl.DataBodyRange(x, 1)
openfile = tbl.DataBodyRange(x, 2)



'   Make sure there is a backslash at the end of the from path
If Right(openPatch, 1) <> "\" Then openPatch = openPatch & "\"

Set wkbFrom = Workbooks.Open(openPatch & openfile)


'set "newwkb" & x = ThisWorkbook

wkb.Activate

Next x


MsgBox ("Files Opened. Press next button to save the files in new names")


End Sub

Sub SaveTheFiles()

Dim wkb As Workbook, wkbFrom As Workbook, wbkto As Workbook
Dim openfile As String
Dim openPatch As String
Dim savefile As String
Dim savePatch As String
Dim openWin As String
Dim closeWin As String
Dim tbl As ListObject
Dim x As Long
Dim y As Long
Dim z As Long

Application.DisplayAlerts = False

Set wkb = ThisWorkbook
Set tbl = ActiveSheet.ListObjects("RoeeTbl")


For y = 1 To tbl.Range.Rows.Count - 1
openWin = tbl.DataBodyRange(y, 2)
Windows(openWin).Activate

'   Get path from cell A1 on Report tab
savePatch = tbl.DataBodyRange(y, 3)
savefile = tbl.DataBodyRange(y, 4)



'   Make sure there is a backslash at the end of the from path
If Right(savePatch, 1) <> "\" Then savePatch = savePatch & "\"

'save as

ActiveWorkbook.SaveAs Filename:=(savePatch & savefile), FileFormat:=xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False

Next y
wkb.Activate


End Sub

我希望将链接更改为新文件名。

0 个答案:

没有答案