+ Reply to Thread
Results 1 to 2 of 2

Combining Mulitple Sheets into one but it does it twice

  1. #1
    Registered User
    Join Date
    10-30-2012
    Location
    Iowa
    MS-Off Ver
    Excel 2007
    Posts
    30

    Combining Mulitple Sheets into one but it does it twice

    Hi All-

    Below is my code to combine all the sheets into a Master Data sheet called summary. The code does what it's suppose to do but it's duplicating everything twice. I have to remove the duplicates manually. Where am I going wrong?

    PHP Code: 
     Sub CombineSheets()
      
      
    'This macro will copy all rows from the first sheet
        '
    (including headers)
        
    'and on the next sheets will copy only the data
        '
    (starting on row 2)

        
    Dim i As Integer
        Dim j 
    As Long
        Dim SheetCnt 
    As Integer
        Dim lstRow1 
    As Long
        Dim lstRow2 
    As Long
        Dim lstCol 
    As Integer
        Dim ws1 
    As Worksheet

        With Application
            
    .DisplayAlerts False
            
    .EnableEvents False
            
    .ScreenUpdating False
        End With

        On Error Resume Next

        
    'Delete the Summary Sheet on the document (in case it exists)
        Sheets("Summary").Delete
        '
    Count the number of sheets on the Workbook
        SheetCnt 
    Worksheets.Count

        
    'Add the Summary Sheet
        Sheets.Add After:=Worksheets(SheetCnt)
        ActiveSheet.Name = "Summary"
        Set ws1 = Sheets("Summary")
        lstRow2 = 1
        '
    Define the row where to start copying
        
    '(first sheet will be row 1 to include headers)
        j = 1

        '
    Combine the sheets
        
    For 1 To SheetCnt
            Worksheets
    (i).Select

            
    'check what is the last column with data
            lstCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column

            '
    check what is the last row with data
            lstRow1 
    ActiveSheet.Cells(ActiveSheet.Rows.Count"A").End(xlUp).Row

            
    'Define the range to copy
            Range("A" & j, Cells(lstRow1, lstCol)).Select

            '
    Copy the data
            Selection
    .Copy
            ws1
    .Range("A" lstRow2).PasteSpecial
            Application
    .CutCopyMode False

            
    'Define the new last row on the Summary sheet
            lstRow2 = ws1.Cells(65536, "A").End(xlUp).Row + 1

            '
    Define the row where to start copying
            
    '(2nd sheet onwards will be row 2 to only get data)
            j = 2
        Next

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

        Sheets("Summary").Select
        Cells.EntireColumn.AutoFit
        Range("A1").Select

    End Sub 

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    You need to exclude the Summary worksheet.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

+ 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