刷新Excel连接 [英] Refresh Excel connections
问题描述
我试图循环遍历我的Excel工作簿中的每个连接,并单独刷新每个连接,并在每次刷新之间捕获任何错误消息。但是,当尝试运行代码时,我收到类型不匹配错误:
I'm trying to loop through each connection in my Excel workbook and refresh each one individually and capture any error messages in between each refresh. However, I receive a 'Type-Mismatch' error when trying to run the code:
Private Sub btnRefreshConns_Click()
Dim cn As WorkbookConnection
Set cn = ActiveWorkbook.Connections.Count
For Each cn In Workbook.Connections
cn.Refresh
Next
End Sub
有人可以帮我吗? >
Could someone please assist me?
推荐答案
Private Sub btnRefreshConns_Click()
Dim cn As WorkbookConnection
'Remove this line --> Set cn = ActiveWorkbook.Connections.Count
For Each cn In ActiveWorkbook.Connections
cn.Refresh
Next
End Sub
应该这样做。使用 For Each
,您不需要跟踪计数。
should do it. With For Each
, you don't need to keep track of the count.
(注意:撇号'
在VB中引用评论,所以你可以尝试评论违规行,看看会发生什么。)
(Note: apostrophes '
introduce comments in VB, so you can try commenting out offending lines to see what happens.)
编辑:循环需要参考 ActiveWorkbook
。 工作簿
是一种类型,即什么样的东西 ActiveWorkbook
是。 ActiveWorkbook
是一个对象,您可以操作的实际操作。
edit: The loop needs to refer to ActiveWorkbook
. Workbook
is a type, i.e., what kind of thing ActiveWorkbook
is. ActiveWorkbook
is an object, an actual thing you can manipulate.
这篇关于刷新Excel连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!