Pages

Thursday, December 14, 2006

Dir Function in VBA (Visual Basic)

Dir or Dir$ Function can be used in Microsoft Visual Basic (Applications) to get the list of directories or files

sDir = Dir$(sPath & "*.xls", vbNormal)

Do Until LenB(sDir) = 0

Set oWB = Workbooks.Open(sPath & sDir)

‘ Do some stuff

oWB.close

sDir = Dir$

Loop

The above will open all Excel Workbooks under a particular directory.

4 comments:

  1. Anonymous4:20 PM

    thanks. this code works great!

    ReplyDelete
  2. Anonymous4:04 AM

    With subfolders?

    ReplyDelete
  3. Anonymous2:36 PM

    Excellent example. For Word, just use "Document.Open" instead of "Workbooks.Open". Thanks!

    ReplyDelete
  4. What is Dir$ and sPath?

    ReplyDelete