[英]Copy single sheet from multiple workbooks and paste in a single workbook
I am trying to use VBA in order to automate a boring work process.我正在尝试使用 VBA 来自动化无聊的工作流程。 I don't know the language so I copied a code from the internet which is as follows:
我不懂语言,所以我从互联网上复制了一个代码,如下所示:
Problem statement: I have multiple excel files in one folder, from them I have to extract a single sheet named "sheet 1" (all the files have it, but sheet1 isn't the only sheet those workbooks have) .
问题陈述:我在一个文件夹中有多个 excel 文件,我必须从中提取一张名为“sheet 1”的工作表(所有文件都有它,但 sheet1 不是这些工作簿唯一的工作表) 。
Then I have to paste them in a new workbook.
然后我必须将它们粘贴到一个新的工作簿中。 (I don't mind if each of them are in different sheet, because I'll just record a macro to compile them later on)**
(我不介意它们是否在不同的工作表中,因为我只会记录一个宏以便稍后编译它们)**
Does anyone have any suggestions?有没有人有什么建议?
Sub Combine_files()
Dim Path As String
Dim Filename As String
Dim Sheet As Worksheet
Path = "C:\Users\prayag.purohit\OneDrive\Desktop\Project KC\New folder\"
Filename = Dir(Path & "*.xlsx")
Do While Filename <> ""
Workbooks.Open Filename:=Path & Filename, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(Filename).Close
Filename = Dir()
Loop
End Sub
Below code takes the first sheet in every file and names the sheet to the filename.下面的代码获取每个文件中的第一个工作表,并将工作表命名为文件名。
Option Explicit
Sub Combine_files()
Dim Path As String, Filename As String
Dim wbFile As Workbook, wbActive As Workbook
Set wbActive = ActiveWorkbook
Path = "C:\Users\prayag.purohit\OneDrive\Desktop\Project KC\New folder\"
Filename = Dir(Path & "*.xlsx")
With Application
.ScreenUpdating = False
End With
Do While Filename <> ""
Set wbFile = Workbooks.Open(Path & Filename, False, True)
wbFile.Sheets(1).Copy After:=wbActive.Sheets(1)
wbActive.Sheets(2).Name = Filename
wbFile.Close SaveChanges:=False
Filename = Dir()
Loop
wbActive.Sheets(1).Select
With Application
.ScreenUpdating = True
End With
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.