Number of files

melmac82

New Member
Joined
Jul 31, 2007
Messages
13
How can I find out the number of files in a folder when coding in VBA? I need to use the number of files that I have as the bound on a loop in the code, and the number of files in the folder can vary so I can't just count them.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
How/why to you need to use it in the loop?
 
Upvote 0
I am pasting one column from each file into one worksheet, so each time through the loop I need to increment to the next column.
 
Upvote 0
So you don't actually need to know the no of files then?

You just need to loop through all the files in the folder.:)
 
Upvote 0
But how do I know what column to put them in then. I am using Offset(1, col) where col is the number of files that I have. Is there a way to put the info into consecutive columns without needing to tell it the column number?
 
Upvote 0
Just set col to 1 and increment it as you loop through the files.
Code:
col=col+1
PS It would be helpful if you posted your code.:)
 
Upvote 0
Here's my code that I have currently, where I have set the number of files to 20 for the loop just as an example.

Sub getfiles()

Dim col As Integer

strpath = "H:\..."
Strfile = Dir(strpath & "*.csv")

Do While Strfile <> ""
For col = 0 To 20
Workbooks.Open (strpath & Strfile)
Columns("b:b").Copy
ActiveWindow.Close

Workbooks("test3.xls").Activate
Sheets("Sheet1").Activate
Range("A1").Offset(1, col).Select
ActiveSheet.Paste

Strfile = Dir()
Next col
Loop

End Sub
 
Upvote 0
Why do you need the For loop?
Code:
Sub getfiles()
Dim wbThis As Workbook
Dim wbOpen As Workbook
Dim col As Integer
    
    strpath = "H:\..."
    Strfile = Dir(strpath & "*.csv")
    col = 0
    Set wbThis = ThisWorkbook
    
    Do While Strfile <> ""
        
        Set wbOpen = Workbooks.Open(strpath & Strfile)
        
        wbOpen.ActiveSheet.Range("B:B").Copy wbThis.ActiveSheet.Range("A1").Offset(, col)
        
        col = col + 1
        
        wbOpen.Close
    
        Strfile = Dir()
    
    Loop
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,227
Messages
6,164,723
Members
451,912
Latest member
HMF009

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