+ Reply to Thread
Results 1 to 14 of 14

Record Macro to Copy / Paste Dynamic Ranges

Hybrid View

  1. #1
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Record Macro to Copy / Paste Dynamic Ranges

    Good evening.

    I've recorded a Macro (I'm new to this process) that copies and pastes data from two workbooks into a table in yet a 3rd 'Calc' wb. As you can see, the "HIST" file is first followed by the "WIP" file.

    The problem I'm having is that the 'Hist' file varies in number of rows from day to day. Currently, if the Hist file is longer than the file I used when recording the Macro, the WIP file overlaps Historical data. Conversely, on days when the 'Hist' file is shorter, the WIP data is pasted somewhere below the table.

    So, how do I get the WIP data copied and pasted directly beneath the Hist data regardless of the number of rows in the Hist file?

    Thanks, Steve



    Sub GetData()
    '
    ' GetData Macro
    '
    
    '
        Range("WIP[WC]").Select
        ChDir "C:\Users\Steve\Desktop\Test folder"
        Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\HIST.xlsx"
        Range("A2:O2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Windows("Morning Prod Calc (v1.5a).xlsx").Activate
        ActiveSheet.Paste
        Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\WIP.xlsx"
        Range(Selection, Cells(ActiveCell.Row, 1)).Select
        Range("A2:O2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Morning Prod Calc (v1.5a).xlsx").Activate
        Range("A2").Select
        Selection.End(xlDown).Select
        Range("A12").Select
        ActiveSheet.Paste
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("A2").Select
    End Sub
    Attached Files Attached Files

  2. #2
    Forum Contributor BeachRock's Avatar
    Join Date
    11-01-2011
    Location
    Oregon, United States
    MS-Off Ver
    Excel 2016
    Posts
    403

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve,

    If everything is manually entered, on both HIST and WIP you could hide row 2, blank with no data but with the same formatting as the rows below 2, and then setup both HIST and WIP with macros to insert new rows below row 2 for you to enter your data into for each new record. Then, you create a named range for each file, A2 through 1 row past the last row with data through Column O. Use those named ranges in your code instead of cell ranges.

    As long as you don't delete the row below the last row of data, the ranges will stay in place and automatically grow and shrink.

    Your code could look something like this, which isn't much different at all:

    Sub GetData()
    '
    ' GetData Macro
    '
    
    '
        Range("WIP[WC]").Select
        ChDir "C:\Users\Steve\Desktop\Test folder"
        Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\HIST.xlsx"
     '   
        Range("HistRange").Select
     '   
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        Windows("Morning Prod Calc (v1.5a).xlsx").Activate
        ActiveSheet.Paste
        Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\WIP.xlsx"
        Range(Selection, Cells(ActiveCell.Row, 1)).Select
     '   
        Range("WipRange").Select
     '   
        Range(Selection, Selection.End(xlDown)).Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Morning Prod Calc (v1.5a).xlsx").Activate
        Range("A2").Select
        Selection.End(xlDown).Select
        Range("A12").Select
        ActiveSheet.Paste
        Cells.Select
        Cells.EntireColumn.AutoFit
        Range("A2").Select
    End Sub
    -------------
    Tony

  3. #3
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Thank you, BeachRock. Actually, the Hist and Wip files are much larger. Hist is usually in the >20K row range and WIP is ~4K rows and both files are pulled (queried) from a main database. I get the files each morning and copy/paste them into the calc wb which feeds a production performance dashboard.

    Not sure I understand the 'hide row 2' scenario...?

  4. #4
    Forum Contributor BeachRock's Avatar
    Join Date
    11-01-2011
    Location
    Oregon, United States
    MS-Off Ver
    Excel 2016
    Posts
    403

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve,

    Ok, so they aren't being populated manually... How are they being populated from your main database?

    The 2nd row being hidden allows you to use it as the beginning of your range but is never used other than to be able to insert new records under it so the range is dynamic and includes all rows that are added. You have to also include one row under the last row with data for it to remain dynamic.

  5. #5
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve N

    The HIST File you posted has Data in Rows 2 through 6, empty Rows 7 through 15 and more Data in Rows 16 through 20.

    Is this what your actual file looks like? Data then empty Rows then more Data?
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  6. #6
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Re: Record Macro to Copy / Paste Dynamic Ranges

    jaslake,

    Sorry 'bout that. The Hist file should indeed have all rows together - no blank rows between. I had the separation because I was testing the macro with a different number of rows each run.

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve

    Essentially you need to Resize the Table...this Code does that and appears to do as you require
    Option Explicit
    
    Sub GetData()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim LR As Long, LR1 As Long
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("NALCOMIS_Data")
    
        With ws
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            .Range("A2:O" & LR).ClearContents
            If LR > 2 Then
                With .Range("P3:U" & LR)
                    .ClearContents
                    .Interior.TintAndShade = 0
                    .Interior.Pattern = xlNone
                End With
            End If
        End With
    
        With ws.ListObjects("WIP")
            .Resize Range("$A$1:$U$2")
        End With
        Application.ScreenUpdating = False
        '    ChDir "C:\Documents and Settings\Administrator\Desktop\Steve N"
        ChDir "C:\Users\Steve\Desktop\Test folder"
    
        '    Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Steve N\HIST.xlsx"
        Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\HIST.xlsx"
    
        With Sheets("NALCOMIS_Data")
            LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Range("A2:O" & LR1).Copy
    
            ws.Range("A2").PasteSpecial
        End With
        ActiveWorkbook.Close , False
    
        '    Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Steve N\WIP.xlsx"
        Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\WIP.xlsx"
    
        With Sheets("NALCOMIS_Data")
            LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Range("A2:O" & LR1).Copy
            With ws
                LR = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                ws.Range("A" & LR).PasteSpecial
            End With
        End With
        ActiveWorkbook.Close , False
    
        With ws
            .Cells.EntireColumn.AutoFit
            .Range("A2").Select
        End With
        Application.ScreenUpdating = True
    End Sub

  8. #8
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Excellent Jaslake!. Thank you. Now, when I put this into the master file on another computer with different folder names, should I be changing any of the code to make it work?

  9. #9
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve

    Now, when I put this into the master file on another computer with different folder names, should I be changing any of the code to make it work?
    Probably...if you wish it to work.

  10. #10
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Then I shall.

  11. #11
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve

    Good for you...need help let me know...

  12. #12
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Will do - thank you.

  13. #13
    Forum Contributor Steve N.'s Avatar
    Join Date
    12-22-2011
    Location
    USA
    MS-Off Ver
    Excel 2007, 2010
    Posts
    298

    Re: Record Macro to Copy / Paste Dynamic Ranges

    jaslake,

    I greatly appreciate your work and time on this - thank you again. I'm finally back at work and ready to give this a try. I'm going to need help putting this into the correct location and redirecting it to the applicable folders. I'm very new to VBA/Macros so please type slowly...

    I copied and pasted the code into a module in the VBE of the wb I'm using. When I attempt to run it it stops at ~line 20
    .Resize Range("$A$1:$U$2")
    I have no idea how to procede from here.

    Also, the actual directory for the WIP and HIST files on my work machine is:
    S:\WHDB\FRCNW\020 PC\Morning Production Report\2013 Morning Production Report
    Is this a simple copy paste to replace the existing addresses? I see what looks like two lines per, but one is in green text...?

    Frankly, I'm lost with this stuff...

    Any help, guidance, education is most appreciated and will eventually be put to good use...thank you.

    Steve N.

  14. #14
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Record Macro to Copy / Paste Dynamic Ranges

    Hi Steve

    In this Code I've indicated the Lines of Code I believe need to be changed in order for the Code to run in your environment. ALL of the Code should be in a General Module. I can't test this as I'm not in Your Environment.

    Copy the Code then Alt + F11 to get to VBA --> Insert --> Module --> Paste the Code in the Right Hand Window ---> Save the File
    Option Explicit
    
    Sub GetData()
        Dim wb As Workbook
        Dim ws As Worksheet
        Dim LR As Long, LR1 As Long
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("NALCOMIS_Data")
    
        With ws
            LR = .Range("A" & .Rows.Count).End(xlUp).Row
            .Range("A2:O" & LR).ClearContents
            If LR > 2 Then
                With .Range("P3:U" & LR)
                    .ClearContents
                    .Interior.TintAndShade = 0
                    .Interior.Pattern = xlNone
                End With
            End If
        End With
    
        With ws.ListObjects("WIP")
            .Resize Range("$A$1:$U$2")
        End With
        Application.ScreenUpdating = False
        
        '##### This is the Testing Environment #####
        '    ChDir "C:\Documents and Settings\Administrator\Desktop\Steve N" '<---This is MY Environment for MY Testing
    '    ChDir "C:\Users\Steve\Desktop\Test folder" '<---This is the Environment of your Sample File per the Code
        '##### --------------------------------####
        
        'Based on what you've posted you'll probably need to change the above Testing Environment to your Actual Environment
        ChDir "S:\WHDB\FRCNW\020 PC\Morning Production Report\2013 Morning Production Report"
           
        
        '##### This is the Testing Environment #####
        '    Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Steve N\HIST.xlsx" '<---This is MY Environment for MY Testing
    '    Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\HIST.xlsx" '<---This is the Environment of your Sample File per the Code
        '##### --------------------------------####
        
        'Based on what you've posted you'll probably need to change the above Testing Environment to your Actual Environment
        Workbooks.Open Filename:="S:\WHDB\FRCNW\020 PC\Morning Production Report\2013 Morning Production Report\HIST.xlsx"
        
        With Sheets("NALCOMIS_Data")
            LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Range("A2:O" & LR1).Copy
    
            ws.Range("A2").PasteSpecial
        End With
        ActiveWorkbook.Close , False
    
        '##### This is the Testing Environment #####
        '    Workbooks.Open Filename:="C:\Documents and Settings\Administrator\Desktop\Steve N\WIP.xlsx" '<---This is MY Environment for MY Testing
    '    Workbooks.Open Filename:="C:\Users\Steve\Desktop\Test folder\WIP.xlsx" '<---This is the Environment of your Sample File per the Code
         '##### --------------------------------####
         
         'Based on what you've posted you'll probably need to change the above Testing Environment to your Actual Environment
        Workbooks.Open Filename:="S:\WHDB\FRCNW\020 PC\Morning Production Report\2013 Morning Production Report\WIP.xlsx"
    
        With Sheets("NALCOMIS_Data")
            LR1 = .Cells.Find("*", .Cells(.Rows.Count, .Columns.Count), SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious).Row
            .Range("A2:O" & LR1).Copy
            With ws
                LR = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                ws.Range("A" & LR).PasteSpecial
            End With
        End With
        ActiveWorkbook.Close , False
    
        With ws
            .Cells.EntireColumn.AutoFit
            .Range("A2").Select
        End With
        Application.ScreenUpdating = True
    End Sub
    Regarding this
    I copied and pasted the code into a module in the VBE of the wb I'm using. When I attempt to run it it stops at ~line 20
    Is the the Table Structure of the Workbook your using the SAME STRUCTURE as that of your Sample File Table? If yes then I can't troubleshoot this issue without seeing the actual Workbook you're using.

+ 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. Copy and paste dynamic ranges + transforming into a table
    By tigerallied in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-29-2013, 01:26 PM
  2. Replies: 5
    Last Post: 03-02-2013, 04:22 PM
  3. [SOLVED] Macro to copy and paste Ranges
    By djmarsh51 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-14-2012, 01:58 PM
  4. Copy and paste based on found cells using two spreadsheets with dynamic ranges
    By shawnsonline in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-28-2012, 02:49 PM
  5. How to record a Macro by copy paste from a worksheet
    By pectin232 in forum Excel General
    Replies: 1
    Last Post: 05-05-2009, 04:10 PM

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