我是VBA和編程的新手。這是我在這個板子上的第一篇文章。我一直在修改這個代碼,修改我在互聯網上找到的代碼,並且我有代碼來做我想做的事情,但是我想稍微修改它以加快此過程。從子目錄中獲取excel文件中的數據
我的代碼從我的文件夾中存放我的桌面「接收溫度」,並在一個工作簿「接收數據提取」放在數據上Excel文件中提取數據。我每月從大約1000個文件中獲取數據,這些數據存儲在爲P.O命名的子目錄中。他們與(不同的名字)相關聯。現在,我必須通過這些子目錄中的每一個,並在宏工作之前將excel文件移動到「Receiving Temp」。我想修改代碼,對文件夾中的子目錄中包含的所有excel文件執行相同的操作,以便將子文件夾複製到「receive temp」文件夾中,然後運行宏而不是打開每個子目錄並抓取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
這應該讓你在正確的方向:) http://www.vbaexpress.com/kb/getarticle.php?kb_id=245 –
這看起來會有所幫助。非常感謝你。 – NewSpeaker
我剛剛測試了代碼,它返回了我想從中獲取數據的文件列表。然而,我不知道如何將代碼應用到我現有的代碼中,讓它在該文件列表上執行「getinfofromclosedworkbooks」過程。我感謝您的幫助。您能否就此提供建議?謝謝 – NewSpeaker