Combining/Merging Workbooks in need of help!!!!!!!

Status
Not open for further replies.

sehrlich

New Member
Joined
Apr 7, 2004
Messages
18
:rolleyes: :rolleyes: Hello,

Sorry about the length of this post, but I really need your help. So please bear with me.

I would like to merge a multitude of workbooks 5 - 15 (depending on the source) to populate to the main workbook. Keep in mind though, that each workbook will have similar data, but there maybe some worksheets that have data and some that don't. There are 14 worksheets in the workbook. So, what I need to do is create a macro that will
* Merge the workbooks without opening them
* When merging to the worksheets it will populate to the
worksheet in the Main workbookand will automatically go to the
next row
I recently found a macro on that will do this on the boards but, the infomation populates to a new worksheet. So for example, if I had a worksheet called Footwear in the Main Workbook it would create a Footwear 1, Footwear 2, Footwear 3, etc.

If anyone has any ideas please let me know. I am attaching the macro for your review. It is kind of lengthy so you may want to copy it or print it out.

Thank you for your help!! :oops:

Macro:
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 Sheets
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

' Create array of filenames; the True is for multi-select
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

' ubound determines how many items in the array
On Error GoTo quit

response = MsgBox("Retain Original Worksheet Names? (If No, then each copied worksheet will be given the name of the Excel file from which it came.", vbYesNo, "Copy Worksheets")
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
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Does anyone have any solutions? I am wondering if in the code I can just say go to a specific worksheet and update it instead of creating new worksheets. If I do this though, do I have to set a code for each worksheet?

Many thanks in advance,

Sherri
 
Upvote 0
Just seeing this for the first time, a few questions...

(1)
What is the essence of the request? That all data in all worksheets of all files in a certain folder path get "merged" to a single worksheet in a main workbook?

(2)
What exactly do you mean by "merged"?

(3)
Why do you not want the workbooks to open? There is a way to keep them closed and extract data programmatically but that generally involves simulating paste link formulas in a loop, which for 14 sheets times 10 workbooks times however many cells there are with data on each sheet would be a hell of a lot of formulas and extra work in the macro.

(4)
Is the source file path known?


Generally, try explaining in more English and less code what it is you want to accomplish, especially answering the above questions, and including destination worksheet names, source workbook file location(s), etc., plus any other detail you can muster, to help people see your issue more clearly.
 
Upvote 0
Okay, here it is in PLAIN English for those requesting:

What I'm looking for is code that will consolidate information from a number of workbooks (or Reports if you will), which have been created using a standard company template. The information in each worksheet of the multiple workbooks will be different according to the Region or District of our retail stores. The information must be consolidated into just ONE workbook on a regular basis for reporting purposes. I have created a template with one sheet that includes the instructions for the Admin Assistants along with a button that is to prompt them for the multiple workbooks that they want to consolidate. The goal for the Admin Assistants is to cut the time down on consolidating the information from the multiple workbooks (reports). Currently, the Admin Assistants are manually copying and pasting the information into one workbook in which we don't want that them to do that anymore. I have code that does help in pulling the information in from the multiple workbooks, but it creates multiple worksheets of the same name (for example, Operations, Operations(2), Operations(3), and so forth). What I need is code that will do the following:

1. Prompt the end user to select multiple workbooks;
2. Consolidate the information from the worksheets in the multiple workbooks and place the information into just ONE workbook of the "report".

PROBLEM: Not sure if the best way to do this is to include the proper worksheets, which are blank, in the template file and have code that searchs the workbooks and then copies and pastes the information accordingly. Remember, the template only has one worksheet that has the instructions and a button to start the consolidation.

Please assist...I am on a time crunch. :oops: :devilish:
 
Upvote 0
Duplicate of
http://www.mrexcel.com/board2/viewtopic.php?t=87427

And you did not answer my questions #3 and #4. And after your explanation above, other questions arrive such as whether all sheets ina given workbook will be automatically copied, or just the ones the AA would select.

I'm sure you know exactly what you want but try to imagine that everyone else does not, unless you explain the simple mechanics of what will need to happen. And please, stick to the same thread.
 
Upvote 0
Alrighty then TOM,
I am writting on behalf of sehrlich. :oops: Look, we created a new post because we weren't getting any responses from this one...well, guess what, someone responded to the new post (MISSION IMPOSSIBLE ---> Multiple File Import) immediately and he was quite nice about it too. We're not trying to be difficult and at the same time we didn't want to ramble on in a lengthy post just confusing people.

BTW, we did answer questions #3 and #4...AGAIN, in laymen's terms:
#3: We want to automate the process so that all the Admin has to do is click a button in the template to import the information in order to consolidate it all into ONE report.
#4: The Admins don't need to know the source file path because the regions and districts will be sending their files to them via email; therefore, they will save it to their local drive. In other words, one Admin will be getting up to 15 separate reports. That's why we want the code to prompt them for the files they want to consolidate.

We're almost there, but we need the extra assistance in acheiving the final result.

All we want is an answer and not a war.
-patti
 
Upvote 0
No one is giving you any wars, but if you or sehrlich or anyone is not clear with a question, that contributes to a general non-response, and / or questions from people who want to help but need more clarification. We are not mind readers. The people on this board are the most friendly and helpful you'll find anywhere on the web for solving Excel problems. We also have our time constraints and deadlines too, while finding time to help friendly people like you, free of charge. Double-posting after re-posting is an annoyance that hurts your cause more than helping it. And for the assistance you've received so far, you're welcome.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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