Consulting

Results 1 to 7 of 7

Thread: Combine Files but only copy certain worksheets

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

    Combine Files but only copy certain worksheets

    Hi Everyone


    I got this code below from your kb's entry. But I would like to have a condition in the code like some kind of wild card. Is it possible to just only copy worksheets tabs that has the word "Incentive"? Example

    [vba]
    If Mid(WS.Name, 9) = "Incentive" Then
    WS.Copy
    End If

    [/vba]


    I tried to modifief the code but know luck.



    [vba]Option Explicit

    Sub CombineFiles()

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

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

    If Mid(WS.Name, 9) = "Incentive" Then
    WS.Copy
    End If

    For Each WS In Wkb.Worksheets
    WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    Next WS
    Wkb.Close False
    FileName = Dir()
    Loop
    Application.EnableEvents = True
    Application.ScreenUpdating = True

    End Sub[/vba]
    SHAZAM!

  2. #2
    VBAX Regular
    Joined
    Oct 2006
    Location
    Warsaw, Poland
    Posts
    23
    Location
    Hi,

    It seems that the code for the condition is misplaced - it checks for a worksheet from the previously opened, not current file. Try to modify it this way:
    [vba]
    ...
    Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)

    For Each WS In Wkb.Worksheets
    If Mid(WS.Name, 9) = "Incentive" Then
    WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    End If
    Next WS
    Wkb.Close False
    FileName = Dir()
    ...
    [/vba]
    One Hundred MS Excel Games

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Quote Originally Posted by Bartek
    Hi,

    It seems that the code for the condition is misplaced - it checks for a worksheet from the previously opened, not current file. Try to modify it this way:
    [vba]
    ...
    Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)

    For Each WS In Wkb.Worksheets
    If Mid(WS.Name, 9) = "Incentive" Then
    WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    End If
    Next WS
    Wkb.Close False
    FileName = Dir()
    ...
    [/vba]

    Hi Bartek,



    Thank you for replying. I ran your code and nonthing happend. Maybe it's me. The entire worksheet name is:

    "Crane Incentive 10-16-2006"

    I would like to only copy worksheets that has "Incentive"

    Here are some sample files below.
    SHAZAM!

  4. #4
    VBAX Regular
    Joined
    Oct 2006
    Location
    Warsaw, Poland
    Posts
    23
    Location
    Hi,

    Quote Originally Posted by Shazam
    "Crane Incentive 10-16-2006"
    I would like to only copy worksheets that has "Incentive"
    It that case the Left function will not work - it recognizes only worksheets beginning with "Incentive". You shoud change the contition to:

    [vba]If InStr(1, WS.Name, "Incentive") <> 0 Then
    WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
    End If[/vba]
    One Hundred MS Excel Games

  5. #5
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Its giving me a compile error:

    "Loop without do"

    Here is the code I'm working with.


    [VBA]
    Option Explicit

    Sub CombineFiles()

    Dim Path As String
    Dim FileName As String
    Dim Wkb As Workbook
    Dim WS As Worksheet
    Application.ScreenUpdating = False
    Path = "C:\Production" '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
    Wkb.Close False
    FileName = Dir()
    Loop

    Application.ScreenUpdating = True

    End Sub

    [/VBA]
    SHAZAM!

  6. #6
    VBAX Regular
    Joined
    Oct 2006
    Location
    Warsaw, Poland
    Posts
    23
    Location
    Quote Originally Posted by Shazam
    Its giving me a compile error:
    "Loop without do"
    You are missing Next before Wkb.Close - see your original code.
    One Hundred MS Excel Games

  7. #7
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    Than You so much it works great.

    I have one question what does this line means?


    [VBA]
    If InStr(1,
    [/VBA]
    SHAZAM!

Posting Permissions

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