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
 
Heh

Sorry but I had to reply to this.

This really entertained me, it's amazing to see people asking for help like this.. and expecting people to help them.

Go git em tom!

:devilish: :devilish: :devilish: :devilish:
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
I had to try your code Tom and with the wb that I selected locally it appeared to work correctly. With the data that I had available.

Keep up the GREAT work!

(y)

Enjoy,
Carl.
 
Upvote 0
To DaneHanson and caricc - -

You guys have absolutely made my day today, thank you.

Most people who come to these boards for assistance are very friendly, courteous, and just need a nudge. And they are grateful. I love helping them, as I know other question-answerers on this board enjoy the chance to help others as well. I have also learned quite a bit over the years from observing how others answer a question differently than I would have, as there is always more than 1 way to solve an Excel problem.

I vividly remember this thread and still think about it from time to time, because it still stands as the one involving the most difficult people I've ever offered programming assistance to. I don't care who thinks that is not a nice thing to say; that's just the way this one was. Thankfully, their rudeness (and crashing of my corporate email which they also did that day) is the miniscule exception.

Keep visiting this MrExcel message board, which I think is the best on the internet for Excel information.

Thanks again you guys.
 
Upvote 0
This thread has it all!

Action!

Surprise posters!

Drama!

Email Crashing!

Next week on Excel Creek!

=P


I could literally see the vein in Tom's head grow larger with each post.

:wink:

And let me say this is THE nicest board I have ever been to for help with excel, and the rate at witch it moves is very fast.

I know now to not bump my thread, because people will always get to it, a little patience goes a long way.
 
Upvote 0
I know that I have had my share of problems, mutiple posts on different boards etc...But by far this is the best of them all. I have gotten more help here than anywhere else. Anyone who abuses this boards privilidge of getting the necessary help, without waiting patiently. gets what they deserve.

Carl.
 
Upvote 0
Thanks a lot for the kind words usurper4, much appreciated.

I don't want to embarass these two people, but a postscript to this thread took place a few days later and is an example of why & how not to cross-post on these message boards. This same person posted her question with my code on the MS server, not referencing who she got the code from or any of the history that could have beeen useful for anyone (Dave Peterson in this case) to help her. The link is here...click the "Show quoted text" links to see what she did and did not say:

http://groups.google.com/group/microsoft.public.excel.programming/browse_frm/thread/9465bfecfd35632f/762bef05490a77e5?lnk=st&q=excel+'Variable+declarations++%22so+close+yet+so+far%22&rnum=1&hl=en#762bef05490a77e5

This got her no further ahead, and in fact what I posted 3 days earlier here really did do what she wanted.

Although 99.9% of people on this and other Excel boards are very courteous, friendly, and appreciative of the help they received, especially when the solution works, there are a few bad apples like these two boors who practice scorched earth posting tactics. I've long put this one behind me and don't hold bad feelings for anyone. There's too much satisfaction from helping the 99.9% to fret about the .1%, and at least maybe this thread has helped show newcomers to these message boards how not to act.

This thread still pops up occasionally, and I do appreciate the nice follow-up comments. I bet most helpers on this and other boards who volunteer their time have their own stories, which is also why I have tremendous respect for people who work in customer service and computer support troubleshooting jobs.

Maybe it's time for one of the admins to please lock this thread, as it's run its course and we can move on to the real business of Excel. Thanks !!
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,216,028
Messages
6,128,399
Members
449,446
Latest member
CodeCybear

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