MISSION IMPOSSIBLE ---> Multiple File Import

Status
Not open for further replies.

sehrlich

New Member
Joined
Apr 7, 2004
Messages
18
Okay guys, I need some serious help here!!!! :oops:

Here is what I'm looking for, I have several workbooks that I need to import into just ONE workbook. I found some code that imports the workbooks into the ONE workbook, but it creates multiple worksheets of the same name. For instance, Human Resources(1), Human Resouces(2). Well, that's not quite what I need. I need to have the data from a particularlly named worksheet (for example, Human Resources) from multiple workbooks, which have been created using a template with the different named worksheets. I need the data from the multiple workbooks to be copied into just ONE worksheet of the same worksheet name in that ONE workbook. The following code is what brings the data into multiple worksheets of the same name, but this is NOT what i need...PLEASE HELP! :rolleyes: :rolleyes: :rolleyes: :rolleyes: :rolleyes: :rolleyes: :rolleyes:


'Opens the selected files
Workbooks.Open filenames(counter)
strSourceDataFile = ActiveWorkbook.Name
If strSourceDataFile <> strActiveBook Then
Set allwShts = Worksheets
For Each wSht In allwShts
Workbooks(strSourceDataFile).Activate

If wSht.Visible = True Then
Sheets(wSht.Name).Select
Sheets(wSht.Name).Copy before:= _
Workbooks(strActiveBook).Sheets(1)
Sheetname = Left(strSourceDataFile, Len(strSourceDataFile) - 4)
Workbooks(strActiveBook).Activate
'Check to see if a Sheet already has the name
If SheetExists(Sheetname) = True Then
ActiveSheet.Name = Left(strSourceDataFile, Len(strSourceDataFile) - 4) & "(" & wSht.Index & ")"
Else
ActiveSheet.Name = Left(strSourceDataFile, Len(strSourceDataFile) - 4)
End If
End If

Next wSht

Thanks a bunch :biggrin: :p
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Just before the end of your macro:

Code:
Application.DisplayAlerts = False
Do While Worksheets.Count > 1
    Worksheets(2).Select
    Application.Goto Reference:="R1C1"
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Cut
    ActiveSheet.Previous.Select
    Application.Goto Reference:="R65500C1"
    Selection.End(xlUp).Select
    ActiveCell.Offset(1, 0).Select
    ActiveSheet.Paste
    ActiveSheet.Next.Select
    ActiveWindow.SelectedSheets.Delete
    Loop
Application.DisplayAlerts = True

This will take all sheets and consolodate them into one by cut and paste.

(y)
 
Upvote 0
So, out of my entire code (see below), I just have to add your code to the end, right? If this is not correct, please assist...thanks :biggrin:

Option Explicit
Sub CopyWorksheets2()
Dim filenames As Variant
Dim strActiveBook As String
Dim strSourceDataFile As String
Dim wSht As Worksheet, wSht2 As Worksheet
Dim allwShts As Sheets, allwShts2 As String
Dim Sheetname As String, response As String
Dim counter As Integer, intResponse As Integer

Application.DisplayAlerts = False
intResponse = MsgBox("This macro will copy all worksheets from selected files to the current workbook. Continue?", vbOKCancel, "Copy Worksheets to Current File")

If intResponse = vbOK Then
strActiveBook = ActiveWorkbook.Name

filenames = Application.GetOpenFilename("Excel Files (*.xls),*.xls", , , , True)
On Error GoTo 1000
If filenames = False Then Exit Sub
1000
On Error GoTo 0

counter = 1

On Error GoTo quit

response = MsgBox("Retain Original Worksheet Names? (If No, then each copied worksheet will be tiven the name of the Excel file from which it came.", vbYesNo, "Copy Sheets")
Application.ScreenUpdating = False
If response = vbNo Then
While counter <= UBound(filenames)


'Opens the selected files
Workbooks.Open filenames(counter)
strSourceDataFile = ActiveWorkbook.Name
If strSourceDataFile <> strActiveBook Then
Set allwShts = Worksheets
For Each wSht In allwShts
Workbooks(strSourceDataFile).Activate

If wSht.Visible = True Then
Sheets(wSht.Name).Select
Sheets(wSht.Name).Copy before:= _
Workbooks(strActiveBook).Sheets(1)
Sheetname = Left(strSourceDataFile, Len(strSourceDataFile) - 4)
Workbooks(strActiveBook).Activate
'Check to see if a Sheet already has the name
If SheetExists(Sheetname) = True Then
ActiveSheet.Name = Left(strSourceDataFile, Len(strSourceDataFile) - 4) & "(" & wSht.Index & ")"
Else
ActiveSheet.Name = Left(strSourceDataFile, Len(strSourceDataFile) - 4)
End If
End If

Next wSht


Workbooks(strSourceDataFile).Activate
ActiveWorkbook.Close

'displays file name in a message box
MsgBox strSourceDataFile & " Has Been Processed", vbOKOnly + vbInformation, "File Processed"
'End If
'increment counter
counter = counter + 1
Wend
Else
While counter <= UBound(filenames)
'Opens the selected files
Workbooks.Open filenames(counter)
strSourceDataFile = ActiveWorkbook.Name
If strSourceDataFile <> strActiveBook Then
'Copy all worksheets except "Specifications"
Set allwShts = Worksheets
For Each wSht In allwShts
If wSht.Visible = True Then
Workbooks(strSourceDataFile).Activate
Sheets(wSht.Name).Select
Sheets(wSht.Name).Copy before:= _
Workbooks(strActiveBook).Sheets(1)
End If
Next wSht
Workbooks(strSourceDataFile).Activate
ActiveWorkbook.Close

'displays file name in a message box
MsgBox strSourceDataFile & " Has Been Processed", vbOKOnly + vbInformation, "File Processed"
End If
'increment counter
counter = counter + 1
Wend
End If

quit:
If Err <> 0 Then
MsgBox "An Error Occurred Trying to open the File. Please close any open Excel files and try again", vbOKOnly + vbExclamation, "File Open Error"
On Error GoTo 0
End If
End If
Set allwShts = Nothing
Application.ScreenUpdating = True
Application.DisplayAlerts = True

End Sub
Private Function SheetExists(sname) As Boolean
'Returns TRUE if sheet exists in the active workbook
Dim x As Object
On Error Resume Next
Set x = ActiveWorkbook.Sheets(sname)
If Err = 0 Then SheetExists = True _
Else SheetExists = False
On Error GoTo 0
End Function
End Function
 
Upvote 0
You can really help yourself and everone else if you do not make duplicate posts, which this is of this:
http://www.mrexcel.com/board2/viewtopic.php?t=86702

And if you answer the questions asked of you on that original post so everyone, including you, can be on the same page. You did not answer my question #3 or #4, and you did not explain if your Admin Assistants will select which sheets they want to consolidate, or if it would be all sheets in that workbook.

Please stick to the same thread.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,195
Latest member
Stevenciu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top