Hello Folks,
I want to create a macro that will allow me to select a folder and then open each .xlsx file and count the number of rows and columns in each spreadsheet. (for now i want to assume there is no tabs) Then i want to display the name of the .xlsx file in column A and display their respected number of rows in column B and number of columns in C.
Right now i can list the names of all excel files in the folder, but my main problem is that the row/column count keeps getting messed up. I don't have any macro coding experience and i have just researching online and picking up code that i think will work. Any help or suggestions i would greatly appreciate!
Sub CountRows()
Dim wb As Workbook, wbXLS As Workbook
Dim LastRow As Long
Dim sPath As String, sFilename As String
Dim LastColumn As Long
Dim sFName As String
Dim intFNumber As Integer
Dim lCounter As Long
Dim lLastRow As Long
Dim sText As String
Dim TxtRng As Range
Dim xRow As Long
Dim bRow As Long
Dim cRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = "C:\Users/abc/123/" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
sFName = ThisWorkbook.Path & "\Excel Data (Write).txt"
DisplayAlerts = False
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
ws.Unprotect
Set TxtRng = ws.Range("B1:B5")
Application.ScreenUpdating = False
sPath = "" 'Path of XLS Files
sFilename = Dir(sPath & "*.xls")
On Error Resume Next
Do While Len(sFilename) > 0
If sFilename <> ThisWorkbook Then
Set wbXLS = Workbooks.Open(sPath & sFilename)
'open file
With ActiveSheet.UsedRange
LastRow = .Rows(.Rows.Count).Row
End With
ws.Range("B" & Rows.Count).End(xlUp).Offset(1) = LastRow
With ActiveSheet.UsedRange
LastColumn = .Columns(.Columns.Count).Column
End With
ws.Range("C" & Rows.Count).End(xlUp).Offset(1) = LastColumn
rg = sFilename
rg.Offset(0, 1) = NbRows
wbXLS.Close False 'close file
End If
sFilename = Dir
Loop
Application.ScreenUpdating = True
DisplayAlerts = True
TxtRng.Value = LastColumn
End If
End With
End Sub
I want to create a macro that will allow me to select a folder and then open each .xlsx file and count the number of rows and columns in each spreadsheet. (for now i want to assume there is no tabs) Then i want to display the name of the .xlsx file in column A and display their respected number of rows in column B and number of columns in C.
Right now i can list the names of all excel files in the folder, but my main problem is that the row/column count keeps getting messed up. I don't have any macro coding experience and i have just researching online and picking up code that i think will work. Any help or suggestions i would greatly appreciate!
Sub CountRows()
Dim wb As Workbook, wbXLS As Workbook
Dim LastRow As Long
Dim sPath As String, sFilename As String
Dim LastColumn As Long
Dim sFName As String
Dim intFNumber As Integer
Dim lCounter As Long
Dim lLastRow As Long
Dim sText As String
Dim TxtRng As Range
Dim xRow As Long
Dim bRow As Long
Dim cRow As Long
Dim xDirect$, xFname$, InitialFoldr$
InitialFoldr$ = "C:\Users/abc/123/" '<<< Startup folder to begin searching from
With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = Application.DefaultFilePath & "\"
.Title = "Please select a folder to list Files from"
.InitialFileName = InitialFoldr$
.Show
If .SelectedItems.Count <> 0 Then
xDirect$ = .SelectedItems(1) & "\"
xFname$ = Dir(xDirect$, 7)
Do While xFname$ <> ""
ActiveCell.Offset(xRow) = xFname$
xRow = xRow + 1
xFname$ = Dir
Loop
sFName = ThisWorkbook.Path & "\Excel Data (Write).txt"
DisplayAlerts = False
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
ws.Unprotect
Set TxtRng = ws.Range("B1:B5")
Application.ScreenUpdating = False
sPath = "" 'Path of XLS Files
sFilename = Dir(sPath & "*.xls")
On Error Resume Next
Do While Len(sFilename) > 0
If sFilename <> ThisWorkbook Then
Set wbXLS = Workbooks.Open(sPath & sFilename)
'open file
With ActiveSheet.UsedRange
LastRow = .Rows(.Rows.Count).Row
End With
ws.Range("B" & Rows.Count).End(xlUp).Offset(1) = LastRow
With ActiveSheet.UsedRange
LastColumn = .Columns(.Columns.Count).Column
End With
ws.Range("C" & Rows.Count).End(xlUp).Offset(1) = LastColumn
rg = sFilename
rg.Offset(0, 1) = NbRows
wbXLS.Close False 'close file
End If
sFilename = Dir
Loop
Application.ScreenUpdating = True
DisplayAlerts = True
TxtRng.Value = LastColumn
End If
End With
End Sub