VBA宏在目录

时间:2018-05-03 17:07:44

标签: excel vba excel-vba

我根本不熟悉VBA,但我很想知道是否有办法计算工作簿中工作表的数量,这些工作表将循环播放文件夹中的所有文件。

例如,A1列出文件名,B1显示工作表数。

A1       B1
book1    5
book2    6

目前已设置此代码并需要进行调整

Sub ListAllFile()

Dim objFSO As Object
Dim objFolder As Object
Dim objFile As Object
Dim ws As Worksheet

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ws = Worksheets.Add

Set objFolder = objFSO.GetFolder("W:\101g-19 (4.20.18) - Copy\")
ws.Cells(1, 1).Value = "The files found in " & objFolder.Name & " are:"


For Each objFile In objFolder.Files
    ws.Cells(ws.UsedRange.Rows.Count + 1, 1).Value = objFile.Name
    'ADD A WORKSHEET AND PASTE "=SHEETS()" in A1 the copy value of a1 in to list
    'close files with out saving

Next

Set objFolder = Nothing
Set objFile = Nothing
Set objFSO = Nothing

End Sub

3 个答案:

答案 0 :(得分:1)

在for循环中,打开文件(假设它们都是excel)并获取工作表的数量。

类似的东西:

For Each objFile In objFolder.Files
    writeCell = ws.Cells(ws.UsedRange.Rows.Count + 1, 1)
    writeCell.Value = objFile.Name
    'ADD A WORKSHEET AND PASTE "=SHEETS()" in A1 the copy value of a1 in to list
    'close files with out saving

    Set wb = Workbooks.Open(objFile.Name)
    writeCell.Offset(,1).value = wb.Worksheets.Count()
    wb.Close(false)

Next

答案 1 :(得分:0)

Sub ListallFiles()
    Dim sFileName As String
    Dim sFolderPath As String: sFolderPath = "C:\Temp\"     ' Change folder path. Ensure that folder path ends with "\"
    Dim oWB As Workbook
    Dim oWS As Worksheet

    ' Get the first excel file name from specified folder
    sFileName = Dir(sFolderPath & "*.xls*")

    ' Add a worksheet
    Set oWS = ThisWorkbook.Worksheets.Add

    With oWS

        ' Set folder name in the new sheet
        .Range("A1").Value = "The file found in " & sFolderPath & " are:"

        ' Loop through all excel files in the specified folder
        Do While Len(Trim(sFileName)) > 0

            ' Open workbook
            Set oWB = Workbooks.Open(sFolderPath & sFileName)

            ' Set workbook details in the file
            .Range("A" & .Cells(.Rows.Count, "A").End(xlUp).Row + 1).Value = sFileName
            .Range("B" & .Cells(.Rows.Count, "B").End(xlUp).Row + 1).Value = oWB.Worksheets.Count

            ' Close workbook
            oWB.Close False

            ' Clear workbook object
            Set oWB = Nothing

            ' Get next excel file
            sFileName = Dir()
        Loop

    End With

End Sub

上面的UDF应该打开指定文件夹中的所有文件,并在新工作表上为每个工作簿提供工作表的数量

答案 2 :(得分:0)

请看下面的内容 - 请注意,您应该从空白工作表中运行

Set CurrentWB = ActiveWorkbook

Dim folderPath As String
Dim Filename As String
Dim wb As Workbook
Dim J As Long
Dim N As Long
Dim lc As Long
Dim lr As Long

'UPDATE FOLDER PATH OF WHERE XLS FILES ARE LOCATED
folderPath = "C:\Users\username\Desktop\test\" 'change to suit

J = 2

'   Column Headers
    CurrentWB.Sheets(1).Range("A1").Value = "Filename"
    CurrentWB.Sheets(1).Range("B1").Value = "# of Sheets"

If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

'YOU CAN CHANGE TO BE ANY FILE TYPE BUT CURRENTLY SET TO .XLSX
Filename = Dir(folderPath & "*.xlsx")
Do While Filename <> ""
  Application.ScreenUpdating = False
    Set TempWB = Workbooks.Open(folderPath & Filename)



'       Counts Per Worksheet
    N = ActiveWorkbook.Worksheets.Count
    CurrentWB.Sheets(1).Range("A" & J).Formula = Filename
    CurrentWB.Sheets(1).Range("B" & J).Formula = N


'       Close Temporary Workbook
    TempWB.Close False

    J = J + 1
    Filename = Dir
Loop