Consulting

Results 1 to 12 of 12

Thread: Solved: Copy data from all the sheets in a workbook into a single sheet

  1. #1
    Banned VBAX Regular
    Joined
    Mar 2007
    Posts
    13
    Location

    Solved: Copy data from all the sheets in a workbook into a single sheet

    Hi,
    I have a workbook with 8 sheets. The headings on all the sheets are the same. I want to copy the data of each sheet and paste it into a new sheet.. in short i want to consolidate the data on all the sheets into one sheet.
    Is this possible through VBA ??
    If yes.. pls help me with the same.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    See if this works for you....
    [VBA]Option Explicit
    Sub CopyFromWorksheets()
    Dim wrk As Workbook 'Workbook object - Always good to work with object variables
    Dim sht As Worksheet 'Object for handling worksheets in loop
    Dim trg As Worksheet 'Master Worksheet
    Dim rng As Range 'Range object
    Dim colCount As Integer 'Column count in tables in the worksheets

    Set wrk = ActiveWorkbook 'Working in active workbook

    For Each sht In wrk.Worksheets
    If sht.Name = "Master" Then
    MsgBox "There is a worksheet called as 'Master'." & vbCrLf & _
    "Please remove or rename this worksheet since 'Master' would be " & _
    "the name of the result worksheet of this process.", vbOKOnly + vbExclamation, "Error"
    Exit Sub
    End If
    Next sht

    'We don't want screen updating
    Application.ScreenUpdating = False

    'Add new worksheet as the last worksheet
    Set trg = wrk.Worksheets.Add(After:=wrk.Worksheets(wrk.Worksheets.Count))
    'Rename the new worksheet
    trg.Name = "Master"
    'Get column headers from the first worksheet
    'Column count first
    Set sht = wrk.Worksheets(1)
    colCount = sht.Cells(1, 255).End(xlToLeft).Column
    'Now retrieve headers, no copy&paste needed
    With trg.Cells(1, 1).Resize(1, colCount)
    .Value = sht.Cells(1, 1).Resize(1, colCount).Value
    'Set font as bold
    .Font.Bold = True
    End With

    'We can start loop
    For Each sht In wrk.Worksheets
    'If worksheet in loop is the last one, stop execution (it is Master worksheet)
    If sht.Index = wrk.Worksheets.Count Then
    Exit For
    End If
    'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
    Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
    'Put data into the Master worksheet
    trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Next sht
    'Fit the columns in Master worksheet
    trg.Columns.AutoFit

    'Screen updating should be activated
    Application.ScreenUpdating = True
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Regular
    Joined
    Jun 2007
    Location
    Texas
    Posts
    62
    Location
    Lucas,

    You rock...... We were just discussing this very issue in our office this morning.....

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    I yoinked it from someone else...can't remember who..

    It is handy though..
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Banned VBAX Regular
    Joined
    Mar 2007
    Posts
    13
    Location
    Thanks Lucus .... it works:

  6. #6
    VBAX Newbie
    Joined
    Jul 2007
    Posts
    3
    Location

    how to skip 1 sheet

    This is great.

    How would I skip 1 sheet out of 22 so that it's contents didn't get copied to the master sheet?

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]
    For Each sht In wrk.Worksheets
    if sht.name<>"Skip" then
    'do things.
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Newbie
    Joined
    Jul 2007
    Posts
    3
    Location
    When I tried that I got an error compiler error: next without for

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You need to add the corresponding End If. Here's the whole section
    [VBA]
    'We can start loop
    For Each sht In wrk.Worksheets
    If sht.Name <> "Skip" Then
    'If worksheet in loop is the last one, stop execution (it is Master worksheet)
    If sht.Index = wrk.Worksheets.Count Then
    Exit For
    End If
    'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
    Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
    'Put data into the Master worksheet
    trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    End If
    Next sht

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    VBAX Newbie
    Joined
    Jul 2007
    Posts
    3
    Location
    ok i tried
    [VBA]
    'If worksheet in loop is the last one, stop execution (it is Master worksheet)
    If sht.Index = wrk.Worksheets.Count Then
    Exit For
    End If
    'Data range in worksheet - starts from second row as first rows are the header rows in all worksheets
    If sht.Name <> "Labour" Then
    Set rng = sht.Range(sht.Cells(2, 1), sht.Cells(65536, 1).End(xlUp).Resize(, colCount))
    'Put data into the Master worksheet
    trg.Cells(65536, 1).End(xlUp).Offset(1).Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    End If
    Next sht
    [/VBA]
    i don't get an error but I have one more sheet after labour and it skips it.

  11. #11
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by lucas
    I yoinked it from someone else...can't remember who..

    It is handy though..
    I think it may be smozgurs
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I gave you the whole section. Why did you do more than substitute Labour for Skip?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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