Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 循环浏览文件夹中的工作簿_Excel_Vba_Loops_Copy - Fatal编程技术网

Excel 循环浏览文件夹中的工作簿

Excel 循环浏览文件夹中的工作簿,excel,vba,loops,copy,Excel,Vba,Loops,Copy,我正在尝试从文件夹中的所有工作簿复制某些单元格。下面的代码只在第一个文件中循环。新手到VBA。欢迎任何帮助 提前谢谢 Sub Get_Data() Dim Directory As String Dim Filename As String Dim Sheet As Worksheet Dim i As Integer Dim j As Integer Dim wsDest As Workbook Application.ScreenUpdating = False Set wsDest

我正在尝试从文件夹中的所有工作簿复制某些单元格。下面的代码只在第一个文件中循环。新手到VBA。欢迎任何帮助

提前谢谢

Sub Get_Data()

Dim Directory As String
Dim Filename As String
Dim Sheet As Worksheet
Dim i As Integer
Dim j As Integer
Dim wsDest As Workbook

Application.ScreenUpdating = False

Set wsDest = ThisWorkbook
Directory = "C:\Users\dchandarman\Desktop\Current Season\Weekly Production SA\"
Filename = Dir(Directory & "*.xls")

Do While Filename <> ""
MsgBox Filename
Workbooks.Open (Directory & Filename)
Application.ActiveWorkbook.Worksheets("Exec").Range("C21:Y21").Copy
wsDest.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial                         
Paste:=xlPasteValuesAndNumberFormats
Application.ActiveWorkbook.Worksheets("Exec").Range("C23:Y23").Copy
wsDest.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial         
Paste:=xlPasteValuesAndNumberFormats
Application.Workbooks(Filename).Worksheets("Exec").Range("C31:Y32").Copy
wsDest.Worksheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Offset(1).PasteSpecial 
Paste:=xlPasteValuesAndNumberFormats

i = 0

Do Until i = 4
Application.Workbooks(Filename).Worksheets("Exec").Range("D7").Copy
wsDest.Worksheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Offset(1).PasteSpecial             
Paste:=xlPasteValuesAndNumberFormats
i = i + 1
Loop
Application.Workbooks(Filename).Close Savechanges:=False
Loop
End Sub

您可以复制/粘贴非连续范围

子获取_数据2 Const Directory=C:\Users\dchandarman\Desktop\Current Season\Weekly Production SA\ 将文件名设置为字符串 Dim wsDest As工作表,rngDest As范围 将wbSrc作为工作簿,wsSrc作为工作表 设置wsDest=thiswoolk.SheetsSheet1 Filename=dirdirdirectory&*.xls 当文件名 MsgBox文件名 设置wbSrc=Workbooks.OpenDirectory和Filename 设置wsSrc=wbSrc.WorksheetsExec wsSrc.RangeC21:Y21,C23:Y23,C31:Y32.Copy 设置rngDest=wsDest.CellsRows.Count,B.EndxlUp.Offset1 rngDest.Paste特殊粘贴:=XLPasteValues和NumberFormats wsSrc.RangeD7.Copy rngDest.Offset0,-1.Resize4,1.Paste特殊粘贴:=XLPasteValues和NumberFormats wbSrc.Close Filename=Dir 环 MsgBox已完成 端接头 在最后一个循环之前添加Filename=Dir。