+ Reply to Thread
Results 1 to 9 of 9

Copy Range of Cells from One Worksheet to Another Worksheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Copy Range of Cells from One Worksheet to Another Worksheet

    Hello Everyone,

    KEY INFORMATION that will be relevant to the VBA code:

    - I have 4 worksheets in the workbook “PivotTableReport.xlsm” and these are the names of the worksheets:

    1. rptTune (Data in Cells should be PASTED to this worksheet)
    2. rptNoTune (Data in Cells should be COPIED from this worksheet)
    3. AFTER MACRO in rptTune (What “rptTune” worksheet should look like after the Macro is executed)
    4. Data Feeding Pivot Tables (The data feeding worksheets “rptTune” and “rptNoTune”)

    An EXPLANATION of what I am looking to do and I attached a workbook along with post by the name of “PivotTableReport.xlsm”:

    I want to copy cells that contain data from the “rptNoTune” worksheet and paste them on to a specific location on the “rptTune” worksheet . I want to copy all the cells that come AFTER the SUBTOTAL ROW “RT Total” under the PIVOT FIELD “Make” in the “rptNoTune” worksheet and PASTE them AFTER the SUBTOTAL ROW “RT Total” under the PIVOT FIELD “Make” in the “rptTune” worksheet. Since the Pivot Tables can expand and contract with data rows, it is KEY that the cells being copied always come after the SUBTOTAL ROW “RT Total” or the last SUBTOTAL ROW under the PIVOT FIELD “Make” in the “rptNoTune” worksheet.

    The worksheet “AFTER MACRO in rptTune” provides a VISUAL of what the “rptTune” worksheet should look like after the Macro has been executed.

    Note: When copying cells from the “rptNoTune” worksheet, I really don’t care if the cells being copied extend all the way to the last row of the worksheet as oppose to the last row that contains data. Whatever is easier to code is fine by me.

    Thanks in Advance!
    Attached Files Attached Files
    Last edited by boldcode; 08-25-2011 at 04:05 PM. Reason: Solved by tlafferty

  2. #2
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    See attached workbook.
    Attached Files Attached Files
    If your question has been satisfactorily addressed, please consider marking it solved. Click the Thread Tools dropdown and select Mark thread as solved.
    Also, you might want to add to the user's reputation by clicking the star icon in the lower left corner of the post with the answer- it's why we do what we do...

    Thomas Lafferty
    Analyst/Programmer

  3. #3
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    tlafferty,

    It works, but how I can change this piece of code from K to last column in the pivot table. Since the pivot field "Year" and its data labels (e.g. 2010, 2011, etc...) can expand to 2020 or above;

    ("A" & lngFirstRowToAdd & ":K"

    Your Complete Code Just for Reference:
    Sub UpdateReport()
        Dim lngFirstRowToAdd As Long, lngLastRowToAdd As Long, lngLastReportRow As Long
        lngFirstRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).End(xlUp).Offset(1, 0).Row
        lngLastRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).Row
        lngLastReportRow = Sheets("rptTune").Range("A65536").End(xlUp).Offset(1, 0).Row
    
        Sheets("rptNoTune").Range("A" & lngFirstRowToAdd & ":K" & lngLastRowToAdd).Copy
        Sheets("rptTune").Range("A" & lngLastReportRow).PasteSpecial
        Application.CutCopyMode = False
    End Sub

  4. #4
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    Ah...good point. It's better to use R1C1 notation and refer to cells. Then you can use column and row numbers in your references, and refer to them using the cells method of range:
    Sub UpdateReport()
        Dim lngFirstRowToAdd As Long, lngLastRowToAdd As Long, lngLastReportRow As Long, lngLastColumnToAdd As Long
        lngFirstRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).End(xlUp).Offset(1, 0).Row
        lngLastRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).Row
        lngLastColumnToAdd = Sheets("rptNoTune").Range("A10").End(xlToRight).Column
        lngLastReportRow = Sheets("rptTune").Range("A65536").End(xlUp).Offset(1, 0).Row
        Sheets("rptNoTune").Range(Cells(lngFirstRowToAdd, 1), Cells(lngLastRowToAdd, lngLastColumnToAdd)).Copy
        Sheets("rptTune").Range("A" & lngLastReportRow).PasteSpecial
        Application.CutCopyMode = False
    End Sub
    I added the variable lngLastColumnToAdd which will hold the column number, and this line of code was modified from:
    Sheets("rptNoTune").Range("A" & lngFirstRowToAdd & ":K" & lngLastRowToAdd).Copy
    To:
    Sheets("rptNoTune").Range(Cells(lngFirstRowToAdd, 1), Cells(lngLastRowToAdd, lngLastColumnToAdd)).Copy

  5. #5
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    tlafferty,

    It doesn't work, the procedure stops at the code that was modified:

    Sheets("rptNoTune").Range(Cells(lngFirstRowToAdd, 1), Cells(lngLastRowToAdd, lngLastColumnToAdd)).Copy

  6. #6
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    Weird. I ran into the same thing, but noticed if I save, close and reopen the book, the code runs, and I haven't a clue why it doesn't generate the error every time. I may want to look at a range object instead of a reference. Let me see about tweaking the code.

  7. #7
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    tlafferty,

    OK, I appreciate your help.

  8. #8
    Forum Contributor
    Join Date
    04-20-2011
    Location
    New York, New York
    MS-Off Ver
    Excel 2010
    Posts
    160

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    tlafferty,

    Thanks for all your help, I figured it out. Here is what the code is suppose to look like:


    Sub UpdateReport()
        Dim lngFirstRowToAdd As Long, lngLastRowToAdd As Long, lngLastReportRow As Long, lngLastColumnToAdd As Long
        
        lngFirstRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).End(xlUp).Offset(1, 0).Row
        lngLastRowToAdd = Sheets("rptNoTune").Range("A65536").End(xlUp).Row
        lngLastColumnToAdd = Sheets("rptNoTune").Range("A10").End(xlToRight).Column
        lngLastReportRow = Sheets("rptTune").Range("A65536").End(xlUp).Offset(1, 0).Row
        
    With Sheets("rptNoTune")
        .Range(.Cells(lngFirstRowToAdd, 1), .Cells(lngLastRowToAdd, lngLastColumnToAdd)).Copy
    End With
    
    With Sheets("rptTune")
        .Range("A" & lngLastReportRow).PasteSpecial
        Application.CutCopyMode = False
    End With
    End Sub

  9. #9
    Valued Forum Contributor tlafferty's Avatar
    Join Date
    04-08-2011
    Location
    United States, Tacoma, WA
    MS-Off Ver
    Excel 2010, Excel 2013 Customer Preview
    Posts
    1,112

    Re: Copy Range of Cells from One Worksheet to Another Worksheet

    Hmmmm....that's cool! I'll have to remember the With|End with construction next time I run into this.

+ 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