Hello
Can someone help me with a Macro that reads and counts the number of .xls files in a directory, list them on an Excel sheet and count the number of records (rows) that each file has?
Please please please please
I really need help.
Ana
Hello
Can someone help me with a Macro that reads and counts the number of .xls files in a directory, list them on an Excel sheet and count the number of records (rows) that each file has?
Please please please please
I really need help.
Ana
Ana,
Welcome to the forum!
Your request is extremely vague, and as such I have several questions:
Are these .xls files only 1 sheet? If any of the files have more than one sheet, how do you want handled?
Are there any blank rows? If so, should blank rows be counted or ignored?
When you say "list them on an Excel sheet" do you want just the file names or the full path to the file including its name?
Is this macro always going to look at the same directory, or should the user be prompted to select a folder?
Hope that helps,
~tigeravatar
Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble
Hello
1. These files have multiple sheets... but the info we need always comes from sheet "NPI Spreadsheet"
2. There should not be any blank rows. Records (rows) that need to be counted start from (including) row 11. If there are any blanks they should be ignored.
3. Path and then only filename.
4. Ideally user should be prompted to select the folder.
Ana
I was able to find a code that helped me a lot. Now I'm looking to add to this also the last date the files were modify, the last date they were accessed , also the # of columns the file has based on row 2 data....and last how to run the macro and have the results listed in the ActiveSheet and do not open a new workbook for this.
Below the code I'm using right now
Option Explicit
Sub SKUs_Counter()
Dim MyPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destRange As Range
Dim rnum As Long, CalcMode As Long
Dim NumOfRecordsInSourceFile As Long
Const FirstDataRowInSourceFile As Long = 11 '#### change this if necessary
'Fill in the path\folder where the files are
' MyPath = "C:\Documents and Settings\xaxc091\My Documents\Work\Work2"
MyPath = "C:\Documents and Settings\xaxc091\My Documents\Work\Work2"
'Add a slash at the end if the user forget it
If Right(MyPath, 1) <> "\" Then
MyPath = MyPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(MyPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No spreadsheets found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
With ActiveSheet
.Cells(1, 1).Value = "Spreadsheet Name"
.Cells(1, 2).Value = "Batch #"
.Cells(1, 3).Value = "SKUs Count"
End With
rnum = 2 '### changed to include Headers
'Loop through all files in the array(myFiles)
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
If MyPath & MyFiles(FNum) <> ThisWorkbook.FullName Then
Set mybook = Workbooks.Open(MyPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
'################################################################## most changes made in this section
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("F7") '######### rephrased the range from "F7:F7"
' count the number of records in the source file from row 11 downwards for all _
'filled rows (until the first blank is encountered)
'It shouldn't be a problem in this case but please "note that a not-yet calculated cell returns Empty..." _
'see Charles Williams post on the following page _
'http://www.eggheadcafe.com/software/aspnet/33557080/ranges-vs-cells-with-vba.aspx
If IsEmpty(.Range("a" & FirstDataRowInSourceFile)) Then
NumOfRecordsInSourceFile = 0
Else
NumOfRecordsInSourceFile = _
.Range(.Range("a" & FirstDataRowInSourceFile), .Range("a" & FirstDataRowInSourceFile).End(xlDown)).Rows.Count
End If
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(FNum)
End With
'Set the destrange
Set destRange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destRange = destRange. _
Resize(.Rows.Count, .Columns.Count)
End With
With destRange
.Value = sourceRange.Value
.Offset(0, 1).Value = NumOfRecordsInSourceFile '###### added by Rob
End With
'#############################################################
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Else
On Error GoTo 0
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'########### added by Rob to free memory
Set mybook = Nothing
Set sourceRange = Nothing
Set destRange = Nothing
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
Beep
MsgBox "Done", , "Macro Complete"
End Sub
Ana,
In the future, please wrap your code in code tags.
As for your question, this code should meet all of your requirements:
![]()
Please Login or Register to view this content.
How to use a macro:
- Make a copy of the workbook the macro will be run on
- Always run new code on a workbook copy, just in case the code doesn't run smoothly
- This is especially true of any code that deletes anything
- In the copied workbook, press ALT+F11 to open the Visual Basic Editor
- Insert | Module
- Copy the provided code and paste into the module
- Close the Visual Basic Editor
- In Excel, press ALT+F8 to bring up the list of available macros to run
- Double-click the desired macro (I named this one tgr)
Thanks, two more things if you could help me with that. Is it possible to get information from a specific cell and throw the results in the same sheet? I will like to get information from cell B1 (Header = Product Manager), Cell B4 (Header = PTIS), Cell F1 (Header = Batch Description), Cell F8 (Header = Batch #).
Also is it possible to count the number of different records in the category column (Column W). There might be 10 records there, but I need to know the count of different ones. ie: 5 records are 2345_DIV1 and other 5 are 2346_DIV1, meaning that the result should be 2. That result can be listed under the header # of Categories.
Please let me know if that makes sense. I'm from Panama and probably I can't explain myself well enough in English
Thanks
Ana
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks