473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Combine worksheets

I found this code online i really need to merge all me file together can some tell me were i going wrong with this.



  1. Sub CopyRangeFromMultiWorksheets()
  2. Dim sheet As Worksheet
  3. Dim DestSheet As Worksheet
  4. Dim Last As Long
  5. Dim CopyRng As Range
  6. With Application
  7. .ScreenUpdating = False
  8. .EnableEvents = False
  9. End With
  10. ' Delete the summary sheet if it exists.
  11. Application.DisplayAlerts = False
  12. On Error Resume Next
  13. ActiveWorkbook.Worksheets("RDBMergeSheet").Delete
  14. On Error GoTo 0
  15. Application.DisplayAlerts = True
  16. ' Add a new summary worksheet.
  17. Set DestSh = ActiveWorkbook.Worksheets.Add
  18. DestSh.Name = "RDBMergeSheet"
  19. ' Loop through all worksheets and copy the data to the
  20. ' summary worksheet.
  21. For Each sh In ActiveWorkbook.Worksheets
  22. If sh.Name <> DestSh.Name Then
  23. ' Find the last row with data on the summary worksheet.
  24. Last = LastRow(DestSheet)
  25. ' Specify the range to place the data.
  26. Set CopyRng = sh.Range("A1:G1")
  27. ' Test to see whether there are enough rows in the summary
  28. ' worksheet to copy all the data.
  29. If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
  30. MsgBox "There are not enough rows in the " & _
  31. "summary worksheet to place the data."
  32. GoTo ExitTheSub
  33. End If
  34. ' This statement copies values and formats from each
  35. ' worksheet.
  36. CopyRng.Copy
  37. With DestSh.Cells(Last + 1, "A")
  38. .PasteSpecial xlPasteValues
  39. .PasteSpecial xlPasteFormats
  40. Application.CutCopyMode = False
  41. End With
  42. ' Optional: This statement will copy the sheet
  43. ' name in the H column.
  44. DestSh.Cells(Last + 1, "H").Resize(CopyRng.Rows.Count).Value = sh.Name
  45. End If
  46. Next
  47. ExitTheSub:
  48. Application.Goto DestSh.Cells(1)
  49. ' AutoFit the column width in the summary sheet.
  50. DestSh.Columns.AutoFit
  51. With Application
  52. .ScreenUpdating = True
  53. .EnableEvents = True
  54. End With
  55. End Sub
Oct 26 '11 #1
0 914

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: JMCN | last post by:
hello, i have two worksheets that i need to import from a workbook that has a total of 5 worksheets. i tried to use this line of code but i run into an error message that it cannot find the...
1
by: Dan | last post by:
I have a c# app that creates and Excel doc, I would like to know how to create new worksheets (tabs) in the excel doc using c#?
4
by: paul.chae | last post by:
I have a table in Access with about 3000 records. There are ~60 unique values in the ID field for the 3000 records. What I would like to do is automatically generate multiple Excel worksheets...
1
by: J Daniel Melton | last post by:
Hello, I am using late binding in a managed VC++ .NET 2003 application. I used KB 302902 (for C#) as a starting point and converted it to managed C++. I built a managed class that is intantiated...
3
by: mike11d11 | last post by:
I was able to create three worksheets in my workbook, but when I go to add the 4th I get an Invalid Index error. I must be leaving something out to when adding 4 or more sheets. Thanks Dim...
6
by: Peter Plate | last post by:
Hi all. I have a system which operates on Windows MSSQL. It is used for registering Suppotr requests. The system works with different Tables inside a Database. One of the tables is for new...
7
by: Claudia d'Amato | last post by:
I would like to do something in a *.vbs script and all the operations should be applied on each worksheet within an Excel file. How do I do this? It must be something like: for i in (1 .....
1
by: MarkDotNet | last post by:
Hi I am trying to switch Excel worksheets in VBA. I get an error saying "subscript out of range". Please Help- Here is code (Fails on last 2 lines- Note that I ommited the recordset portion of...
2
by: timleonard | last post by:
I am trying to copy the contents of 6 to 10 worksheets and paste them into one called "coverpage" I have been working with the following code, I've managed to get it to paste data to the coverpage...
2
by: patrick keady | last post by:
This feels simple. But not enough coffee I suppose. Cant get it to work. I have about ten worksheets in a workbook. The first worksheet is where I want ROWS returned to from the other 9...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.