我是 VBA 和一般编程的新手。这是我在这个板上的第一篇文章。我一直在修改我在互联网上找到的代码并且我有代码可以做我想做的事情,但是我想稍微修改它以加快这个过程。
我的代码从我存放在桌面“接收温度”文件夹中的 excel 文件中提取数据,并将数据放在工作簿“接收数据提取器”中。我每月从大约 1000 个文件中获取数据,这些文件存储在以与其关联的 PO 命名的子目录中(名称不同)。现在我必须遍历每个子目录并将 excel 文件移动到“接收温度”,然后宏才能工作。我想修改代码以对文件夹内子目录中包含的所有 excel 文件执行相同的操作,从而允许我将子文件夹复制到“接收临时”文件夹并运行宏,而不是打开每个子目录并抓取excel文件并手动移动它。同样,子目录具有不同的名称。
感谢您提供的任何帮助。
Sub ReadDataFromAllWorkbooksInFolder()
Dim FolderName As String, wbName As String, r As Long
Dim cValue As Variant, bValue As Variant, aValue As Variant
Dim dValue As Variant, eValue As Variant, fValue As Variant
Dim wbList() As String, wbCount As Integer, i As Integer
FolderName = ThisWorkbook.Path & "\Receiving Temp\"
' create list of workbooks in foldername
wbCount = 0
wbName = Dir(FolderName & "\" & "*.xls")
While wbName <> ""
wbCount = wbCount + 1
ReDim Preserve wbList(1 To wbCount)
wbList(wbCount) = wbName
wbName = Dir
Wend
If wbCount = 0 Then Exit Sub
' get values from each workbook
r = 1
For i = 1 To wbCount
r = r + 1
cValue = GetInfoFromClosedFile(FolderName, wbList(i), "Quality Rep.", "c9")
bValue = GetInfoFromClosedFile(FolderName, wbList(i), "Quality Rep.", "o61")
aValue = GetInfoFromClosedFile(FolderName, wbList(i), "Quality Rep.", "ae11")
dValue = GetInfoFromClosedFile(FolderName, wbList(i), "Quality Rep.", "v9")
eValue = GetInfoFromClosedFile(FolderName, wbList(i), "Quality Rep.", "af3")
fValue = GetInfoFromClosedFile(FolderName, wbList(i), "Non Compliance", "a1")
Sheets("Sheet1").Cells(r, 1).Value = cValue
Sheets("Sheet1").Cells(r, 2).Value = bValue
Sheets("Sheet1").Cells(r, 3).Value = aValue
Sheets("Sheet1").Cells(r, 4).Value = dValue
Sheets("Sheet1").Cells(r, 6).Value = eValue
Sheets("Sheet1").Cells(r, 5).Value = fValue
Next i
End Sub
Private Function GetInfoFromClosedFile(ByVal wbPath As String, _
wbName As String, wsName As String, cellRef As String) As Variant
Dim arg As String
GetInfoFromClosedFile = ""
If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\"
If Dir(wbPath & "\" & wbName) = "" Then Exit Function
arg = "'" & wbPath & "[" & wbName & "]" & _
wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1)
On Error Resume Next
GetInfoFromClosedFile = ExecuteExcel4Macro(arg)
End Function