+ Reply to Thread
Results 1 to 3 of 3

combining sheets (code works from ron debruin but need 1 line modification)

  1. #1
    Registered User
    Join Date
    12-10-2004
    Posts
    40

    combining sheets (code works from ron debruin but need 1 line modification)

    Hi gurus,

    I got this code that combines all worksheets into a master worksheet. It works beautifully. How do i modify the for statement to only combine sheet1 and sheet2?? Help out


    Sub Combineallsheets() ' I WANT TO MODIFY FOR ONLY SHEET 1 and SHEET 2
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim shLast As Long
    Dim Last As Long

    On Error Resume Next
    If Len(ThisWorkbook.Worksheets.Item("Master").Name) = 0 Then
    On Error GoTo 0
    Application.ScreenUpdating = False
    Set DestSh = Worksheets.Add
    DestSh.Name = "Master"
    For Each sh In ThisWorkbook.Worksheets
    If sh.Name <> DestSh.Name Then
    Last = LastRow(DestSh)
    shLast = LastRow(sh)

    sh.Range(sh.Rows(3), sh.Rows(shLast)).Copy DestSh.Cells(Last + 1, "A")

    End If
    Next
    Cells(1).Select
    Application.ScreenUpdating = True
    Else
    MsgBox "The sheet Master already exist"
    End If
    End Sub

  2. #2
    Registered User
    Join Date
    12-10-2004
    Posts
    40

    here is what i thought might work but i get nothing in master worksheet

    Option Explicit

    Sub Combining() 'Combines both sheets 1 and sheets 2 to a new sheet called master
    Dim sh As Worksheet
    Dim DestSh As Worksheet
    Dim Last As Long
    Start:
    On Error Resume Next

    If Len(ThisWorkbook.Worksheets.Item("Master").Name) = 0 Then
    On Error GoTo 0
    Application.ScreenUpdating = False
    Set DestSh = Worksheets.Add
    DestSh.Name = "Master"
    For Each sh In ThisWorkbook.Worksheets

    If sh.Name = "sheet1" Then

    GoTo skip


    If sh.Name <> DestSh.Name Then
    Last = LastRow(DestSh)

    sh.UsedRange.Copy DestSh.Cells(Last + 1, "A")
    'Instead of this line you can use the code below to copy only the values
    'or use the PasteSpecial option to paste the format also.


    'With sh.UsedRange
    'DestSh.Cells(Last + 1, "A").Resize(.Rows.Count, _
    '.Columns.Count).Value = .Value
    'End With


    'sh.UsedRange.Copy
    'With DestSh.Cells(Last + 1, "A")
    ' .PasteSpecial xlPasteValues, , False, False
    ' .PasteSpecial xlPasteFormats, , False, False
    ' Application.CutCopyMode = False
    'End With

    End If
    End If


    skip:

    Next
    Cells(1).Select
    Application.ScreenUpdating = True
    Else
    Application.DisplayAlerts = False
    Worksheets("Master").Delete
    Application.DisplayAlerts = True

    GoTo Start

    'MsgBox "The sheet Master already exist."




    End If
    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

  3. #3
    Registered User
    Join Date
    12-10-2004
    Posts
    40

    ok i worked it..

    ok mine works

    i fixed it

    thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1