通过 VBA 从另一个工作簿复制数据 [英] Copy data from another Workbook through VBA
问题描述
伙计们,这就是我想做的事情,但我在做这件事时遇到了一些麻烦.我有 1 个工作簿,我想从不同的文件中收集数据,做这样的事情.
Guys here's what I want to do and I have a little trouble doing it. I have 1 Workbook where I want to collect data from different files doing something like this.
Do While THAT_DIFFERENT_FILE_SOMEWHERE_ON_MY_HDD.Cells(Rand, 1).Value <> "" And Rand < 65536
then 'I will search if the last row in my main worksheet is in this file...
End Loop
如果是,我将退出 While
循环,如果不是,我将复制所有内容.实际上这不会像我想要的那样工作,但我不会很难找到正确的算法.
If it is I'll quit the While
Loop, if it's not I'll copy everything. Actually this won't work as I want but I won't have trouble finding the right algorithm.
我的问题是我不知道如何访问不同的工作簿.
My problem is that I don't know how to access different workbooks.
推荐答案
您可能会喜欢 GetInfoFromClosedFile()
由于上面的链接似乎不再起作用,我添加 备用链接 1 和 备用链接 2 + 代码:
Since the above link does not seem to work anymore, I am adding alternate link 1 and alternate link 2 + code:
Private Function GetInfoFromClosedFile(ByVal wbPath As String, _
wbName As String, wsName As String, cellRef As String) As Variant
Dim arg As String
GetInfoFromClosedFile = ""
If Right(wbPath, 1) <> "" Then wbPath = wbPath & ""
If Dir(wbPath & "" & wbName) = "" Then Exit Function
arg = "'" & wbPath & "[" & wbName & "]" & _
wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
On Error Resume Next
GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
End Function
这篇关于通过 VBA 从另一个工作簿复制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!