+ Reply to Thread
Results 1 to 20 of 20

Copy range and paste into non contiguous range

  1. #1
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Copy range and paste into non contiguous range

    Hi all,

    I am trying to copy a range from one row and paste those values into a non contiguous range. To clarify, I want to do the exact opposite of the code I wrote below where I copy data from a non contiguous cell range and paste on another sheet in one row. I just started with VBA this week and I have no idea how to do it.

    This code works like a charm:

    Please Login or Register  to view this content.


    Now I want to do the opposite and paste a certain range from the SavedSpecs tab into the range Worksheets("Cover").Range("D3:D6, H3:H6, K3:K5, D8:D10, H8:H10, K8:K9, D13:F14").


    I tried the code below but that gives a runtime error 1004. Alternatively I can copy the values for each cell at the time but I quickly realized that will be way too slow.


    Please Login or Register  to view this content.


    I hope someone can push me in the right direction.
    Last edited by amros; 07-01-2017 at 02:20 AM.

  2. #2
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Hi amros

    Did not use your code....Try this.
    Please Login or Register  to view this content.
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    Thank you so much! This is exactly what I wanted.
    I am a little bit confused about the loop in the loop though. Would you mind explaining me in short why two loops are needed? It would be great to understand this solution a bit better.

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    I am sure there is a simpler method but just can't wrap my head around it...
    Loop 1 - loops through the different Ranges in Array
    Loop 2 - loops through each cell in the Range

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Copy range and paste into non contiguous range

    Single loop is enough if array.
    Please Login or Register  to view this content.

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Well there you have it...the Master has spoken....Thanks for the lesson jindon

    Edited:
    @ jindon....Please explain
    Please Login or Register  to view this content.
    Edited2:
    Nevermind...got it

    Edited3:
    Nope not
    Last edited by sintek; 07-01-2017 at 05:35 AM.

  7. #7
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    Thanks a lot, shorter code but seems a little slower than Sintek's. The range that I need to copy the values too will be expanded to roughly 300 cells.
    I just tested with 50 values and that takes roughly 7 seconds.

    Is there any way to optimize the code for speed?

    -- Edit --

    Saving the spec, so doing the opposite is very fast. Is there a way to give the copy command and paste all values at the same time? I think there must be way to make it faster?
    Last edited by amros; 07-01-2017 at 05:48 AM.

  8. #8
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Add this
    Please Login or Register  to view this content.
    before and set to true after

  9. #9
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Hereby amended as per jindon suggestion...
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    Yep, that made a big difference. I will keep this thread open until I have tested this with all the values I need to copy. It might be a little slow when I'm finished so maybe I will ask for more help to optimize the code.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Copy range and paste into non contiguous range

    300 cells for 7 seconds.

    I don't believe it is from the code.
    This should take much less than a second.
    Please Login or Register  to view this content.

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Happy to keep contributing and thereby learning. Thanks for rep + added

  13. #13
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    Hi all,

    I am trying to copy all the values but it looks like it won't let me add more values to the range (saveSpecRng) than what I have in the code below. When I add more, it results in a runtime 1004 error. Now there are about 135 cells. Is there any limit or can it have another reason? Could it be the array? I guess I could create a second range and array to paste copy paste in parts but I'd look to see if there are better options.



    Please Login or Register  to view this content.

  14. #14
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Think it is time for a upload of your workbook....or sample thereof...
    Originally Posted by Pete_UK
    To attach a file, click on Go Advanced (below the Edit Window) while you are composing a reply, then scroll down to and click on Manage Attachments and the Upload window will open. Click on Browse and navigate to (and double-click) the file icon that you want to attach, then click on Upload and then on Close this Window to return to the Edit window. When you have finished composing your post, click on Submit Post.

  15. #15
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    File is destroyed a bit as content is sensitive.
    What I want here is to 'Save' (Copy) data from Cover sheet, all the grey cells, to the SavedSpecs sheet. When this is done properly I want to continue by having the options to load (Copy) specifications from SavedSpecs onto the Cover sheet.
    Attached Files Attached Files
    Last edited by amros; 07-02-2017 at 04:07 AM.

  16. #16
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Nope invalid attachment...

  17. #17
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Your code works without errors

  18. #18
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    Yes, this one works:
    Please Login or Register  to view this content.
    But it doesn't work when I add more values such as H57:64 to this range. For example:

    Please Login or Register  to view this content.
    I tried adding a single cells as well but it looks like there is some kind of maximum. I don't understand what prompts the runtime 1004 error

  19. #19
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: Copy range and paste into non contiguous range

    Hi amos

    Use this...removed spaces between ","
    Please Login or Register  to view this content.

    Please Login or Register  to view this content.
    Last edited by sintek; 07-02-2017 at 05:38 AM.

  20. #20
    Registered User
    Join Date
    12-12-2013
    Location
    China
    MS-Off Ver
    Excel 2010
    Posts
    40

    Re: Copy range and paste into non contiguous range

    That works, thanks! The only difference seems to be that no spaces are used? I am going to experiment with some different ranges for copy paste. With this range I am only about halfway and would be hard to debug in the future.

    Thanks again for your help.

+ 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: 6
    Last Post: 01-21-2016, 01:25 PM
  2. [SOLVED] Help! - Copy Range (Sheet1,A10:F50), Paste Range (Sheet 2,A1:F41), Offset Range & Repeat
    By cjtimmer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2015, 06:27 PM
  3. Select & copy non-contiguous range
    By SunRise in forum Excel General
    Replies: 0
    Last Post: 01-08-2013, 05:56 PM
  4. Copy and Paste an array (contiguous & non contiguous ranges)
    By Xrull in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2010, 09:17 AM
  5. Copy and Paste Non-contiguous Range
    By Rashed.R in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 02-08-2010, 07:56 AM
  6. Excel copy formulas using non contiguous range
    By Paul Moles in forum Excel General
    Replies: 4
    Last Post: 03-11-2006, 07:10 PM
  7. copy formulas from a contiguous range to a safe place and copy them back later
    By Lucas Budlong in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2006, 04:35 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