嗨,我正在尝试列出Excel工作簿所在子目录中的所有文件。由于某些原因,该代码无法执行Dir功能。任何人都可以请教吗?谢谢!Sub ListFiles() ActiveSheet.Name = "temp" Dim MyDir As String 'Declare the variables Dim strPath As String Dim strFile As String Dim r As Long MyDir = ActiveWorkbook.Path 'current path where workbook is strPath = MyDir & ":Current:" 'files within "Current" folder subdir, I am using Mac Excel 2011 'Insert the headers in Columns A, B, and C Cells(1, "A").Value = "FileName" Cells(1, "B").Value = "Size" Cells(1, "C").Value = "Date/Time" 'Find the next available row r = Cells(Rows.Count, "A").End(xlUp).Row + 1 'Get the first file from the folder 'Note: macro stops working here strFile = Dir(strPath & "*.csv", vbNormal) 'Loop through each file in the folder Do While Len(strFile) > 0 'List the name, size, and date/time of the current file Cells(r, 1).Value = strFile Cells(r, 2).Value = FileLen(strPath & strFile) Cells(r, 3).Value = FileDateTime(strPath & strFile) 'Determine the next row r = r + 1 'Get the next file from the folder strFile = Dir Loop 'Change the width of the columns to achieve the best fit Columns.AutoFitEnd Sub
3 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
对于上面的答案,当我在MacID中取出“ TEXT”时,它对我有用:
Sub LoopThruFiles()
Dim mydir As String
Dim foldercount As Integer
Dim Subjectnum As String
Dim strpath As String
Dim strfile As String
ChDir "HD:Main Folder:"
mydir = "HD:Main Folder:"
SecondaryFolder = "Folder 01:"
strpath = mydir & SecondaryFolder
strfile = Dir(strpath)
'Loop through each file in the folder
Do While Len(strfile) > 0
If Right(strfile, 3) = "cef" Then
MsgBox (strfile)
End If
strfile = Dir
Loop
End Sub
- 3 回答
- 0 关注
- 839 浏览
相关问题推荐
添加回答
举报
0/150
提交
取消