Consulting

Results 1 to 8 of 8

Thread: Open files that are listed in column A

  1. #1
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location

    Open files that are listed in column A

    Hi everyone


    I have this code below that will combined files into one workbook.

    [VBA]
    Sub CombineFiles()

    Dim Path As String
    Dim FileName As String
    Dim Wkb As Workbook
    Dim WS As Worksheet

    Application.ScreenUpdating = False
    Path = "C:\Test" 'Change as needed
    FileName = Dir(Path & "\*.xls", vbNormal)
    Do Until FileName = ""
    Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)

    For Each WS In Wkb.Worksheets
    If InStr(1, WS.Name, "Incentive") <> 0 Then
    WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    End If
    Next
    Wkb.Close False
    FileName = Dir()
    Loop
    Sheets("Sheet1").Select
    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub
    [/VBA]


    But I would like to open the files that are listed in column A. How can the code below be combined with the code above.

    [VBA]
    Dim R As Range
    For Each R In Range("A1", Range("A65535").End(xlUp))
    Workbooks.Open ("C:\Test" & R.Value)
    Next R
    [/VBA]
    SHAZAM!

  2. #2
    VBAX Regular vonpookie's Avatar
    Joined
    Jun 2004
    Location
    Are we there yet?
    Posts
    74
    Location
    Untested, but I believe this should work:

    [vba]Sub CombineFiles()
    Dim Path As String
    Dim Wkb As Workbook
    Dim WS As Worksheet
    Dim R As Range

    Application.ScreenUpdating = False
    Path = "C:\Test" 'Change as needed

    For Each R In Range("A1", Range("A65535").End(xlUp))
    Set Wkb = Workbooks.Open(FileName:=Path & "\" & R.Value)

    For Each WS In Wkb.Worksheets
    If InStr(1, WS.Name, "Incentive") <> 0 Then
    WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    End If
    Next
    Wkb.Close False
    FileName = Dir()
    Next R

    Sheets("Sheet1").Select
    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub[/vba]

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Thank You for replying vonpookie.


    I'm getting a runtime error '1004' this line of code.

    [VBA]
    Set Wkb = Workbooks.Open(FileName:=Path & "\" & R.Value)
    [/VBA]
    SHAZAM!

  4. #4
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Ok my fault It was the file path I had to change. But it will only open the first workbook not the rest. It gives me this line error.

    [VBA]
    FileName = Dir()
    [/VBA]

    Run-time error '5':
    Invalid Procedure call or argument
    SHAZAM!

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Shazan,
    I'm not clear if you're wanting to combine all the files listed in A. If you just want to open them, your code is OK except a missing "\" and possible extension
    [vba]
    Dim R As Range
    For Each R In Range("A1", Range("A65535").End(xlUp))
    Workbooks.Open ("C:\Test\" & R.Value & ".xls" )
    Next R
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by Shazam
    Ok my fault It was the file path I had to change. But it will only open the first workbook not the rest. It gives me this line error.

    [vba]
    FileName = Dir()
    [/vba]

    Run-time error '5':
    Invalid Procedure call or argument
    I figuered it out I had to changed it to:

    [VBA]
    FileName = r()
    [/VBA]


    Thank You so much vonpookie!
    SHAZAM!

  7. #7
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by mdmackillop
    Hi Shazan,
    I'm not clear if you're wanting to combine all the files listed in A. If you just want to open them, your code is OK except a missing "\" and possible extension
    [vba]
    Dim R As Range
    For Each R In Range("A1", Range("A65535").End(xlUp))
    Workbooks.Open ("C:\Test\" & R.Value & ".xls" )
    Next R
    [/vba]

    Hi mdmackillop thank you for replying. Yes I do want to combined the files. I think your right I'll test your code and post back.
    SHAZAM!

  8. #8
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey Shazam, don't you think you should do a preliminary check if the file exists so you don't get the error, even if the file address is invalid? And if it is invalid maybe you could highlight the cell(s) with the bad filename.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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