Consulting

Results 1 to 6 of 6

Thread: Copy Filenames from Folder

  1. #1
    VBAX Regular
    Joined
    Feb 2009
    Posts
    54
    Location

    Copy Filenames from Folder



    Been trying to work on a macro. Seems quite self explainatory from the pic. Have partially got a part of it to work. Here's what I intend to do:-

    1. Copy all sheet names from folder path mentioned in A1
    2. Open all the Workbooks in the source folder in Hidden mode.

    Thanks in advance

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Greetings PSL,

    Could you tack in the code you have thus far? Use the little green/white VBA button at the top of the message window and paste the code between the tags.

    Mark

  3. #3
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Hello PSL

    You could try this:

    [VBA]
    Sub Test()
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    Dim fso As Object, fld As Object, fil As Object, fldPath As String, i As Long
    fldPath = Range("A1")
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fld = fso.getfolder(fldPath)
    i = 1
    On Error GoTo ErrHandler
    For Each fil In fld.Files
    Cells(i, 2) = fil.Name
    i = i + 1
    Workbooks.Open fil
    ActiveWindow.Visible = False
    Next fil
    ErrHandler:
    Application.EnableEvents = True
    End Sub
    [/VBA]
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  4. #4
    VBAX Regular
    Joined
    Feb 2009
    Posts
    54
    Location
    This is what im using to open the files

    [VBA]Sub Allfiles()

    Dim i As Integer, wb As Workbook
    With Application.FileSearch
    .NewSearch
    .LookIn = "C:\New Folder"
    .SearchSubFolders = False
    .Filename = "*.xls"
    .Execute
    For i = 1 To .FoundFiles.Count
    'Open each workbook
    Set wb = Workbooks.Open(Filename:=.FoundFiles(i))

    Next i
    End With

    End Sub
    [/VBA]

  5. #5
    VBAX Regular JONvdHeyden's Avatar
    Joined
    Mar 2009
    Location
    Hampshire, UK
    Posts
    75
    Location
    Looks like you just need to add:

    ActiveWindow.Visible = False

    ...within your for loop
    Regards,
    Jon von der Heyden (Excel 2003, OS XP Pro)

  6. #6
    VBAX Regular
    Joined
    Feb 2009
    Posts
    54
    Location
    Quote Originally Posted by JONvdHeyden
    Looks like you just need to add:

    ActiveWindow.Visible = False

    ...within your for loop
    Thanks!.. I was able to open the files, but was having trouble getting their names on the sheet!..

    Your code works perfectly.

    Thanks again!

Posting Permissions

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