2016-07-19 2 views
1

여러 엑셀 파일을 하나로 결합하려고합니다. 이것을 위해 저는 이전의 대답 인 found here을 사용하고 수정했지만 Mac 용 Excel 2016에서 실행하는 동안 문제가 발생했습니다 (Mac 용 Excel 2011에서는 약간의 변경이있었습니다).폴더의 파일을 반복하는 방법 Excel Mac 2016

Excel 2016 (Mac)에서 다음 코드는 루프를 한 번 실행 한 후 선택한 폴더의 첫 번째 파일 이름을 인쇄하지만 중지됩니다.

Excel 2011 (Mac)에서는 선택한 폴더의 모든 파일 이름이 올바르게 인쇄됩니다.

Sub wat() 
Dim FilesFolder As String, strFile As String 

'mac excel 2011 
'FilesFolder = MacScript("(choose folder with prompt ""dis"") as string") 

'mac excel 2016 
FilesFolder = MacScript("return posix path of (choose folder with prompt ""dat"") as string") 

If FilesFolder = "" Then Exit Sub 

strFile = Dir(FilesFolder) 

Do While Len(strFile) > 0 

    Debug.Print "1. " & strFile 

    strFile = Dir 

Loop 

MsgBox "ded" 
End Sub 

그래서, 나는이 꽤 새로운 해요,하지만 제대로 작동하지 strFile = Dir 같은 나에게 보인다.

나는 론 deBruin 페이지를 보았다 : Loop through Files in Folder on a Mac (Dir for Mac Excel) 하지만 솔직히 그런 나를 이해하고 내 요구에 수정을 위해 너무 복잡했다.

도움이 되었으니 양해 해 주셔서 감사합니다.

+0

http://www.rondebruin.nl/mac/mac013.htm – cyboashu

+0

안녕하세요 @ cyboashu, 답변 주셔서 감사합니다. 나는 이미 게시 한 페이지를 보았습니다. (원래 질문에서), 장기적으로는 도움이 될 것 같았지만 지금 당장은 내 수준보다 훨씬 높았습니다. 나는 그것을 내 필요에 맞게 수정할 수 없었습니다. 2016 Excel에서 폴더의 파일을 반복하는 유일한 방법입니까? – Mal

답변

1
Option Explicit 

Sub GetFileNames() 

'Modified from http://www.rondebruin.nl/mac/mac013.htm 


Dim folderPath As String 
Dim FileNameFilter As String 
Dim ScriptToRun As String 
Dim MyFiles As String 
Dim Extensions As String 
Dim Level As String 
Dim MySplit As Variant 
Dim FileInMyFiles As Long 
Dim Fstr As String 
Dim LastSep As String 


'mac excel 2016 

'Get the directory 
On Error Resume Next 'MJN 
folderPath = MacScript("choose folder as string") 'MJN 
If folderPath = "" Then Exit Sub 'MJN 
On Error GoTo 0 'MJN 

'Set up default parameters to get one level of Folders 
'All files 

Level = "1" 
Extensions = ".*" 

'Set up filter for all file types 
FileNameFilter = "'.*/[^~][^/]*\\." & Extensions & "$' " 'No Filter 

'Set up the folder path to allow to work in script 
folderPath = MacScript("tell text 1 thru -2 of " & Chr(34) & folderPath & _ 
          Chr(34) & " to return quoted form of it's POSIX Path") 
folderPath = Replace(folderPath, "'\''", "'\\''") 

'Run the script 
     ScriptToRun = ScriptToRun & "do shell script """ & "find -E " & _ 
         folderPath & " -iregex " & FileNameFilter & "-maxdepth " & _ 
         Level & """ " 

'Set the String MyFiles to the result of the script for processing 
On Error Resume Next 
MyFiles = MacScript(ScriptToRun) 
On Error GoTo 0 

'Clear the fist four columns of the current 1st sheet on the workbook 
Sheets(1).Columns("A:D").Cells.Clear 

'Split MyFiles and loop through all the files 
    MySplit = Split(MyFiles, Chr(13)) 
     For FileInMyFiles = LBound(MySplit) To UBound(MySplit) 
      On Error Resume Next 
      Fstr = MySplit(FileInMyFiles) 
      LastSep = InStrRev(Fstr, Application.PathSeparator, , 1) 
      Sheets(1).Cells(FileInMyFiles + 1, 1).Value = Left(Fstr, LastSep - 1) 'Column A - Directory 
      Sheets(1).Cells(FileInMyFiles + 1, 2).Value = Mid(Fstr, LastSep + 1, Len(Fstr) - LastSep) 'Column B - file name 
      Sheets(1).Cells(FileInMyFiles + 1, 3).Value = FileDateTime(MySplit(FileInMyFiles)) 'Column C - Date 
      Sheets(1).Cells(FileInMyFiles + 1, 4).Value = FileLen(MySplit(FileInMyFiles)) 'Column D - size 
      On Error GoTo 0 
     Next FileInMyFiles 

'Fit the contents 
     Sheets(1).Columns("A:D").AutoFit 

End Sub 
관련 문제