+ Reply to Thread
Results 1 to 4 of 4

Macro to import all data into Mastersheet

Hybrid View

  1. #1
    Registered User
    Join Date
    08-04-2010
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    7

    Macro to import all data into Mastersheet

    Hello all,

    I have a workbook with multiple sheets that are used as forecasts so they are updated on a weekly basis. I am looking to use a macro that imports all the data into a new - Master sheet. Any suggestions? your help would be greatly appreciated.

    I have attached an example of the document! Please help!!

    -Matt
    Last edited by Matt Lee; 08-05-2010 at 11:10 AM.

  2. #2
    Forum Expert
    Join Date
    12-23-2006
    Location
    germany
    MS-Off Ver
    XL2003 / 2007 / 2010
    Posts
    6,326

    re: Macro to import all data into Mastersheet

    Hi,
    you'll find different possibilities at this site under Copy/paste/Merge examples

  3. #3
    Registered User
    Join Date
    07-27-2010
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Macro to import all data into Mastersheet

    Hi Matt,

    This code will add a new sheet and paste the data from all other sheets (Note: this assumes that all sheets are in the same format).

    Public Sub ImportData()
        Dim wksMaster As Excel.Worksheet
        Dim wksSheet As Excel.Worksheet
        Dim lLastDataRow As Long
        Dim lMasterRow As Long
        Dim lLastRow As Long
        Const lFIRST_DATA_ROW As Long = 3
        
        Set wksMaster = ThisWorkbook.Sheets.Add(, Sheets(ThisWorkbook.Sheets.Count))
        wksMaster.Name = "Master " & Format(Now(), "dd.mm.yy hhmm")
        
        ' copy the header rows
        Sheets(1).Rows("1:2").EntireRow.Copy
        wksMaster.Cells(1, 1).PasteSpecial , Paste:=xlAll
        
        lMasterRow = lFIRST_DATA_ROW
        For Each wksSheet In ThisWorkbook.Sheets
            
            If wksSheet.Name <> wksMaster.Name Then
            
                ' use the last filled cell in column A as the last data row
                lLastRow = wksSheet.Cells(wksSheet.Rows.Count, 1).End(xlUp).Row
                
                ' copy from the first to last rows
                wksSheet.Rows(lFIRST_DATA_ROW & ":" & lLastRow).Copy
                wksMaster.Cells(lMasterRow, 1).PasteSpecial , Paste:=xlAll
                
                ' set the new destination row on the master sheet
                lMasterRow = wksMaster.Cells(wksMaster.Rows.Count, 1).End(xlUp).Row
                
            End If
            
        Next
        
        Set wksMaster = Nothing
        Set wksSheet = Nothing
        
    End Sub
    Cheers,
    Dom

  4. #4
    Registered User
    Join Date
    08-04-2010
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    7

    Re: Macro to import all data into Mastersheet

    Now Im just curious, say the sheets are continually being changed and added to, they are forecasts. Will I have to run the macro over everytime a *NEW* forecast is added to one of the sheets?

+ 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