2014-06-09 77 views
0

隨着世界盃在不久的將來,我運行一個池並希望計算統計數據。 我有幾百Excel電子表格的文件夾C:\世界盃在MS Excel中搜索多個工作簿以查看數值

每一個電子表格是相同的 在每一個電子表格,預計獲勝者是在細胞:STEP2 N17

我想有一個電子表格,可通過百個XLS文件中搜索並返回一個值計數

德國34 西班牙20 美國10 意大利1

...等

回答

0

我大部分都找到了我的答案。這將從封閉工作簿中返回該單元格中的所有值

Sub ReadDataFromAllWorkbooksInFolder() 
Dim FolderName As String, wbName As String, r As Long, cValue As Variant 
Dim wbList() As String, wbCount As Integer, i As Integer 
    FolderName = "C:\Users\GARING\Documents\MyWorldCup\2014\2014Entries" 
    ' create list of workbooks in foldername 
    wbCount = 0 
    wbName = Dir(FolderName & "\" & "*.xls") 
    While wbName <> "" 
     wbCount = wbCount + 1 
     ReDim Preserve wbList(1 To wbCount) 
     wbList(wbCount) = wbName 
     wbName = Dir 
    Wend 
    If wbCount = 0 Then Exit Sub 
    ' get values from each workbook 
    r = 0 
    Workbooks.Add 
    For i = 1 To wbCount 
     r = r + 1 
     cValue = GetInfoFromClosedFile(FolderName, wbList(i), "STEP2", "N17") 
     Cells(r, 1).Formula = wbList(i) 
     Cells(r, 2).Formula = cValue 
    Next i 
End Sub 

Private Function GetInfoFromClosedFile(ByVal wbPath As String, _ 
    wbName As String, wsName As String, cellRef As String) As Variant 
Dim arg As String 
    GetInfoFromClosedFile = "" 
    If Right(wbPath, 1) <> "\" Then wbPath = wbPath & "\" 
    If Dir(wbPath & "\" & wbName) = "" Then Exit Function 
    arg = "'" & wbPath & "[" & wbName & "]" & _ 
     wsName & "'!" & Range(cellRef).Address(True, True, xlR1C1) 
    On Error Resume Next 
    GetInfoFromClosedFile = ExecuteExcel4Macro(arg) 
End Function 
相關問題