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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
How/why to you need to use it in the loop?
 

melmac82

New Member
Joined
Jul 31, 2007
Messages
13
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.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
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.:)
 

melmac82

New Member
Joined
Jul 31, 2007
Messages
13
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?
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
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.:)
 

melmac82

New Member
Joined
Jul 31, 2007
Messages
13
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,357
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,181,102
Messages
5,928,063
Members
436,586
Latest member
latintxn

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
Top