Option Explicit
Sub ConsolidateWBsToSheets2()
'Author: Jerry Beaucaire'
'Date: 6/23/2010 (2007 compatible)
'Summary: Open all Excel files in a specific folder and copy
' one sheet from the source files into this master workbook
' naming sheets for the names of the source workbooks
' Move imported files into another folder
Dim fName As String, fPath As String, fPathDone As String
Dim LR As Long, NR As Long, shtAdd As String, ShtName As Worksheet
Dim wbData As Workbook, wbkNew As Workbook
Dim bowStr As String
bowStr = "BOW"
Dim hcStr As String
hcStr = "HC"
Dim name1Str As String
name1Str = "Name1"
Dim name2Str As String
name2Str = "Name2"
Dim name3Str As String
name3Str = "Name3"
Dim name4Str As String
name4Str = "Name4"
Dim name5Str As String
name5Str = "Name5"
Dim name6Str As String
name6Str = "Name6"
Dim name7Str As String
name7Str = "Name7"
Dim name8Str As String
name8Str = "Name8"
Dim pos As Integer
'Setup
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Set wbkNew = ThisWorkbook
wbkNew.Activate
'Path and filename (edit this section to suit)
fPath = ThisWorkbook.Path & "\Test\" 'remember final \ in this string
fPathDone = fPath & "Imported\" 'remember final \ in this string
On Error Resume Next
MkDir fPathDone 'creates the completed folder if missing
On Error GoTo 0
fName = Dir(fPath & "*.xlsm") 'listing of desired files, edit filter as desired
'Import data from each found file
Do While Len(fName) > 0
'make sure THIS file isn't accidentally reopened
If fName <> wbkNew.Name Then
'This is the section to customize, what to copy and to where
'Get name of workbook without extension
shtAdd = Left(fName, InStr(fName, ".") - 1)
Select Case True
Case InStr(1, shtAdd, name1Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name1 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name1 HC"
End Select
Case InStr(1, shtAdd, name2Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name2 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name2 HC"
End Select
Case InStr(1, shtAdd, name3Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name3 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name3 HC"
End Select
Case InStr(1, shtAdd, name4Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name4 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name4 HC"
End Select
Case InStr(1, shtAdd, name5Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name5 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name5 HC"
End Select
Case InStr(1, shtAdd, name6Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name6 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name6 HC"
End Select
Case InStr(1, shtAdd, name7Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name7 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name7 HC"
End Select
Case InStr(1, shtAdd, name8Str, vbTextCompare) <> 0
Select Case True
Case InStr(1, shtAdd, bowStr, vbTextCompare) <> 0
shtAdd = "Name8 BOW"
Case InStr(1, shtAdd, hcStr, vbTextCompare) <> 0
shtAdd = "Name8 HC"
End Select
End Select
'Open file
Set wbData = Workbooks.Open(fPath & fName)
'Rename sheet and copy to target workbook
wbData.Sheets(2).Name = shtAdd
wbData.Sheets(2).Copy After:=wbkNew.Sheets(wbkNew.Sheets.Count)
'close source file
wbData.Close False
'move file to IMPORTED folder
Name fPath & fName As fPathDone & fName
'ready next filename, reassert the list since a file was moved
fName = Dir(fPath & "*.xlsm")
End If
Loop
ErrorExit: 'Cleanup
Application.DisplayAlerts = True 'turn system alerts back on
Application.EnableEvents = True 'turn other macros back on
Application.ScreenUpdating = True 'refreshes the screen
End Sub
Bookmarks