简体   繁体   English

在多个工作簿中定位一个特定的工作簿

[英]Target one specific workbook among multiple workbooks

So I have this code to update links within my excel workbook 所以我有这段代码来更新我的excel工作簿中的链接

Dim alertTime As Date

Public Sub Refresh()
'refresh
ActiveWorkbook.UpdateLink (ActiveWorkbook.LinkSources)
'Workbooks("Requests").UpdateLink (Workbooks("Requests").LinkSources)
alertTime = Now + TimeValue("00:00:05") 'hh:mm:ss
    Application.OnTime alertTime, "Refresh"
Debug.Print Now() & " - Links Updated"

'MsgBox "5 Seconds have passed, refreshing", vbInformation, "Debug"

End Sub
Sub StopRefresh()
    Application.OnTime alertTime, "Refresh", , False
End Sub

The code works, but as soon as I open up another workbook and the refresh triggers and it errors out. 该代码有效,但是一旦我打开另一个工作簿和刷新触发器,它就会出错。 I want to be able to target that specific workbook for the refresh command. 我希望能够针对该特定工作簿进行刷新命令。

Any help is welcomed. 欢迎任何帮助。

您可能需要将工作表也这样放置:

workbooks("Requests").Sheets("Sheetname").UpdateLink(Workbooks("Requests").Sheets("Sheetname").LinkSources)

Your statement ActiveWorkbook.UpdateLink (ActiveWorkbook.LinkSources) updates the link in the "Active" Workbook. 您的语句ActiveWorkbook.UpdateLink (ActiveWorkbook.LinkSources)更新了“活动”工作簿中的链接。 That's the workbook that you are currently working on. 那是您当前正在使用的工作簿。 When you open a new workbook, the new workbook becomes the active workbook. 当您打开一个新工作簿时,新工作簿将成为活动工作簿。

Instead of ActiveWorkbook.UpdateLink (ActiveWorkbook.LinkSources) try this: 代替ActiveWorkbook.UpdateLink (ActiveWorkbook.LinkSources)尝试以下操作:

Workbooks("nameOfWorkbook").UpdateLink (Workbooks("nameOfWorkbook").LinkSources)

Where nameOfWorkbook is the filename of the workbook (ie, filename, extension and path if necessary) for which you want to update links. 其中nameOfWorkbook是您要为其更新链接的工作簿的文件名(即文件名,扩展名和路径,如果需要) For example, 例如,

Workbooks("Requests.xlsb").UpdateLink (Workbooks("Requests.xlsb").LinkSources)

If the active directory is different than the directory in which Requests.xlsb is saved, then you must include the full path, like this: 如果活动目录与保存Requests.xlsb的目录不同,则必须包括完整路径,如下所示:

Workbooks("C:\Users\yourID\Documents\Requests.xlsb").UpdateLink _
   (Workbooks("C:\Users\yourID\Documents\Requests.xlsb").LinkSources)

or more concisely: 或更简而言之:

With Workbooks("C:\Users\yourID\Documents\Requests.xlsb")
    .UpdateLink .LinkSources
End with

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

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