Sub Test() Dim fso As New FileSystemObject Dim fFile As Object Dim strFileName As String Dim strFile As String Dim intRow As Integer Dim fromPath As String Dim xlBook As New Excel.Workbook Dim xlSheet As New Excel.Worksheet fromPath = "C:\NewHTV" '指定文件夹 If Right(fromPath, 1) <> "\" Then fromPath = fromPath & "\" For Each fFile In fso.GetFolder(fromPath).Files If UCase(Right(fFile.Name, 3)) = "XLS" Then strFileName = fFile.Name Set xlBook = Workbooks.Open(fromPath & fFile.Name) Set xlSheet = xlBook.Sheets(1) intRow = xlSheet.UsedRange.Rows.Count 'strFileInfo = strFileName & "包含" & intRow & "行" strFileInfo = strFileInfo & "'" & strFileName strFileRow = strFileRow & "'" & intRow xlBook.Close Set xlBook = Nothing End If Next 'MsgBox strFile arrFileName = Split(strFileInfo, "'") arrFileRow = Split(strFileRow, "'") Set xlBook = Workbooks.Add Set xlSheet = xlBook.Sheets(1) '选择第一行第一列开始写写文件名 k = 1 For i = 1 To UBound(arrFileName) '从第一行开始写 xlSheet.Cells(k, 1) = arrFileName(i) k = k + 1 Next '选择第一行第二列开始写行数 s = 1 For j = 1 To UBound(arrFileRow) '从第一行开始写 xlSheet.Cells(s, 2) = arrFileRow(j) s = s + 1 Next Application.DisplayAlerts = False xlBook.SaveAs "File info" '默认保存在我的文档里面名为File info xlBook.Close Application.DisplayAlerts = True Set xlBook = Nothing Set fso = Nothing End Sub |