Consulting

Results 1 to 2 of 2

Thread: Name and row count for every .xls file in a directory

  1. #1
    VBAX Newbie
    Joined
    Oct 2011
    Posts
    1
    Location

    Name and row count for every .xls file in a directory

    I have a set of excel files in a directory, is it possible to print every file name and the number of rows in each file through a vba script? if yes, could some one please help me.

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    wellcome to vbax

    by number of rows i understand the number of rows in active sheet, ie the worksheet the the workbook last saved.

    you may try this.

    [vba]
    Sub OpFileCntRow()

    Dim wb As Workbook, ws As Worksheet
    Dim rng As Range
    Dim fFile As String, fPath As String
    Dim lrow As Long, cntRows As Long

    fPath = "C:\Users\MyName\Docs\"
    fFile = Dir(fPath)

    Set wb = ThisWorkbook 'change to suit
    Set ws = wb.Worksheets("Sheet1") 'change to suit

    With ws
    .Range("A1") = "File Name"
    .Range("B1") = "Number of Rows"

    lrow = 1
    Do Until fFile = ""
    lrow = lrow + 1
    Cells(lrow, 1).Value = fFile
    fFile = Dir
    Loop

    Set rng = .Range("A2")
    While rng.Value <> ""
    Workbooks.Open fPath & rng.Value
    cntRows = ActiveSheet.UsedRange.Rows.Count
    ActiveWorkbook.Close False
    rng.Offset(0, 1) = cntRows
    Set rng = rng.Offset(1, 0)
    Wend
    End With

    End Sub
    [/vba]
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •