使用 VBA 和 excel 从文件夹中的文件收集数据
Collecting data from files in folders with VBA and excel
我是 VBA 和宏的新手,所以我在这里写了一篇 post,希望能为我的解决方案提供一些帮助和提示。我的问题如下:
我需要从文件夹和子文件夹中的 excel-文件中复制不确定数量的包含数据的单元格以粘贴到 excel-"mother"-文件中:
"All files that contain data is in one folder and it's subfolders. the cells to be copied in theese files ALWAYS start at row 40, and are in cells A, B, C and D. How many rows that need to be copied however is uncertain."
我正在寻找的是循环遍历文件夹及其子文件夹的代码,该代码正在寻找要从中获取数据的文件。我也在想,在这个循环中,我稍后会编写代码来从每个单独的文件中收集数据。
所以,我正在寻找的是:
- 循环遍历文件夹和子文件夹以从文件中收集数据的代码。
- 查找包含数据的最后一行并将所有数据从开始复制到最后一行的代码。我在想这样的事情:"A40:D & UncertainRange"
非常感谢所有帮助.. 毕竟我仍然是一个 VBA 菜鸟。
祝周末愉快,祝你所有的问题都可以通过脚本来解决。
美好的一天。
看看这个 link:http://online-vba.de/vba_readfolder.php - 将 sRootPath
更改为您的目录,末尾没有 \
这是一个命令,用于识别 Excel sheet 中包含数据的最后一行:
lastRow = ActiveWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
要遍历从每个文件的第 40 行开始的数据,您可以使用如下内容:
lastRow = ActiveWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For iRow = 40 to lastRow
destinationSheet.Cells(outputRow, 1) = sourceSheet.Cells(iRow, 1)
destinationSheet.Cells(outputRow, 2) = sourceSheet.Cells(iRow, 2)
destinationSheet.Cells(outputRow, 3) = sourceSheet.Cells(iRow, 3)
destinationSheet.Cells(outputRow, 4) = sourceSheet.Cells(iRow, 4)
outputRow = outputRow + 1
Next iRow
要遍历文件,请使用如下内容:
Sub mySub()
Dim strFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "C:\"
.title = "Please select a folder..."
.Show
If .SelectedItems.Count > 0 Then
strFolder = .SelectedItems(1) & "\"
Else
Exit Sub
End If
End With
Dim myobject As Object
Set myobject = CreateObject("Scripting.FileSystemObject")
Set mysource = myobject.GetFolder(strFolder)
Application.Workbooks.Open ("c:\motherWorkbook.xlsx")
For Each MyFile In mysource.Files
''' Do Something with files in main folder
Next
' Subfolders
For Each mySubFolder In mysource.Subfolders
Set mysource = myobject.GetFolder(mySubFolder.Path)
For Each MyFile In mysource.Files
''' Do Something with files in sub folders
Next
Next
End Sub
我是 VBA 和宏的新手,所以我在这里写了一篇 post,希望能为我的解决方案提供一些帮助和提示。我的问题如下:
我需要从文件夹和子文件夹中的 excel-文件中复制不确定数量的包含数据的单元格以粘贴到 excel-"mother"-文件中:
"All files that contain data is in one folder and it's subfolders. the cells to be copied in theese files ALWAYS start at row 40, and are in cells A, B, C and D. How many rows that need to be copied however is uncertain."
我正在寻找的是循环遍历文件夹及其子文件夹的代码,该代码正在寻找要从中获取数据的文件。我也在想,在这个循环中,我稍后会编写代码来从每个单独的文件中收集数据。
所以,我正在寻找的是: - 循环遍历文件夹和子文件夹以从文件中收集数据的代码。 - 查找包含数据的最后一行并将所有数据从开始复制到最后一行的代码。我在想这样的事情:"A40:D & UncertainRange"
非常感谢所有帮助.. 毕竟我仍然是一个 VBA 菜鸟。 祝周末愉快,祝你所有的问题都可以通过脚本来解决。
美好的一天。
看看这个 link:http://online-vba.de/vba_readfolder.php - 将 sRootPath
更改为您的目录,末尾没有 \
这是一个命令,用于识别 Excel sheet 中包含数据的最后一行:
lastRow = ActiveWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
要遍历从每个文件的第 40 行开始的数据,您可以使用如下内容:
lastRow = ActiveWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For iRow = 40 to lastRow
destinationSheet.Cells(outputRow, 1) = sourceSheet.Cells(iRow, 1)
destinationSheet.Cells(outputRow, 2) = sourceSheet.Cells(iRow, 2)
destinationSheet.Cells(outputRow, 3) = sourceSheet.Cells(iRow, 3)
destinationSheet.Cells(outputRow, 4) = sourceSheet.Cells(iRow, 4)
outputRow = outputRow + 1
Next iRow
要遍历文件,请使用如下内容:
Sub mySub()
Dim strFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = "C:\"
.title = "Please select a folder..."
.Show
If .SelectedItems.Count > 0 Then
strFolder = .SelectedItems(1) & "\"
Else
Exit Sub
End If
End With
Dim myobject As Object
Set myobject = CreateObject("Scripting.FileSystemObject")
Set mysource = myobject.GetFolder(strFolder)
Application.Workbooks.Open ("c:\motherWorkbook.xlsx")
For Each MyFile In mysource.Files
''' Do Something with files in main folder
Next
' Subfolders
For Each mySubFolder In mysource.Subfolders
Set mysource = myobject.GetFolder(mySubFolder.Path)
For Each MyFile In mysource.Files
''' Do Something with files in sub folders
Next
Next
End Sub