简体   繁体   English

将文件夹中的不同工作簿复制到一个工作簿中的不同工作表中

[英]Copy different workbooks from an folder into different sheets in one workbook

I wrote the following code to clean the workbook and then to create empty sheets 我编写了以下代码来清理工作簿,然后创建空白表

Sub conclusion()
Dim xWs As Worksheet
Dim Path As String, Filename As String



    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    For Each xWs In Application.ActiveWorkbook.Worksheets
        If xWs.Name <> "Sheet1" And xWs.Name <> "Summary" Then
            xWs.Delete
        End If
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
    ' create new sheets

    Dim MyCell As Range, MyRange As Range

    Set MyRange = Sheets("Summary").Range("A2")
    Set MyRange = Range(MyRange, MyRange.End(xlDown))

    For Each MyCell In MyRange
        Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
        Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
    Next MyCell

    ' copy the workbooks into the sheets (My question )

End Sub

and then it should read the path from a cell in my case B2 and look for all xls files in this folder and copy to the crated sheets whose names are in 然后它应该从我的案例B2中的一个单元中读取路径,并在此文件夹中查找所有xls文件,然后复制到名称位于 范围A2

I wrote the following 我写了以下

 Path= Sheets("Summary").Range("B2").Value ***( it does not read the value of B2, why? )***
  Filename = Dir("Path" & "*.xls")
  Do While Filename <> ""

***here is my question, how can I write the following:
COPY THE WORKBOOK 1 INTO SHEET with the name from Cell A2*** 
  Loop

To fix the first problem try: 要解决第一个问题,请尝试:

With Application.Workbooks("BookName").Sheets("Summary")
    Path = .Range("B1").Text & "\" & .Range("A2").Text
End With

For the second your variable Path is in quotes which it shouldn't be as it is not a string but a string variable. 对于第二个变量, Path应该用引号引起来,因为它不是字符串,而是字符串变量。 Not too sure why you had the wildcard asterisk in there either... 不太确定为什么在其中也有通配符星号...

Filename = Dir(Path & ".xls")

For the last part, your For Loop is missing what it is that you want to cycle through (ie. cells) 对于最后一部分,您的For Loop缺少要循环的内容(即单元格)

For Each MyCell In MyRange.Cells
    Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
    Sheets(Sheets.Count).Name = MyCell.Value ' renames the new worksheet
Next MyCell

暂无
暂无

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

相关问题 如何仅将文件夹中的第一批工作簿复制到一个Excel工作簿中 - How to copy only the first sheets of workbooks in a folder into one excel workbook Excel 将不同工作簿中的数据复制到 1 个工作簿中 - Excel copy data from different workbooks into 1 workbook 将来自不同工作簿的具有相同名称的图纸合并到主工作簿中 - Combine Sheets from different workbooks with the same names into a master workbook 从各种工作簿中复制工作表并粘贴到其他工作簿中的工作表中 - Copying sheets from various workbooks and pasting into a sheet in a different workbook 从一个工作簿复制并粘贴到文件夹中的多个CSV工作簿中 - Copy and Paste from one workbook into multiple CSV workbooks in a folder 用于将数据从一个工作簿复制到特定文件夹中的多个工作簿的宏 - Macro to copy data from one workbook to multiple workbooks in a specific folder VBA将工作表从一个工作簿复制到另一个文件夹中的所有工作簿 - VBA to copy worksheet from one workbook to all workbooks in another folder 用来自不同工作簿的工作表过滤文件夹中的所有(多个)工作簿 - Filter all (multiple) workbooks in a folder with a sheet from different workbook 将列从不同的工作簿移动到不同工作表中的一个工作簿中 - Moving columns from different workbook into one single workbook in different sheets 从不同的工作簿复制数据并将其粘贴到报表工作簿上的特定工作表 - Copy data from different workbook and paste it to specific sheets on a report workbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM