2

我需要找出文件夹下每个 excel 表中的行数。谷歌搜索显示下面的脚本有效..但是对 vb 的了解很少,我无法解决它。脚本包含''Wscript object.I认为这也适用于这个对象

事情在“c:\ temp”下,我有100个excel表(.xls)。需要找出每个文件中的行数。需要 vb 专家的帮助

 Dim objFSO, strFolder, objFolder, objFile, objExcel, objSheet, objRange, objRows As Object

        Dim strExtension As String
        Dim V_FilePath As String = " "

        ' Specify folder.
        strFolder = "c:\\temp"    -----

        objExcel = CreateObject("Excel.Application")

        ' Enumerate files in the folder.
        objFSO = CreateObject("Scripting.FileSystemObject")
        objFolder = objFSO.GetFolder(strFolder)

        For Each objFile In objFolder.Files
            ' Select only Excel spreadsheet file.
            strExtension = objFSO.GetExtensionName(objFile.Path)

            If (strExtension = "xls") Or (strExtension = "xlsx") Then
                ' Open each spreadsheet and count the number of rows.
                objExcel.Workbooks.Open(objFile.Path)
                objSheet = objExcel.ActiveWorkbook.Worksheets(1)
                objRange = objSheet.UsedRange
                objRows = objRange.Rows

      ' Display spreadsheet name and the number of rows.

                MsgBox(objExcel.ActiveWorkbook + CStr(objRows.Count))
                ''Wscript.Echo(objFile.Path & " (" & objRows.Count & ")")

 ' Close the spreadsheet.

                objExcel.ActiveWorkbook.Close()
            End If

        Next

        ' Clean up.
        objExcel.Application.Quit()

        Dts.TaskResult = ScriptResults.Success
    End Sub
4

3 回答 3

2

确保您在顶部使用“Sub _ ()”声明子例程。此外,我认为有几件事在语法上是不正确的。试试这个:

Sub blah()
Dim objFSO, strFolder, objFolder, objFile, objExcel, objSheet, objRange, objRows As Object

        Dim strExtension As String
        Dim V_FilePath As String

        V_FilePath = " "
        ' Specify folder.
        strFolder = "c:\\temp"

        objExcel = CreateObject("Excel.Application")

        ' Enumerate files in the folder.
        objFSO = CreateObject("Scripting.FileSystemObject")
        objFolder = objFSO.GetFolder(strFolder)

        For Each objFile In objFolder.Files
            ' Select only Excel spreadsheet file.
            strExtension = objFSO.GetExtensionName(objFile.Path)

            If (strExtension = "xls") Or (strExtension = "xlsx") Then
                ' Open each spreadsheet and count the number of rows.
                objExcel.Workbooks.Open (objFile.Path)
                objSheet = objExcel.ActiveWorkbook.Worksheets(1)
                objRange = objSheet.UsedRange
                objRows = objRange.Rows

      ' Display spreadsheet name and the number of rows.

                MsgBox (objExcel.ActiveWorkbook + CStr(objRows.Count))
                ''Wscript.Echo(objFile.Path & " (" & objRows.Count & ")")

 ' Close the spreadsheet.


            objExcel.ActiveWorkbook.Close

            End If

        Next

        ' Clean up.

        objExcel.Application.Quit

        Dts.TaskResult = ScriptResults.Success
    End Sub
于 2013-08-05T13:24:39.003 回答
1

如果您在 Excel 宏中的 VBA 中执行此操作,也许这会更好一些:

Sub LoopThroughFiles()
    Dim strFile As String
    Dim strPath As String
    Dim colFiles As New Collection
    Dim i As Integer
    Dim rowCount As Integer

    strPath = "C:\Users\[windows_username]\Documents\" 'Your path here
    strFile = Dir(strPath)

    While strFile <> ""
        colFiles.Add strFile
        strFile = Dir
    Wend

    'List filenames in Column A of the active sheet
    If colFiles.Count > 0 Then
        For i = 1 To colFiles.Count
            ActiveSheet.Cells(i, 1).Value = colFiles(i)

            Workbooks.Open strPath & colFiles(i)
            rowCount = ActiveSheet.UsedRange.Rows.Count
            Workbooks(colFiles(i)).Close
            'Workbooks.Close
            'ThisWorkbook.Close
            ActiveSheet.Cells(i, 2).Value = rowCount


        Next i
    End If

End Sub
于 2013-08-05T15:27:31.627 回答
0

这将工作 MsgBox(objFile.name + CStr(objRows.Count))

于 2013-08-05T14:23:59.587 回答