简体   繁体   English

如何从Excel 2011中的几个工作簿中编译数据

[英]How to compile data from several workbooks in excel 2011

I am trying to copy a line of data from several workbooks in a folder and paste them into a master workbook (in the same folder) using VBA for Excel 2011 Mac. 我正在尝试使用VBA for Excel 2011 Mac从一个文件夹中的几个工作簿复制一行数据并将其粘贴到主工作簿(在同一文件夹中)。 I have the following code but continually run into a Runtime error 1004 . 我有以下代码,但始终遇到运行Runtime error 1004

After debugging it highlights the Workbooks.Open(MyFile) line but I can't figure out why. 调试后,它突出显示了Workbooks.Open(MyFile)行,但我不知道为什么。 The path is correct. 路径正确。 Can anyone please advise on what is wrong? 任何人都可以请教出什么问题吗?

I got the original code from http://www.familycomputerclub.com/transfer-data-from-multiple-workbooks-into-master-workbook-automatically-using-vba.html which is for Windows. 我从适用于Windows的http://www.familycomputerclub.com/transfer-data-from-multiple-workbooks-into-master-workbook-automatically-using-vba.html获得了原始代码。 Any info would be greatly appreciated! 任何信息,将不胜感激!

Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
MyFile = ("/Users/administrator/Desktop/Excel")

Do While Len(MyFile) > 0
    If MyFile = "Test.xlsm" Then
    Exit Sub
    End If

    Workbooks.Open (MyFile)
    Range("A2:D2").Copy
    ActiveWorkbook.Close

    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 4))

    MyFile = Dir

Loop

End Sub

You are using MyFile = ("/Users/administrator/Desktop/Excel") (why the parentheses by the way?), but that is not actually a valid path to a workbook, right? 您正在使用MyFile = ("/Users/administrator/Desktop/Excel") (为什么要加上括号?),但这实际上不是工作簿的有效路径,对吗?

I mean, surely it should point to a .xlsm file and not just to a directory... That should be what gives you the error. 我的意思是,当然,它应该指向.xlsm文件,而不只是指向目录...这应该是导致错误的原因。

I think you need to find all the workbooks in your directory first and then loop through those. 我认为您需要先在目录中找到所有工作簿,然后再遍历这些工作簿。 The way your code is written now seems really wrong to me, but of course that doesn't mean it won't work. 现在,您编写代码的方式在我看来确实是错误的,但这当然并不意味着它将无法正常工作。 But I really think you need to parse the directory for .xlsm files first and then loop through those. 但是我真的认为您需要首先为.xlsm文件解析目录,然后循环遍历这些文件。

UPDATE 更新

I think this code should now do what you want. 我认为这段代码现在应该可以执行您想要的操作。

Sub LoopThroughFiles()

Dim directory As String
Dim file As Variant

directory = "C:\Users\RISE123\Desktop\NSRP\AFPR004\Foundations\20140702 - AFPR004 FA9 Change\"
file = Dir(directory)

Do While Len(file) > 0
    If InStr(1, file, ".xlsm", vbBinaryCompare) > 0 Then
        With Workbooks.Open(directory & file)
            Call .Worksheets(1).Range("A2:D2").Copy(ThisWorkbook.Sheets(1).Range("A2:D2"))
            Call .Close(False)
        End With
    End If
    file = Dir
Loop

End Sub

But you will have to modify the starting directory path directory , as well as the logic in the With End With block to suit your needs. 但是您将必须修改起始目录路径directory以及“ With End With块中的逻辑以适合您的需求。

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

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