[COLOR=darkblue]Sub[/COLOR] ImportWorksheets2()
[COLOR=darkblue]Dim[/COLOR] aPath [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Variant[/COLOR] [COLOR=green]'array of file paths[/COLOR]
[COLOR=darkblue]Dim[/COLOR] wb [COLOR=darkblue]As[/COLOR] Workbook
[COLOR=darkblue]Dim[/COLOR] wbTemp [COLOR=darkblue]As[/COLOR] Workbook
[COLOR=darkblue]Dim[/COLOR] wsTemp [COLOR=darkblue]As[/COLOR] Worksheet
[COLOR=darkblue]Dim[/COLOR] ws [COLOR=darkblue]As[/COLOR] Worksheet
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Integer[/COLOR] [COLOR=green]'loop index[/COLOR]
[COLOR=darkblue]Dim[/COLOR] sFile [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR] 'file to open
[COLOR=darkblue]Dim[/COLOR] counter [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR] [COLOR=green]'append to sheet name for unique name[/COLOR]
[COLOR=darkblue]Dim[/COLOR] fName [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]String[/COLOR]
[COLOR=green]'==============================[/COLOR]
[COLOR=green]'populate an array of filenames[/COLOR]
[COLOR=green]'===============================[/COLOR]
fName = ThisWorkbook.Sheets("Sheet1").Cells(5, 4).Value
[COLOR=green]'array of file paths[/COLOR]
aPath = Array("c:\temp\" & fName & ".xls", _
"c:\temp\sub-folder1\" & fName & ".xls", _
"c:\temp\sub-folder2\" & fName & ".xls")
[COLOR=green]'cater for the file doesn't exist[/COLOR]
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] [COLOR=darkblue]Next[/COLOR]
[COLOR=green]'==================================[/COLOR]
[COLOR=green]'add a new workbook with one sheet[/COLOR]
[COLOR=green]'=================================[/COLOR]
Workbooks.Add 1
[COLOR=darkblue]Set[/COLOR] wbTemp = ActiveWorkbook
[COLOR=darkblue]Set[/COLOR] wsTemp = wbTemp.Sheets("Sheet1")
[COLOR=green]'============================[/COLOR]
[COLOR=green]'copy sheets to new workbook[/COLOR]
[COLOR=green]'============================[/COLOR]
[COLOR=darkblue]For[/COLOR] i = [COLOR=darkblue]LBound[/COLOR](aPath) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](aPath)
[COLOR=green]'open each workbook in the array[/COLOR]
[COLOR=green]'and copy the worksheets[/COLOR]
[COLOR=darkblue]Set[/COLOR] wb = Workbooks.Open(aPath(i))
[COLOR=darkblue]For[/COLOR] [COLOR=darkblue]Each[/COLOR] ws [COLOR=darkblue]In[/COLOR] wb.Worksheets
[COLOR=red] '========================[/COLOR]
[COLOR=red] 'exclude worksheets[/COLOR]
[COLOR=red] '========================[/COLOR]
[COLOR=darkblue]If[/COLOR] ws.Name <> "xxx" And ws.Name <> "yyy" [COLOR=darkblue]Then[/COLOR]
counter = counter + 1
ws.Copy After:=wbTemp.Sheets(wbTemp.Sheets.Count)
ActiveSheet.Name = ws.Name & "_" & counter
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] ws
[COLOR=green]'close the workbook[/COLOR]
wb.Close SaveChanges:=[COLOR=darkblue]False[/COLOR]
[COLOR=green]'delete the data source[/COLOR]
[COLOR=green]'Kill aPath(i)[/COLOR]
[COLOR=darkblue]Set[/COLOR] wb = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Next[/COLOR] i
[COLOR=green]'=======================[/COLOR]
[COLOR=green]'close the new workbook[/COLOR]
[COLOR=green]'=======================[/COLOR]
fName = "c:\my_database\" & fName & ".xls"
[COLOR=darkblue]With[/COLOR] wbTemp
.SaveAs fName
.Close SaveChanges:=[COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
[COLOR=green]'tidy up[/COLOR]
[COLOR=darkblue]Set[/COLOR] wsTemp = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Set[/COLOR] wbTemp = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Set[/COLOR] ws = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]Set[/COLOR] wb = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]