+ Reply to Thread
Results 1 to 9 of 9

Combining some sheets into another sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-31-2008
    Location
    Berkshire, UK
    MS-Off Ver
    2003 & 2007
    Posts
    118

    Combining some sheets into another sheet

    Hi all.

    Really appreciate everyone's help on this.

    I have a number of sheets in my workbook which I want to combine to a single sheet.

    The Data I want to copy is from A2:M? on Sheets called F1, F2, F3, F4, and pasted into a 5th sheet called FCombined.

    All of the above sheets have exactly the same layout.

    I did a search on combining sheets but the results threw up solutions where the sheets were the only ones in the entire workbook, I have other sheets which do different things.

    The only issue is that the quantity of records could change.

    I would be enormously grateful if anyone could help me out on this.

    Thanks in advance.

    Simon
    Last edited by simjambra; 03-11-2010 at 08:16 AM.

  2. #2
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Combining some sheets into another sheet

    Try:

    Sub test()
    
    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
    
    If ws.Name <> "FCombined" Then
    
    With ws.Range("A1").CurrentRegion
    
    .Offset(1, 0).Resize(.Rows.Count - 1, 13).Copy Sheets("FCombined").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    
    End With
    
    End If
    
    Next ws
    
    End Sub

    Dom
    Last edited by Domski; 03-11-2010 at 08:05 AM. Reason: Small correction to code
    "May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."

    Use code tags when posting your VBA code: [code] Your code here [/code]

    Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.

  3. #3
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining some sheets into another sheet

    Untested (ie coded on the fly) but perhaps:

    Sub Collate()
    Dim ws As Worksheet, wsFinal As Worksheet, rngCopy As Range, xlCalc As xlCalculation
    On Error GoTo ExitPoint
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set wsFinal = Sheets("FCombined")
    With wsFinal
        .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A")).Resize(,13).Clear
    End With
    For Each ws In ThisWorkbook.Worksheets
        Select Case UCase(ws.Name)
            Case "F1", "F2", "F3", "F4"
                With ws
                    Set rngCopy = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp)).Resize(, 13)
                End With
                With wsFinal
                    .Cells(.Rows.Count, "A").End(xlUp).Offset(1).Resize(rngCopy.Rows.Count, rngCopy.Columns.Count) = rngCopy.Value
                End With
        End Select
    Next ws
    ExitPoint:
    Set wsFinal = Nothing
    With Application
        .Calculation = xlCalc
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    End Sub

    the above assumes

    a) final sheet has header row

    b) final sheet should be purged with each collation

    c) values only copy


    EDIT: 12:14 - missing comma in the .Clear line at beginning of routine - did say it was untested
    Last edited by DonkeyOte; 03-11-2010 at 08:14 AM.

  4. #4
    Forum Contributor
    Join Date
    07-31-2008
    Location
    Berkshire, UK
    MS-Off Ver
    2003 & 2007
    Posts
    118

    Re: Combining some sheets into another sheet

    Dom,

    Thanks so much for the reply but I don't want to combine all sheets in the workbook, only the ones named F1, F2, F3 and F4.

    There are another 5 unrelated sheets, you see.

    Thanks very much

    Simon

  5. #5
    Forum Expert Domski's Avatar
    Join Date
    12-14-2009
    Location
    A galaxy far, far away
    MS-Off Ver
    Darth Office 2010
    Posts
    3,950

    Re: Combining some sheets into another sheet

    I'm sure Donkey's works but if not...

    Sub test()
    
    Dim ws As Worksheet
    
    For Each ws In ThisWorkbook.Worksheets
    
        Select Case ws.Name
    
        Case "F1", "F2", "F3", "F4"
    
            With ws.Range("A1").CurrentRegion
    
                .Offset(1, 0).Resize(.Rows.Count - 1, 13).Copy _
                    Sheets("FCombined").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    
            End With
    
        End Select
    
    End If
    
    Next ws
    
    End Sub

    Dom

  6. #6
    Forum Contributor
    Join Date
    07-31-2008
    Location
    Berkshire, UK
    MS-Off Ver
    2003 & 2007
    Posts
    118

    Re: Combining some sheets into another sheet

    Thanks both

    Reps added to.

    You guys are great

  7. #7
    Forum Contributor
    Join Date
    07-31-2008
    Location
    Berkshire, UK
    MS-Off Ver
    2003 & 2007
    Posts
    118

    Re: Combining some sheets into another sheet

    Donkey, that works a treat.

    Thanks ever so much.

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Combining some sheets into another sheet

    Quote Originally Posted by Simon
    Donkey...
    Note my edit - I'd omitted a comma in the Clear which will if left unchanged cause problems on re-run (will not purge correctly).

  9. #9
    Forum Contributor
    Join Date
    07-31-2008
    Location
    Berkshire, UK
    MS-Off Ver
    2003 & 2007
    Posts
    118

    Re: Combining some sheets into another sheet

    Quote Originally Posted by DonkeyOte View Post
    Note my edit - I'd omitted a comma in the Clear which will if left unchanged cause problems on re-run (will not purge correctly).
    Noted and updated, cheers

+ 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