+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    [SOLVED] Select Multiple Rows But Only Copy Specified Columns

    Okay, so I am trying to copy and paste data from one spreadsheet to another but it isn't as simple as it sounds...

    The spreadsheet I am copying from will always be a new sheet; however, it will always have the same number of columns. The sheet I am pasting into is a template, and it will always be the same.

    What I need to do is be able to copy entire rows of data from the first sheet and paste them into the second, but I don't need any information pasted after column BQ.

    What I have been doing thus far is simply scrolling to column BQ and then down to last row and selecting it, then dragging up to the top to have selected only what is necessary. I then copy it and paste it into the first cell of my template, and it works perfectly; however, I was hoping for a quicker way of doing this.

    I know I could use a macro to have it automatically detect the last row of data in column BQ and copy from there up, but there are too many issues that could happen if I do it that way because I may not always want to copy every single row. I want to have the power to just select the rows I want, then copy and simply paste them into the template without screwing up the formulas I have in place after column BQ.

    I was hoping that there was either a formula that I could insert after column BQ in my template that might tell the paste to stop or a macro that would stop my pasting at column BQ.

    Any ideas?

    Thanks in advance!!

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Cochrane,Alberta
    MS-Off Ver
    XL 2003,2007
    Posts
    6,259

    re: Select Multiple Rows But Only Copy Specified Columns

    This code will let you select the rows to copy, but will only copy the columns(A:BQ) in your selection. So Select the rows to copy then run the macro, it will then paste to A1 in Sheet2 .

    You can select many rows at once, but unfortunately I could not get it to work if you select different rows at once, ie: if Rows 1,3,6,9 are selected. It only works if the rows are together.

    Hopefully it will work for your Excel Version.

    Code:
    Sub CopyRows()
    
        Dim ws As Worksheet, r As Range
    
        Set ws = Worksheets("Sheet2")
        Set r = ws.Range("A1")
    
        Selection.Columns("A:BQ").Copy Destination:=r
    
    End Sub
    Dave


  3. #3
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    re: Select Multiple Rows But Only Copy Specified Columns

    Thanks, but not being able to pull rows that aren't next to each other is one of the reasons why the macro I had suggested would cause problems, so that isn't really time saving because then I'd have to move all of the data that I want to be next to each other and no amount of sorting will do that for me perfectly every time.

    Any other ideas?

  4. #4
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    re: Select Multiple Rows But Only Copy Specified Columns

    Bump no response

  5. #5
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    re: Select Multiple Rows But Only Copy Specified Columns

    hi,

    The below modified version of Dave's code (or perhaps the code in the attached file) may work for you...
    Code:
    Option Explicit
    Sub Modified_CopyRows()
    Dim ws As Worksheet
    Dim FirstBlankRw As Range
    Dim AreaInSlctn As Range
    Dim Slctn As Range
    application.screenupdating = false
        Set ws = Worksheets("Sheet2")
        Set Slctn = Selection
        For Each AreaInSlctn In Slctn.Areas
            With ws
                Set FirstBlankRw = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0)
            End With
            AreaInSlctn.Columns("A:BQ").Copy Destination:=FirstBlankRw
        Next AreaInSlctn
    'release memory
    set Slctn = Nothing
    set FirstBlankRw =Nothing
    set ws = nothing
    application.screenupdating = true
    End Sub
    Dave,
    I learnt this approach of identifying the separate areas from a file of John Walkenbach's called "copymult.xls" which I downloaded a few years ago from his site (? I can't see it on his "new" site layout) & have attached this for interest.
    Note: Charles William's site discusses an error in the way Excel handles multiple area ranges when processing UDF's - not relevant for the OP in this case, but it could possibly become an issue if someone tried to modify the above concept for use in a UDF.

    Others, if using John's code or the attached file, please identify the source.

    hth
    Rob
    Attached Files Attached Files
    Last edited by broro183; 02-16-2010 at 02:31 PM. Reason: E1: adding "set... = Nothing" E2: added screenupdating lines
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  6. #6
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    re: Select Multiple Rows But Only Copy Specified Columns

    Thanks broro183!

    Could this still work if I don't want to actually include the paste destination within this part of the code? At this point in my macro, I need it to only copy and then I have a macro that will open the workbook I need it pasted in. I cannot open the workbook first and then run this code because the workbook always has a new name, so reactivating it would be difficult.

    Anyway, I can take the part that says
    Code:
    Destination:=FirstBlankRw

    out but is there anything else I can take out of it too?

  7. #7
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Select Multiple Rows But Only Copy Specified Columns

    hi,

    Hmmm,
    Not as it's written. The code in my post (not the file) goes through each "contiguous area" (AreaInSlctn) one at a time & pastes each one, so if you were to remove the "Destination:=" argument & then try to have it paste to another file later, it is likely to only copy the last AreaInSlctn into your selected file. The reasons I prefer to use the "Destination:=" argument is that it:
    - completes the action in one go, which
    - minimises errors of the Clipboard being cleared before a Paste occurring (eg by pressing [Esc] or changing the Clipboard when working on another application, such as email, while the macro runs etc) &
    - prevents the need for
    Code:
    application.cutcopymode = false

    Is there a standard naming convention for the file names?
    If so, we could incorporate your other code & use logic to paste into the correct file.
    Can you please expand on the facts to give an overall explanation of what your collection of macros do?

    If you really want to keep the two actions of Copying & Pasting separate, we need to maintain/make an array of all the individual areas in your selection which is likely to be easy based on the code in John W's file - but it may take me a while to wrap my head around it...

    Let us know which approach you'd prefer

    Rob
    Last edited by broro183; 02-16-2010 at 02:34 PM.
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  8. #8
    Forum Contributor amyxkatexx's Avatar
    Join Date
    11-23-2009
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2004 for Mac
    Posts
    128

    Re: Select Multiple Rows But Only Copy Specified Columns

    Okay, so since your last response, I've found that there really isn't much need for me to be able to do this. I can think of only a few times when this might be handy but I've already found a workaround for that.

    Thank you so much for your help though and hopefully this will help someone else out in the future.

    :]

    -Amy Kate

  9. #9
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: Select Multiple Rows But Only Copy Specified Columns

    hi Amy Kate,

    Thanks for the feedback & also for adding to my reputation

    If you are happy with the solution can you please mark the thread as Solved?
    (see Forum Rules for instructions)

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

Thread Information

Users Browsing this Thread

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

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.2.0