简体   繁体   English

循环浏览目录中的excel文件并复制到主表上

[英]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 : 我对循环进行了一些调整,以避免使用SelectActivate

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.

相关问题 Excel,循环浏览XLSM文件并将行复制到另一张表 - Excel, loop through XLSM files and copy row to another sheet Excel VBA循环遍历用空格隔开的文本部分,然后复制并转置到新工作表上 - Excel VBA Loop through sections of texts separated by blank space then copy and transpose onto a new sheet 循环浏览文件夹中的文件,将内容复制到特定的工作表,并循环浏览主文件中的工作表 - Looping through files in a folder, copy contents to specific sheet and loop through sheets in master file 将文件循环到主表上,但数据不断覆盖自身 - Loop files onto master sheet but data keeps overwriting itself 如何遍历目录并导出Excel文件,工作簿,工作表和工作表(VBA宏)? - How to loop through directory and export content of excel files, workbook by workbook, sheet by sheet (VBA Macros)? 循环浏览包含月度报告的文件夹,应用过滤器并复制到主表 - Loop through folder with monthly reports, apply filter and copy to master sheet 复制文件夹中所有文件的数据并在Excel中制作主表进行分析 - Copy Data of All Files in a Folder and Make a Master Sheet in Excel For Analysis 爬取多个excel个文件,匹配复制数据到master文件 - Crawling through multiple excel files, match and copy data to master file Excel VBA:遍历.msg文件目录 - Excel VBA: Loop Through a Directory of .msg files VBA 循环遍历工作簿和工作表名称并复制到母版中的现有工作表名称 - VBA loop through workbooks and sheet names and copy to existing sheet names in master
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM