从一个文件夹复制到不同的表在一个工作簿复制不同的工作簿

问题描述:

我写了下面的代码清理工作簿,然后创建空表从一个文件夹复制到不同的表在一个工作簿复制不同的工作簿

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 

,然后将其改为从细胞在我的情况的路径B2,查找所有的XLS文件这个文件夹中,并复制到装箱张他们的名字都在range A2

我写了下面

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 
+0

有什么问题?问题是什么? – PeterT

+0

嗨,彼得,问题是在代码中:1)Path = Sheets(“Summary”)。Range(“B2”)。Value --->它不读取B2的值,为什么? => 无法读取文件名= Dir(“路径”和“* .xls”)并且2)将单元格A2中的名称复制到工作簿1请参见代码的最后部分 – maniA

+0

您的意思是将工作簿复制到工作表中?难道它不是恰恰相反吗? – user3598756

要解决第一个问题的尝试:

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

对于第二个你的变量Path是在引号,它不应该是因为它不是一个字符串,而是一个字符串变量。也不太清楚为什么你不得不在那里无论是通配符星号...

Filename = Dir(Path & ".xls") 

对于最后一部分,你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 
要循环
+0

非常感谢和抱歉延迟第一个问题是固定在'与',但我仍然有问题'Dir'文件名仍然是空的!在循环中,我想将工作簿从路径导入summary.xls AS SHEETS!类似于(这里是:从Excel文件导入工作表到摘要) 对于工作簿(文件名)中的每个工作表(文件名).Worksheets total = Workbooks(“summary.xls”)。Worksheets.count Workbooks(fileName).Worksheets sheet.Name).Copy _ after:= Workbooks(“summary.xls”)。Worksheets(total) Next sheet – maniA