简体   繁体   English

Excel VBA更新链接

[英]Excel VBA updating links

I am trying to set up a VBA macro to update link paths in excel. 我正在尝试设置VBA宏以更新excel中的链接路径。 I looked up some code online and tried to put it together, and am getting errors. 我在网上查找了一些代码,并试图将它们放在一起,但出现了错误。 I am wondering if i could get some direction here. 我想知道我是否可以在这里找到方向。 Please note that i am not a programmer by profession, just trying to reduce some manual updating work. 请注意,我不是专业的程序员,只是想减少一些手动更新工作。

Cheers! 干杯!

Private Sub CommandButton1_Click() 私人子CommandButton1_Click()

Dim FolderPath As String
Dim FSO As Object
Dim bookname As String
Dim wbook As Workbook
Dim oldname As String
Dim newname As String

oldname = "C:\Users\XX\Documents\[Broadstreet.xlsx]"

newname = "C:\Users\XX\Documents\[Broadstreet2.xlsx]"

FolderPath = "C:\Users\XX\Documents1"


With Application
    .ScreenUpdating = False
    .AskToUpdateLinks = False
End With


For Each Workbook In FSO.GetFolder(FolderPath).Files
    bookname = Workbook.Name

    MsgBox (bookname)

    Set wb = Workbooks.Open(FolderPath & "\" & bookname)

   ActiveWorkbook.ChangeLink oldname1, newname1, xlLinkTypeExcelLinks


   wb.Close SaveChanges:=True

Next

Application.ScreenUpdating = True

End Sub 结束子

Workbooks in Folder Treatment 文件夹处理中的工作簿

  • Loops through all Excel files (workbooks) in a folder, opens each one, changes a link from one document to another, saves the changes and closes the workbook. 循环浏览文件夹中的所有Excel文件(工作簿),打开每个文件,将链接从一个文档更改为另一个文档,保存更改并关闭工作簿。
  • xlLinkTypeExcelLinks is the default parameter of the Type argument of the ChangeLink method and can therefore be omitted. xlLinkTypeExcelLinksChangeLink方法的Type参数的默认参数,因此可以省略。
  • .Close True can be used in this way because SaveChanges is the first argument of the Close method. .Close True可以通过这种方式使用,因为SaveChangesClose方法的第一个参数。

The Code 编码

Private Sub CommandButton1_Click()

  Const strOld As String = "C:\Users\XX\Documents\[Broadstreet.xlsx]"
  Const strNew As String = "C:\Users\XX\Documents\[Broadstreet2.xlsx]"
  Const strPath As String = "C:\Users\XX\Documents1"
  Const strExt As String = "*.xls*"

  Dim strName As String

  With Application
    .ScreenUpdating = False
    .AskToUpdateLinks = False
  End With

  On Error GoTo ProcedureExit

  strName = Dir(strPath & "\" & strExt)

  Do While strName <> ""
    With Workbooks.Open(strPath & "\" & strName)
      .ChangeLink strOld, strNew
      .Close True
    End With
    strName = Dir
  Loop

ProcedureExit:
  With Application
    .AskToUpdateLinks = True
    .ScreenUpdating = True
  End With

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM