Hi,
I have a number of directories that I have excel workbooks in. Each workbook is only one sheet, all sheets have a similar name all would be "Don't Unhide Motor Report" followed by numbers from one to 15 and shown on the tab as "(1)", "(2)" etc. to "(15)". See attachment MR CLR650-02 PO#13CJ-122-41683.xlsx
Now the code below sort of does what I would like it to, I would like it to name all the Workbooks in each directory in column A and then put Cell D6 in Column B. the results of O1 in Column C, the results of O2 in Column D, the results of O3 in Column E.
Currently it creates a new workbook, then grabs the name of workbook, puts it in Column A then takes D6 from "Sheet1" and puts in Column B, but my sheets are not named sheet1, but when it is it works good.
Another issue I guess I should mention is my directory would be "L:\Team Folders\Operational\Directional\Clear Operations\Motor Reports\Clear\6.5\CLR650-02". this is the last level but there are also Workbooks in "L:\Team Folders\Operational\Directional\Clear Operations\Motor Reports\" but the "Motor Reports" directory has 11 directories and some of them have multiple directories as well.
Sub ReadDataFromAllWorkbooksInFolder()
Dim FolderName As String, wbName As String, r As Long, cValue As Variant
Dim wbList() As String, wbCount As Integer, i As Integer
FolderName = "C:\users\dave\desktop\New Folder\New Folder"
' 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 'original line
wbName = Dir
Wend
If wbCount = 0 Then Exit Sub
' get values from each workbook
r = 0
Workbooks.Add
For i = 1 To wbCount
r = r + 1
cValue = GetInfoFromClosedFile(FolderName, wbList(i), "Sheet1", "d6") 'original line
Cells(r, 1).Formula = wbList(i)
Cells(r, 2).Formula = cValue
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
Here is workbook that the Macro is in accurals.xlsm
Bookmarks