[英]Loop through excel files in a directory and copy onto master sheet
I have a folder with nearly 1000 .csv files. 我有一个包含近1000个.csv文件的文件夹。 Each of these files contains 2 columns, and I would like to copy only one of these columns and transpose it onto a new workbook. 这些文件中的每一个都包含2列,我只想复制这些列中的一个并将其转置到新的工作簿上。 The new workbook will contain all the data from each of these files. 新工作簿将包含每个文件的所有数据。 The following code is what I have generated: 以下代码是我生成的:
Sub AllFiles()
Application.EnableCancelKey = xlDisabled
Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
folderPath = "J:etc. etc. etc." 'contains folder path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
Filename = Dir(folderPath & "*.csv")
Do While Filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & Filename)
wb.Range(Range("B1"), Range("B1").End(xlDown)).Select
Application.CutCopyMode = False
Selection.Copy
ActiveWorkbook.Close True
Windows("Compiled.xlsm").Activate
Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
Filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
For whatever reason the code does not work and a box pops-up saying "Code execution has been interrupted." 无论出于何种原因,该代码均无效,并弹出一个框,提示“代码执行已被中断”。 Once I hit "Debug" the following line is highlighted: 当我点击“调试”时,以下行将突出显示:
wb.Range(Range("B1"), Range("B1").End(xlDown)).Select
I am not experienced with VBA at all and I am having trouble troubleshooting this issue. 我完全没有使用VBA的经验,并且无法解决此问题。 Any idea on what this means and what I can do? 对这意味着什么以及我能做什么有任何想法吗?
The highlighted line is referring to a range on the workbook that is running the macro as opposed to the range within the workbook you have opened. 高亮显示的行是指运行宏的工作簿上的范围,而不是您打开的工作簿中的范围。 Try replacing with this: 尝试替换为:
wb.Range(wb.Range("B1"), wb.Range("B1").End(xlDown)).Select
However I would suggest you avoid using the Select
function altogether as it tends to slow down code. 但是,我建议您完全避免使用Select
函数,因为它会降低代码的速度。 I've trimmed the loop a bit to avoid using Select
and Activate
: 我对循环进行了一些调整,以避免使用Select
和Activate
:
Do While Filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & Filename)
wb.Range(wb.Cells(1,"B"), wb.Cells(Rows.Count,"B").End(xlUp)).Copy
Workbooks("Compiled.xlsm").Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Transpose:=True
wb.Close True
Filename = Dir
Loop
Once you open file file, the active workbook is the book just opened and the active sheet is also established. 打开文件文件后,活动工作簿即为刚打开的书,并且活动工作表也已建立。
Your code fails primarily because of the wb. 您的代码失败主要是由于wb。 . 。 (In general you would use a sheet reference instead) , but in this case, replace: (通常,您应该使用工作表引用) ,但是在这种情况下,请替换为:
wb.Range(Range("B1"), Range("B1").End(xlDown)).Select
with: 有:
Range("B1").End(xlDown)).Select
(You also do not need Select to accomplish a copy/paste) (您也不需要选择来完成复制/粘贴)
try with below 尝试以下
Sub AllFiles()
Application.EnableCancelKey = xlDisabled
Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
folderPath = "c:\work\test\" 'contains folder path
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
Filename = Dir(folderPath & "*.xlsx")
Do While Filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & Filename)
Range("B1:B" & Range("B" & Rows.count).End(xlUp).Row).Copy
Workbooks("Compiled").Worksheets("Sheet1").Range("A" & Range("A" & Rows.count).End(xlUp).Row + 1).PasteSpecial Transpose:=True
Workbooks(Filename).Close True
Filename = Dir
Loop
Application.ScreenUpdating = True
End Sub
wb.Range(...)
will never work since wb is a Workbook object. wb.Range(...)
将永远无法工作,因为wb是Workbook对象。 You need a Worksheet object. 您需要一个工作表对象。 Try: 尝试:
Dim ws As Worksheet
Set ws = wb.Activesheet
ws.Range(ws.Range("B1"), ws.Range("B1").End(xlDown)).Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.