У меня есть каталог на диске, как так:VBA список excel файлов в папке?
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017
В пределах этого режиссера, у меня есть ряд папок на неделю, как так:
KW1
KW2
KW3
ETC.
Далее, внутри каждой недельная папка У меня есть список папок поставщика:
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW1
Среднее содержание каждой папки поставщика выглядит так:
Для справочных целей, я только о файлах первенствовать в каждой из этих папок поставщиков.
Следующая:
У меня есть таблица так:
Обратите внимание на каталог файлов в рабочей книге, указывая на:
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW1
Это должно выглядеть через каждой папке поставщика в этом каталоге и перечислить все файлы excel и адрес электронной почты, содержащийся в ячейке c15, в каждом из этих файлов.
Он не перечисляет адрес электронной почты из каждого файла.
Я получаю эту ошибку:
На этой линии:
values(r, 1) = ExecuteExcel4Macro(arg)
Иногда этот код работает, если я типа в другой каталог и указать это WK 9 или 8. Но папки визуально одинаковы, все они содержат файлы pdf и excel.
Вот мой код:
Sub SO()
If Range("I8").Value = "" Then
Exit Sub
Else
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
'//Set your file path
Dim parentFolder As String
parentFolder = Range("I8").Value '// change as required, keep trailing slash
If Dir(parentFolder) = "" Then
parentFolder = Range("I8").Value & "\" '// change as required, keep trailing slash
Else
parentFolder = Range("I8").Value
End If
'//Fetch Results
Dim results As String
results = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & parentFolder & "*.xls*"" /S /B /A:-D").StdOut.ReadAll
Debug.Print results
'On Error GoTo errHandler2
'// uncomment to dump results into column A of spreadsheet instead:
Range("G17").Resize(UBound(Split(results, vbCrLf)), 1).Value = WorksheetFunction.Transpose(Split(results, vbCrLf))
Range("AD17").Resize(UBound(Split(results, vbCrLf)), 1).Value = "Remove"
Range("V17").Resize(UBound(Split(results, vbCrLf)), 1).Value = " "
'//-----------------------------------------------------------------
'// uncomment to filter certain files from results.
'// Const filterType As String = "*.exe"
'// Dim filterResults As String
'//
'// filterResults = Join(Filter(Split(results, vbCrLf), filterType), vbCrLf)
'//
'// Debug.Print filterResults
'On Error GoTo errHandler
Dim app As New Excel.Application
app.Visible = False 'Visible is False by default, so this isn't necessary
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
'//Email copy code
Dim startCell As Range, fileRng As Range
Dim files As Variant, values() As Variant, values2() As Variant
Dim path As String, file As String, arg As String
Dim r As Long, i As Long
'Acquire the names of your files
With ThisWorkbook.Worksheets(1) 'amend to your sheet name
Set startCell = .Range("G17") 'amend to start cell of file names
Set fileRng = .Range(startCell, .Cells(.Rows.Count, startCell.Column).End(xlUp))
End With
files = fileRng.Value2
'Size your output array
ReDim values(1 To UBound(files, 1), 1 To 1)
'Populate output array with values from workbooks
For r = 1 To UBound(files, 1)
'Create argument to read workbook value
i = InStrRev(files(r, 1), "\")
path = Left(files(r, 1), i)
file = Right(files(r, 1), Len(files(r, 1)) - i)
arg = "'" & path & "[" & file & "]Sheet1'!R15C3"
'Acquire the value
values(r, 1) = ExecuteExcel4Macro(arg)
Next
'Write values to sheet
fileRng.Offset(, 16).Value = values
'// end email copy code
errHandler:
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End If
Exit Sub
errHandler2:
MsgBox "Could not locate folder directory."
Exit Sub
End Sub
Пожалуйста, может кто-то показать мне, где я буду неправильно?
EDIT:
Я просто понял это. Это связано с тем, что в папке или имени файла есть апостроф.
И что-то делать с этой частью коды, где он получает имя файла
'Populate output array with values from workbooks
For r = 1 To UBound(files, 1)
'Create argument to read workbook value
i = InStrRev(files(r, 1), "\")
path = Left(files(r, 1), i)
file = Right(files(r, 1), Len(files(r, 1)) - i)
arg = "'" & path & "[" & file & "]Sheet1'!R15C3"
'Acquire the value
values(r, 1) = ExecuteExcel4Macro(arg)
Есть ли способ исправить это?
EDIT2:
С кодом, предложенным @ A.S.H
Когда я отладки и печати ошибок, это выглядит следующим образом.
Нет ошибки в распечатываются
Редактировать 3
Посмотрев в ближайшем окне, я вижу следующие значения ошибок отладки печати:
EDIT 4:
С обновленной кодовой форме @ A.s.H Это единственные ошибки которые производятся в ближайшем окне:
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Magners GB Ltd\AM Magners KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Magners GB Ltd\Magners KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Magners GB Ltd\CONF AM Magners KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Burts Potato Crisps Ltd\Burts KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Burts Potato Crisps Ltd\CONTACT Burts Potato Crisps Ltd KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\High 5\High 5 KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\High 5\AM2 High 5 KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\PhD Nutrition\PHD KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\PhD Nutrition\AM PHD KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Meastsnacks Group\Meatsnacks KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\USN\USN KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\General Mills UK\General Mills KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Bon Bon Buddies\Bon Bon Buddies KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Bon Bon Buddies\AM Bon Bon Buddies KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Bon Bon Buddies\CONF AM Bon Bon Buddies KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Symington's Ltd\CONF Symingtons KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Symington's Ltd\Symingtons KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Copernus\Copernus KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Dale Farm\Dale Farm KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Tayto Group Ltd\Tayto Group KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Tayto Group Ltd\CONF Tayto Group KW10.17.xlsx
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Magners GB Ltd\AM Magners KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Magners GB Ltd\Magners KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Magners GB Ltd\CONF AM Magners KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Burts Potato Crisps Ltd\Burts KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Burts Potato Crisps Ltd\CONTACT Burts Potato Crisps Ltd KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\High 5\High 5 KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\High 5\AM2 High 5 KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\PhD Nutrition\PHD KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\PhD Nutrition\AM PHD KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Meastsnacks Group\Meatsnacks KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\USN\USN KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\General Mills UK\General Mills KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Bon Bon Buddies\Bon Bon Buddies KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Bon Bon Buddies\AM Bon Bon Buddies KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Bon Bon Buddies\CONF AM Bon Bon Buddies KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Symington's Ltd\CONF Symingtons KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Symington's Ltd\Symingtons KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Copernus\Copernus KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Dale Farm\Dale Farm KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Tayto Group Ltd\Tayto Group KW10.17.xlsx
G:\BUYING\Food Specials\2. Planning\3. Confirmation and Delivery\Announcements\2017\KW10\Tayto Group Ltd\CONF Tayto Group KW10.17.xlsx
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
'[]Sheet1'!R15C3
EDIT 5:
с кодом, представленной @dee я все еще получаю ту же ошибку. Ошибка 1004. Heres', как я использую его код:
Sub SO()
If Range("I8").Value = "" Then
Exit Sub
Else
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
'//Set your file path
Dim parentFolder As String
parentFolder = Range("I8").Value '// change as required, keep trailing slash
If Dir(parentFolder) = "" Then
parentFolder = Range("I8").Value & "\" '// change as required, keep trailing slash
Else
parentFolder = Range("I8").Value
End If
'//Fetch Results
Dim results As String
results = CreateObject("WScript.Shell").Exec("CMD /C DIR """ & parentFolder & "*.xls*"" /S /B /A:-D").StdOut.ReadAll
Dim files, i, r, path, file, arg, macroResult
files = Split(Strings.Trim(results), vbCrLf)
ReDim values(LBound(files) To UBound(files, 1), 0 To 1)
For r = LBound(files) To UBound(files, 1)
If Strings.Trim(files(r)) <> "" Then
i = InStrRev(files(r), "\")
file = Replace(file, "'", "''")
path = Replace(path, "'", "''")
arg = "'" & path & "[" & file & "]Sheet1'!R15C3"
macroResult = ExecuteExcel4Macro(arg)
If Not VBA.IsError(macroResult) Then
values(r, 0) = macroResult
values(r, 1) = file
Else
values(r, 0) = "No email was found"
End If
End If
Next
'Write values to sheet
fileRng.Offset(, 16).Value = values
'// end email copy code
errHandler:
Application.DisplayAlerts = False
Application.EnableEvents = False
Application.ScreenUpdating = False
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End If
Exit Sub
errHandler2:
MsgBox "Could not locate folder directory."
Exit Sub
End Sub
Ошибка в этой строке:
macroResult = ExecuteExcel4Macro(arg)
Вы пытались добавить отладочную ошибку в строку? Затем вы можете захватить значение 'arg' и скопировать его в блокнот/текстовый редактор и посмотреть, есть ли проблемы в формуле. Вы можете вставить это как формулу в Excel и посмотреть, может ли макет формулы Excel указать, где формула неверна. – MiguelH
Вам нужно добавить макрос, который вы вызываете в arg, поэтому посмотрите на это немного, чтобы помочь http://www.ashishmathur.com/tag/xlm-4-0-macro/ = GET.CELL (#, arg), похоже, будет синтаксисом, который вы будете после –
@MiguelH, пожалуйста, см. edit – user7415328