+ Reply to Thread
Results 1 to 5 of 5

Limit number of rows pasted with pastespecial

Hybrid View

  1. #1
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Limit number of rows pasted with pastespecial

    Hello - I have one spreadsheet (1000 rows, 30 columns) where users filter down the data until they have the set of students_ids (Column A) that they want to work with.

    Then they click a button which runs the code below to copy the student_id's from the current spreadsheet to another spreadsheet (Test Transfer Data.xls).

    What I would like to do is modify the code to only paste the first 10 id's. So the maximum it will ever paste is 10 ids (even if they filter down to 100 rows) Similar to a select top 15.......

    Thank in advance!!

    Sub CopyData()
    Dim DestBook As Workbook, SrcBook As Workbook
    Application.ScreenUpdating = False
    Set SrcBook = ThisWorkbook
    On Error Resume Next
    Set DestBook = Workbooks("Test Transfer Data.xls")
    If DestBook Is Nothing Then
        Set DestBook = Workbooks.Open("C:\Test Transfer Data.xls")
        If Err.Number = 1004 Then 
            Set DestBook = Workbooks.Add
            SrcBook.Worksheets("Test_From").Range("a5:a1000").Copy
            DestBook.Worksheets("Test_To").Range("A1").PasteSpecial
            Application.CutCopyMode = False
            DestBook.SaveAs ("C:\Test Transfer Data.xls")
            DestBook.Close
        Else
            SrcBook.Worksheets("Test_From").Range("a5:a").Copy
            DestBook.Worksheets("Test_To").Range("A32").PasteSpecial
            Application.CutCopyMode = False
            DestBook.Save
            DestBook.Close
        End If
    Else
           SrcBook.Worksheets("Test_From").Range("a5:a405").Copy
            DestBook.Worksheets("Test_To").Range("A32").PasteSpecial
            Application.CutCopyMode = False
    End If
    On Error GoTo 0
    Set DestBook = Nothing
    Set SrcBook = Nothing
    End Sub

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Limit number of rows pasted with pastespecial

    I may be missing something but if your range always start at A5 and you only want 10 values copied why dont you just set the range A5:A15? Maybe I am missing something though.

  3. #3
    Registered User
    Join Date
    11-03-2010
    Location
    Detroit, MI
    MS-Off Ver
    Excel 2010
    Posts
    65

    Re: Limit number of rows pasted with pastespecial

    Sorry for the confusion.
    The sheet is filtered so the original sheet contains 1000 rows but is filtered down to 20 rows (the other 1980 are hidden).

    The paste correctly only pastes the 20 visible rows but I want to only select the first 15.

    So I have to list A1:A1000 (because the filter might be looking at rows 10, 15, 150, 755, 756, 950, etc.) but cannot figure out how to limit the results to 10 rows.

  4. #4
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,486

    Re: Limit number of rows pasted with pastespecial

    There is a way to loop through filtered data, could you supply a sample workbook.......

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Limit number of rows pasted with pastespecial

    I think this should work for you. The i used SpecialCells to select all the filtered values ie all the visible ones. I was thinking that you could just remove them after they are copied over to the new sheet. Depending on how the new sheet is set up this may cause some conflicts but seeing as how you wrote the macro initially i am sure you can adjust tailor it to fit your needs.

    Option Explicit
    
    Sub CopyData()
    Dim DestBook As Workbook, SrcBook As Workbook
    Dim LR As Long, LR2 As Long
    Application.ScreenUpdating = False
    Set SrcBook = ThisWorkbook
    On Error Resume Next
    Set DestBook = Workbooks("Test Transfer Data.xls")
    If DestBook Is Nothing Then
        Set DestBook = Workbooks.Open("C:\Test Transfer Data.xls")
        If Err.Number = 1004 Then
            Set DestBook = Workbooks.Add
            SrcBook.Worksheets("Test_From").Range("a5:a1000").Copy
            DestBook.Worksheets("Test_To").Range("A1").PasteSpecial
            Application.CutCopyMode = False
            DestBook.SaveAs ("C:\Test Transfer Data.xls")
            DestBook.Close
        Else
            LR = SrcBook.Worksheets("Test_From").Range("A" & Rows.Count).End(xlUp).Row
            SrcBook.Worksheets("Test_From").Range("a5:a" & LR).SpecialCells(xlCellTypeVisible).Copy
            DestBook.Worksheets("Test_To").Range("A32").PasteSpecial
            LR2 = DestBook.Worksheets("Test_To").Range("A" & Rows.Count).End(xlUp).Row
            DestBook.Worksheets("Test_To").Range("A43:A" & LR2).ClearContents
            Application.CutCopyMode = False
            DestBook.Save
            DestBook.Close
        End If
    Else
            LR = SrcBook.Worksheets("Test_From").Range("A" & Rows.Count).End(xlUp).Row
            SrcBook.Worksheets("Test_From").Range("a5:a" & LR).SpecialCells(xlCellTypeVisible).Copy
            DestBook.Worksheets("Test_To").Range("A32").PasteSpecial
            LR2 = DestBook.Worksheets("Test_To").Range("A" & Rows.Count).End(xlUp).Row
            DestBook.Worksheets("Test_To").Range("A43:A" & LR2).ClearContents
            Application.CutCopyMode = False
    End If
    On Error GoTo 0
    Set DestBook = Nothing
    Set SrcBook = Nothing
    End Sub

+ 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