+ Reply to Thread
Results 1 to 3 of 3

copying and data of varying lengths from multiple workbooks and pasting into one worksheet

  1. #1
    Registered User
    Join Date
    09-05-2015
    Location
    Los Angeles, CA
    MS-Off Ver
    MS Office 2013
    Posts
    2

    copying and data of varying lengths from multiple workbooks and pasting into one worksheet

    Hi,

    I have multiple workbooks with data (one worksheet in each workbook) that I need to copy and paste into one master workbook on one worksheet. I wrote the following macro and it works, except that every time it pastes, it overwrites the data of the worksheet right before it. Where have I gone wrong?

    Sub CopyPaste()
    '
    ' CopyPaste Macro

    Dim FolderPath As String, Filepath As String, Filename As String

    FolderPath = "C:\Users\Felicity\Desktop\project\"

    Filepath = FolderPath & "*.xls*"

    Filename = Dir(Filepath)

    Dim lastrow As Long, lastcolumn As Long

    Do While Filename <> ""
    Workbooks.Open (FolderPath & Filename)

    lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
    Application.DisplayAlerts = False
    ActiveWorkbook.Close

    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 24))

    Filename = Dir

    Loop
    Application.DisplayAlerts = True
    End Sub

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,422

    Re: copying and data of varying lengths from multiple workbooks and pasting into one works

    Try
    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    09-05-2015
    Location
    Los Angeles, CA
    MS-Off Ver
    MS Office 2013
    Posts
    2

    Re: copying and data of varying lengths from multiple workbooks and pasting into one works

    Thank you for your help! I was able to get this macro to run correctly when attached to the workbook. But if I put it in my personal workbook it doesn't work anymore. So weird!

    Sub copypaste()

    Dim FolderPath As String, Filepath As String, Filename As String

    FolderPath = "\Users\Felicity\Desktop\project\"


    Filepath = FolderPath & "*.xls*"


    Filename = Dir(Filepath)


    Dim lastrow As Long, lastcolumn As Long



    Do While Filename <> ""
    Workbooks.Open (FolderPath & Filename)


    lastrow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
    lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    Range(Cells(2, 1), Cells(lastrow, lastcolumn)).Copy
    Application.DisplayAlerts = False
    ActiveWorkbook.Close


    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    lastcolumn = ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, lastcolumn))




    Filename= Dir



    Loop
    Application.DisplayAlerts = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Copying data from multiple workbooks having multiple sheets into one master worksheet
    By Navya Ahuja in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-15-2014, 09:42 AM
  2. [SOLVED] Sumproduct, Indirect, Address over multiple sheets and varying row lengths
    By dma1976 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-08-2014, 11:12 AM
  3. [SOLVED] Concatenate with varying lengths and data
    By Unident in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-21-2013, 04:39 PM
  4. Replies: 1
    Last Post: 11-21-2012, 11:23 AM
  5. Replies: 1
    Last Post: 07-20-2012, 06:20 PM
  6. Combining Data of varying lengths
    By jaj8372 in forum Excel General
    Replies: 14
    Last Post: 06-08-2010, 03:47 PM
  7. Creating Charts with varying X/Y-axis data lengths
    By Lewis1 in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 07-29-2009, 12:07 AM

Tags for this Thread

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