Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / General Excel Questions / September 2007

Tip: Looking for answers? Try searching our database.

Putting together worksheets

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
saman110 - 20 Sep 2007 00:38 GMT
Hello All,

The macro below putts and stacks all worksheets together in a new sheet
called "MergeSheet" and works fine, But my problem is I don't want to add all
sheets I just want to stack Sheet1, Sheet2 and Sheet3 together in
"MergeSheet". Where do I have to make corrections?

Thank you.

Sub Test2()
   Dim sh As Worksheet
   Dim DestSh As Worksheet
   Dim Last As Long
   Dim shLast As Long

   With Application
       .ScreenUpdating = False
       .EnableEvents = False
   End With

   'Delete the sheet "MergeSheet" if it exist
   Application.DisplayAlerts = False
   On Error Resume Next
   ThisWorkbook.Worksheets("MergeSheet").Delete
   On Error GoTo 0
   Application.DisplayAlerts = True

   'Add a worksheet with the name "MergeSheet"
   Set DestSh = ThisWorkbook.Worksheets.Add
   DestSh.Name = "MergeSheet"

   'loop through all worksheets and copy the data to the DestSh
   For Each sh In ThisWorkbook.Worksheets
       If sh.Name <> DestSh.Name Then
           Last = LastRow(DestSh)
           shLast = LastRow(sh)

           'This example copies everything, if you only want to copy
           'values/formats look at the example below the first example
           sh.Range(sh.Rows(2), sh.Rows(shLast)).Copy DestSh.Cells(Last + 1,
"A")

       End If
   Next

   Application.Goto DestSh.Cells(1)

   With Application
       .ScreenUpdating = True
       .EnableEvents = True
   End With
End Sub

Function LastRow(sh As Worksheet)
   On Error Resume Next
   LastRow = sh.Cells.Find(What:="*", _
                           After:=sh.Range("A1"), _
                           Lookat:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Row
   On Error GoTo 0
End Function
JW - 20 Sep 2007 05:25 GMT
> Hello All,
>
[quoted text clipped - 63 lines]
> --
> Message posted via OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/ms-excel/200709/1

One quick way:

Replace this:
If sh.Name <> DestSh.Name Then

With this:
If sh.Name = "Sheet1" or sh.Name = "Sheet2" or sh.Name = "Sheet3" Then
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.