[英]Loop Through Directory VBA to Copy Data With Format
I have few files in a directory or a folder and I want to copy a range (values with format to the current sheet). 我在目录或文件夹中只有几个文件,我想复制一个范围(具有格式的值到当前工作表中)。 I have VBA code and I think it is not in order or something is missing in the code. 我有VBA代码,但我认为它顺序不正确或代码中缺少某些内容。 Please help me to fix the issue. 请帮助我解决问题。
(I have defined named range in each files in the directory. Is it is possible to copy using the named range?) (我已经在目录中的每个文件中定义了命名范围。是否可以使用命名范围进行复制?)
Copy from directory files given path & from sheet2 & paste it to file "workbook.xlsm" Sheet "sheet1" 从给定路径的目录文件复制并从sheet2复制并将其粘贴到文件“ workbook.xlsm”工作表“ sheet1”
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Filepath = "C:\test"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "workbook.xlsm" Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
Sheets("Sheet2").Select
Range("A1:N24").Copy
Workbooks.Open ("Filepath & workbook.xlsm")
If Sheets("Sheet1").Range("A1") = vbNullString Then
Sheets("Sheet1").Range ("A1:N24")
Selection.PasteSpecial Paste:=xlPasteFormats
Selection.PasteSpecial Paste:=xlPasteValues
Else
Selection.Copy Sheets("sheet1").Cells(A1, Columns.Count).End(xlToLeft).Offset(0, 1)
End If
MyFile = Dir
Loop
End Sub
One question remains: 仍然有一个问题:
(I have defined named range in each files in the directory. Is it is possible to copy using the named range?) (我已经在目录中的每个文件中定义了命名范围。是否可以使用命名范围进行复制?)
It's certainly possible. 当然有可能。 Thus assuming the Defined Name
range is "DATA"
. 因此,假设Defined Name
范围为"DATA"
。 Just replace this line: 只需替换此行:
sourceWbk.Sheets("Sheet2").Range("A1:N24").Copy
with this: 有了这个:
sourceWbk.Sheets("Sheet2").Range("DATA").Copy
Actually, OP mentioned that this Names
are generated by another procedure with the address "A1:N24"
. 实际上,OP提到此Names
是由另一个过程使用地址"A1:N24"
。 So in the case that the address is changed then there will be a need to update every other procedure that refers to it, instead by using the Defined Name
don't have to worry about it as it will be taking care by design. 因此,在更改地址的情况下,则需要更新所有其他引用该地址的过程,而不必通过使用“ Defined Name
来担心它,因为它会在设计时加以注意。 That why it's a good practice to use Defined Names
. 这就是为什么使用Defined Names
的好习惯。
I'd use this method: 我会使用这种方法:
Sub LoopThroughDirectory()
Dim MyFile As String
Dim FilePath As String
Dim colFiles As Collection
Dim vFile As Variant
Dim wrkbkSource As Workbook
Dim wrkbkTarget As Workbook
Dim rngTarget As Range
FilePath = "C:\test\"
MyFile = "workbook.xlsm"
Set colFiles = New Collection
EnumerateFiles FilePath, "*.xlsm", colFiles
Set wrkbkTarget = Workbooks.Open(FilePath & MyFile)
For Each vFile In colFiles
If vFile <> FilePath & MyFile Then
Set wrkbkSource = Workbooks.Open(vFile, False)
wrkbkSource.Worksheets(1).Range("A1:N24").Copy
Set rngTarget = wrkbkTarget.Worksheets("Sheet1").Cells(1, wrkbkTarget.Worksheets("Sheet1").Columns.Count).End(xlToLeft)
rngTarget.PasteSpecial xlPasteFormats
rngTarget.PasteSpecial xlPasteValues
wrkbkSource.Close False
End If
Next vFile
End Sub
This procedure is needed to get all the files in the folder: 需要以下步骤来获取文件夹中的所有文件:
Sub EnumerateFiles(ByVal sDirectory As String, _
ByVal sFileSpec As String, _
ByRef cCollection As Collection)
Dim sTemp As String
sTemp = Dir$(sDirectory & sFileSpec)
Do While Len(sTemp) > 0
cCollection.Add sDirectory & sTemp
sTemp = Dir$
Loop
End Sub
Okay see if it works for you, had to add quite a bit 好吧,看看是否适合您,必须添加很多
Sub LoopThroughDirectory()
Dim MyFile As String
Dim erow
Dim Filepath As String
Dim targetWbk As Workbook
Dim sourceWbk As Workbook
Filepath = "C:\test"
MyFile = Dir(Filepath)
Workbooks.Open (Filepath & "\workbook.xlsm")
Set sourceWbk = ActiveWorkbook
Do While Len(MyFile) > 0
If Not MyFile = "workbook.xlsm" And MyFile = "*.xls*" Then
Workbooks.Open (Filepath & MyFile)
Set sourceWbk = ActiveWorkbook
sourceWbk.Sheets("Sheet2").Range("A1:N24").Copy
If targetWbk.Sheets("Sheet1").Range("A1") = vbNullString Then
targetWbk.Sheets("Sheet1").Range("A1:N24").PasteSpecial xlPasteFormulas, xlPasteValues
Else
targetWbk.Sheets("sheet1").Cells(A1, Columns.Count).End(xlToLeft).Offset(0, 1).PasteSpecial xlPasteFormulas, xlPasteValues
End If
MyFile = Dir
End If
Loop
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.