2013-08-05 22 views
1

我需要找出每個excel表單下一個文件夾中的行數.Google搜索顯示下面的腳本工作正常..但對vb,我無法解決它。腳本containe'Wscript object.I認爲這也適用於此對象以及腳本查找特定文件夾下excel下的行數使用vb

事情是在「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 
+2

你試過嗎?你遇到了什麼錯誤 ? – rags

+0

這是工作。在這個意義上,我正在獲得的數額。但我需要每個文件的名稱和數量,所以我怎麼能得到這些細節 – user1254579

回答

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 
2

確保您聲明頂部的子過程與「子_()」。另外,我認爲有一些東西在語法上不正確。試試這個:

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 
+0

我得到了一個errorError:System.Reflection.TargetInvocationException:異常已被調用的目標引發。 ---> System.InvalidCastException:運算符'+'未爲類型'Workbook'和字符串「36」定義。 at Microsoft.VisualBasic。 – user1254579

+0

如果我鍵入MsgBox(CStr(objRows.Count),那麼它會工作,但我需要知道文件名和相應的計數 – user1254579

+0

這真的不像VBA。我不認爲你可以使用「+」運算符像在VBA中那樣連接。 – xboxremote

0

這將工作 MSGBOX(objFile.name + CStr的(objRows.Count))

相關問題