+ Reply to Thread
Results 1 to 8 of 8

Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

  1. #1
    Registered User
    Join Date
    03-04-2014
    Location
    Madison, WI
    MS-Off Ver
    Excel 2010
    Posts
    5

    Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    I am looping through a directory of spreadsheets to copy and paste variable rows (set column range) to a master spreadsheet. I am having a problem with one line of code - the destination paste part. Each spreadsheet in the directory will open and copy and paste but I can't get the pasting of subsequent spreadsheets to paste further down the master sheet. It just overlays the data for every spreadsheet until the end of the directory list.

    .Range(("A2:AZ") & Range("B" & Rows.Count).End(xlUp).Row).Copy _
    Dataset.Range("A" & (emptyRow)).End(xlUp).Offset(1, 0)

    emptyRow (Integer) is the row number of the next empty row in the master spreadsheet (it is correct as I have tested this).

    Thanks in advance.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    Hi Utz,

    Can you post the surrounding code?
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    Try moving from the bottom of the sheet.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,528

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    You said you tested it. What did you test?
    Test this and see what you get.
    Please Login or Register  to view this content.
    We can't see what the empty row is as you only show a couple of your code lines here but I assume it is the first empty cell in Column A
    Try
    Please Login or Register  to view this content.
    or something similar.

  5. #5
    Registered User
    Join Date
    03-04-2014
    Location
    Madison, WI
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    I am really sorry, I failed to mention the first row of the source data is a header and will not be brought forward to the destination spreadsheet. Below is the complete section of code:

    emptyRow = (Dataset.Range("B" & Rows.Count).End(xlUp).Row) + 1
    rowCount = sourceData.Range("B" & Rows.Count).End(xlUp).Row

    If sourceData.cells(rowCount, 2) > 1 Then
    'Loop through Source - paste to Master
    With sourceData
    If rowCount > 2 Then
    .cells.EntireRow.Interior.ColorIndex = 0
    .cells.EntireRow.Borders.LineStyle = xlNone
    Debug.Print Dataset.Rows.Count
    .Range(("A2:AZ") & Range("B" & Rows.Count).End(xlUp).Row).Copy _
    Dataset.cells(Dataset.Rows.Count, 2).End(xlUp).Offset(1, 0)
    ' Dataset.Range("A" & (emptyRow)).End(xlUp).Offset(1, 0)
    Else
    .cells.EntireRow.Interior.ColorIndex = 0
    .cells.EntireRow.Borders.LineStyle = xlNone
    .Range(("A2:AZ") & Range("B" & Rows.Count).End(xlUp).Row).Copy _
    Dataset.cells(Dataset.Rows.Count, 2).End(xlUp).Offset(1, 0)
    ' Dataset.Range("A" & (emptyRow)).End(xlUp).Offset(1, 0)
    End If
    'Application.CutCopyMode = False
    End With
    End If

  6. #6
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    Hi Utz,

    I found some syntax errors in your code - see if this fixes it:

    Please Login or Register  to view this content.
    Last edited by xladept; 05-29-2014 at 04:57 PM.

  7. #7
    Registered User
    Join Date
    03-04-2014
    Location
    Madison, WI
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    Almost - made a tweak to your code and now it works beautifully.
    Please Login or Register  to view this content.

  8. #8
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Dynamic Range Paste Problem within a Loop of spreadsheets in a directory - Excel 2013

    Glad you got it working! Thanks for the rep!

+ 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. Replies: 1
    Last Post: 04-11-2014, 05:00 PM
  2. Replies: 4
    Last Post: 04-03-2014, 12:19 AM
  3. [SOLVED] Bizarre paste problem with 2013 - sometimes!
    By gsullymorgan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-17-2014, 09:43 AM
  4. Problem using loop for sending excel range in email body using Range to HTML code
    By drajanm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-24-2012, 03:17 AM
  5. Loop through range names for location of dynamic paste
    By AlvaroSiza in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-17-2011, 11:01 AM

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