[英]Copying data from many workbooks to a summary workbook with Excel-VBA. Run time errors
I have files in a folder and I want to copy data from these files and paste them into another Master workbook sheet. 我的文件夹中有文件,我想从这些文件中复制数据并将其粘贴到另一个Master工作簿工作表中。
I keep getting a runtime error '1004'
: Sorry we couldn't find C:\\Users\\jjordan\\Desktop\\Test Dir\\MASTER`, It is possible it was moved, renamed or deleted. 我一直收到运行时
error '1004'
:很抱歉,我们找不到C:\\ Users \\ jjordan \\ Desktop \\ Test Dir \\ MASTER`,它可能已被移动,重命名或删除。
The error is highlighted on this line of code: Workbooks.Open SumPath & SumName
该错误在以下代码行上突出显示:
Workbooks.Open SumPath & SumName
I have seen other questions similar to this on the web, I have tried making various changes. 我在网上看到了其他与此类似的问题,我尝试进行各种更改。 But still without success.
但是仍然没有成功。 Please advise.
请指教。
C:\\Users\\ jjordan \\Desktop\\Test Dir\\GA Test\\
C:\\Users\\ jjordan \\Desktop\\Test Dir\\GA Test\\
C:\\Users\\ jjordan \\Desktop\\Test Dir\\MASTER\\
C:\\Users\\ jjordan \\Desktop\\Test Dir\\MASTER\\
"*.xlsx."
"*.xlsx."
结尾"*.xlsx."
" MASTER – Data List - 2016.xlsm "
'macro file " MASTER – Data List - 2016.xlsm "
'宏文件 "Supplier_Comments"
"Supplier_Comments"
Master worksheet name = "Sheet5"
主工作表名称=
"Sheet5"
Option Explicit Sub GetDataFromMaster() Dim MyPath As String Dim SumPath As String Dim MyName As String Dim SumName As String Dim MyTemplate As String Dim SumTemplate As String Dim myWS As Worksheet Dim sumWS As Worksheet 'Define folders and filenames MyPath = "C:\\Users\\jjordan\\Desktop\\Test Dir\\GA Test\\" SumPath = "C:\\Users\\jjordan\\Desktop\\Test Dir\\MASTER\\" MyTemplate = "*.xlsx" 'Set the template. SumTemplate = "MASTER – Data List - 2016.xlsm" 'Open the template file and get the Worksheet to put the data into SumName = Dir(SumPath & SumTemplate) Workbooks.Open SumPath & SumName Set sumWS = ActiveWorkbook.Worksheets("Sheet5") 'Open each source file, copying the data from each into the template file MyName = Dir(MyPath & MyTemplate) 'Retrieve the first file Do While MyName <> "" 'Open the source file and get the worksheet with the data we want. Workbooks.Open MyPath & MyName Set myWS = ActiveWorkbook.Worksheets("Suppliers_Comment") 'Copy the data from the source and paste at the end of sheet 5 myWS.Range("A2:N100").Copy sumWS.Range("A65536").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'Close the current sourcefile and get the next Workbooks(MyName).Close SaveChanges:=False 'close MyName = Dir 'Get next file Loop 'Now all sourcefiles are copied into the Template file. Close and save it Workbooks(SumName).Close SaveChanges:=True End Sub
Here is a template for what you'd like done. 这是您想要完成的工作的模板。 NOTE that forward slashes can cause run time error b/c vba handles them in an annoying way.
注意,正斜杠可能会导致运行时错误b / c vba以恼人的方式处理它们。
Sub DougsLoop()
Dim wbk As Workbook
Dim Filename As String
Dim path As String
Dim rCell As Range
Dim rRng As Range
Dim wsO As Worksheet
Dim StartTime As Double
Dim SecondsElapsed As Double
Dim sheet As Worksheet
Application.ScreenUpdating = False 'these three statements help performance by disabling the self titled in each, remeber to re-enable at end of code
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
StartTime = Timer 'Starts timer to see how long code takes to execute. I like having this in macors that loop through files
path = "C:\Users\jjordan\Desktop\Test Dir\GA Test" & "\" 'pay attention to this line of code********
Filename = Dir(path & "*.xl??")
Set wsO = ThisWorkbook.Sheets("Sheet5")
Do While Len(Filename) > 0 'this tells the code to stop when there are no more files in the destination folder
DoEvents
Set wbk = Workbooks.Open(path & Filename, True, True)
For Each sheet In ActiveWorkbook.Worksheets
Set rRng = sheet.Range("a2:n100")
For Each rCell In rRng.Cells
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(1, 0).Value = rCell
Next rCell
Next
wbk.Close False
Filename = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
SecondsElapsed = Round(Timer - StartTime, 2)
MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
End Sub
alter to this to your needs and you'll find it works perfectly :) 根据您的需要对此进行更改,您会发现它非常完美:)
EDIT: Also in your code you make use of COPY & PASTE a lot. 编辑:同样在您的代码中,您经常使用COPY&PASTE。 Try avoid doing this in the future.
尽量避免以后再这样做。 Try doing something:
尝试做一些事情:
ThisWorkbook.Sheets("Sheet1").Range("a1").Value = OtherWork.Sheets("Sheet1").Range("a1").Value
This is more efficient and wont bog down your code as much. 这样效率更高,并且不会使代码陷入困境。
here is some offset logic 这是一些偏移逻辑
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(1, 0).Value =
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 1).Value =
wsO.Cells(wsO.Rows.count, 1).End(xlUp).Offset(0, 2).Value =
notice the Offset(x,y) value? 注意到Offset(x,y)值? Essentially x is down and y is right.
本质上,x向下,y正确。 this is of course referencing the original position.
这当然是参考原始位置。 So to get a value to go in the same row but three columns over you would use "Offset(0,3)" etc etc
因此,要让值进入同一行但三列,则可以使用“ Offset(0,3)”等
Ill let you alter your code to do this. 麻烦您让您更改代码以执行此操作。 :)
:)
I guess actually trying to piece it together was a struggle? 我想实际上试图拼凑起来是一场斗争吗? Here this version assumes the macro is in the master workbook(and that youre running it form the master).
在此版本中,假定宏位于母版工作簿中(并且您是在母版中运行它的)。 If you want to change go ahead, but this is as far as I go.
如果您想更改,请继续,但是就我而言。 At some point, you'll have to experiment on your own.
在某个时候,您必须自己进行试验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.