[英]Copying data from many workbooks to a summary workbook with Excel-VBA. Run time errors
我的文件夹中有文件,我想从这些文件中复制数据并将其粘贴到另一个Master工作簿工作表中。
我一直收到运行时error '1004'
:很抱歉,我们找不到C:\\ Users \\ jjordan \\ Desktop \\ Test Dir \\ MASTER`,它可能已被移动,重命名或删除。
该错误在以下代码行上突出显示: Workbooks.Open SumPath & SumName
我在网上看到了其他与此类似的问题,我尝试进行各种更改。 但是仍然没有成功。 请指教。
C:\\Users\\ jjordan \\Desktop\\Test Dir\\GA Test\\
C:\\Users\\ jjordan \\Desktop\\Test Dir\\MASTER\\
"*.xlsx."
结尾"*.xlsx."
" MASTER – Data List - 2016.xlsm "
'宏文件 "Supplier_Comments"
主工作表名称= "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
这是您想要完成的工作的模板。 注意,正斜杠可能会导致运行时错误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
根据您的需要对此进行更改,您会发现它非常完美:)
编辑:同样在您的代码中,您经常使用COPY&PASTE。 尽量避免以后再这样做。 尝试做一些事情:
ThisWorkbook.Sheets("Sheet1").Range("a1").Value = OtherWork.Sheets("Sheet1").Range("a1").Value
这样效率更高,并且不会使代码陷入困境。
这是一些偏移逻辑
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 =
注意到Offset(x,y)值? 本质上,x向下,y正确。 这当然是参考原始位置。 因此,要让值进入同一行但三列,则可以使用“ Offset(0,3)”等
麻烦您让您更改代码以执行此操作。 :)
我想实际上试图拼凑起来是一场斗争吗? 在此版本中,假定宏位于母版工作簿中(并且您是在母版中运行它的)。 如果您想更改,请继续,但是就我而言。 在某个时候,您必须自己进行试验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.