+ Reply to Thread
Results 1 to 5 of 5

Code not Working !! Please advise

  1. #1
    Registered User
    Join Date
    01-14-2005
    Location
    UK
    Posts
    20

    Code not Working !! Please advise

    I am trying to open every workbook in a given folder but get error code 1004.

    This is baffling me as i kow there are workbooks in this location.

    Any advice appreciated.

    This is the code i am using (as supplied by Tom many moons ago).

    Sub AllWBooks()
    Dim sName As String
    sName = Dir("C:\Data\*.xls")
    Do While sName <> ""
    Workbooks.Open Filename:=sName
    sName = Dir()
    Loop
    End Sub


    Excel 2000 Pro

  2. #2
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    I put your Sub AllWBooks into a module and watched what happened as I stepped through the routine.

    The problem is that sName is does not contain the full path to the file, only the file name.

    I changed your sub as follows, and it worked:

    Sub AllWBooks()
    Dim sName As String

    ChDir "C:\Data"
    sName = Dir("C:\Data\*.xls")

    Do While sName <> ""
    Workbooks.Open Filename:=sName
    sName = Dir()
    Loop
    End Sub

  3. #3
    Registered User
    Join Date
    01-14-2005
    Location
    UK
    Posts
    20
    Many thanks that fixed my problems.

  4. #4
    Registered User
    Join Date
    01-14-2005
    Location
    UK
    Posts
    20
    Just found another problem !!!


    I have used this code as advised:

    Sub AllWBooks()
    Dim sName As String

    ChDir "C:\Data"
    sName = Dir("C:\Data\*.xls")

    Do While sName <> ""
    Workbooks.Open Filename:=sName
    sName = Dir()
    Loop
    End Sub

    But, I also need to set up a way to be able to select each workbook that has been opened.
    Would an array be the best way or could someone please advise the best way to be able to reference the workbooks by workbook name.

    Many thanks

  5. #5
    Forum Contributor
    Join Date
    11-29-2003
    Posts
    1,203
    See two changes below:

    Sub AllWBooks()
    Dim sName As String, wb as Workbook '<<< add another dim
    sName = Dir("C:\Data\*.xls")
    Do While sName <> ""
    Workbooks.Open Filename:=sName
    set wb = ActiveWorkbook '<<< can now refer to the wb just opened as "wb"
    sName = Dir()
    Loop
    End Sub


    You can refer back to the workbook that has the Sub AllWbooks() at any time ... it is "ThisWorkbook".

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1