list 15 rows but it should be 654 rows

newapa

Board Regular
Joined
Sep 13, 2012
Messages
69
Hi!

I was trying to list file from folder bit i only get 15 rows.
It should be about 650 rows?

Code:
Sub CheckFile()
    Dim sFolder As String
    
    sFolder = "C:\Folder\"
    Call CheckPath(sFolder)
End Sub
Sub CheckPath(sFolder As String)
    Dim sPath As String
    Dim sFile() As String
    Dim TotFile As Integer
    Dim i As Integer
    Dim x As Integer
    
    sPath = Dir$(sFolder & "*.*", vbDirectory)
    TotFile = 0
    x = 0


    While sPath <> ""
        If sPath <> "." And sPath <> ".." Then
            If (GetAttr(sFolder & sPath) And vbDirectory) = vbDirectory Then
                TotFile = TotFile + 1
                ReDim Preserve sFile(TotFile)
                sFile(TotFile) = sFolder & sPath & "\"
            Else
                x = x + 1
                ThisWorkbook.Worksheets("Sheet1").Cells(x, 6).Value = sPath
            End If
        End If
        sPath = Dir$()
    Wend
    
    If TotFile <> 0 Then
        For i = 1 To TotFile
            Call CheckPath(sFile(i))
        Next i
    End If
End Sub


thx in davance
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
One problem is going to be this line:
ThisWorkbook.Worksheets("Sheet19").Cells(x, 6).Value = sPath

Because this is a recursive process (a procedure calling itself), the computer retains different versions of x, one for each run of the procedure, and in that procedure you have x being set to 0. This means that you will be overwriting your results.
Try changing it to the likes of:
ThisWorkbook.Worksheets("Sheet19").Cells(Rows.Count, 6).End(xlUp).Offset(1).Value = sPath
which writes the next result below the first empty cell below the last entry in column F.
 
Upvote 0
What p45cal said. Try something like this...

Code:
[COLOR=darkblue]Sub[/COLOR] CheckPath(sFolder [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR], [COLOR=#ff0000]Optional x As Long = 0[/COLOR])
    [COLOR=darkblue]Dim[/COLOR] sPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] sFile() [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] TotFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    [COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR]
    
    sPath = Dir$(sFolder & "*.*", vbDirectory)
    TotFile = 0
    [COLOR=darkblue]While[/COLOR] sPath <> ""
        [COLOR=darkblue]If[/COLOR] sPath <> "." And sPath <> ".." [COLOR=darkblue]Then[/COLOR]
            [COLOR=darkblue]If[/COLOR] (GetAttr(sFolder & sPath) And vbDirectory) = vbDirectory [COLOR=darkblue]Then[/COLOR]
                TotFile = TotFile + 1
                [COLOR=darkblue]ReDim[/COLOR] [COLOR=darkblue]Preserve[/COLOR] sFile(TotFile)
                sFile([COLOR=darkblue]To[/COLOR]tFile) = sFolder & sPath & "\"
            [COLOR=darkblue]Else[/COLOR]
                x = x + 1
                ThisWorkbook.Worksheets("Sheet1").Cells(x, 6).Value = sPath
            [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
        sPath = Dir$()
    [COLOR=darkblue]Wend[/COLOR]
    
    [COLOR=darkblue]If[/COLOR] TotFile <> 0 [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]For[/COLOR] i = 1 To TotFile
            [COLOR=darkblue]Call[/COLOR] CheckPath(sFile(i)[COLOR=#ff0000], x[/COLOR])
        [COLOR=darkblue]Next[/COLOR] i
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
End [COLOR=darkblue]Sub[/COLOR]
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,279
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top