+ Reply to Thread
Results 1 to 6 of 6

Paste a range within a range

  1. #1
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Paste a range within a range

    I am trying to paste part of the first row of cells in a range into a worksheet. The code below keeps giving me an error.
    The ws is active, although I am not sure if it needs to be in this case. rngFull was defined earlier in the code as the set of cells a user had selected. stCol is a number. I want the first row, up to stCol (which would be several cells) to be pasted starting at A1 in the ws.
    Please Login or Register  to view this content.
    I was thinking that maybe Excel didn't like me pasting several cells that way so I also tried this code and got the same error.
    Please Login or Register  to view this content.
    I'm trying to avoid using the clipboard via copy and paste. I know this should be easy. What am I missing?
    Some people volunteer in soup kitchens or hospitals. I choose to make the world better by trying to help you with Excel. We're all learning.

    <---Click * Add Reputation for all helpful comments. It's like giving a smile.
    Forum Rules: How to mark your post [Solved] and have a happier, Excel enriched life.

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

    Re: Paste a range within a range

    What is rngFull and stCol?

    Depending on answer, maybe this might help

    Please Login or Register  to view this content.
    Last edited by stnkynts; 08-26-2015 at 12:25 PM.
    If you are happy with my response please click the * in the lower left of my post.

  3. #3
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Paste a range within a range

    rngFull is a range of cells that usually has about 1,000 rows and 20 columns. stCol-1 is the last column from rngFull that I want in the header row on ws.

    So if stCol is 5, then I want A1 to A4 from rngFull as the values to be pasted into ws.

    I don't usually use resize, but using it this way still gives me an error.
    Please Login or Register  to view this content.
    Last edited by nigelbloomy; 08-26-2015 at 12:39 PM.

  4. #4
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Paste a range within a range

    It looks like my problem is in trying to select the first few cells of the first row in the range. I created rngHeader as a range to just pull out the section I need from rngFull. This also gives me an error.
    Please Login or Register  to view this content.
    This gives me a range with the values I want, but it doesn't allow me to use the variable stCol to define how many columns to include.
    Please Login or Register  to view this content.
    I can also do this and get what I want, but I don't always want the first several columns, sometimes I need the last columns.
    Please Login or Register  to view this content.
    Anyone know why I can use range with the quotes, but when I try to use cell() to get the range it errors out?

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

    Re: Paste a range within a range

    Don't know why it is not working for you; seems to function fine for me

    Please Login or Register  to view this content.
    Note:
    So if stCol is 5, then I want A1 to A4 from rngFull as the values to be pasted into ws.
    I think you mean A1 to D1.
    Last edited by stnkynts; 08-26-2015 at 02:57 PM.

  6. #6
    Valued Forum Contributor nigelbloomy's Avatar
    Join Date
    11-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    1,011

    Re: Paste a range within a range

    You are right. I did mean A1 to D1. When looking at these lines of code, aren't you just calling ws1 again? I was trying to create a range so that I wouldn't have to have the code go back to ws1 over and over again. It looks like in the second line rngHeader is being set by the values in ws1 and not rngFull.
    Please Login or Register  to view this content.
    Your answer and debugging did give me an idea though that I think works.
    Please Login or Register  to view this content.
    I think part of the problem was that cells() was pulling the value from that cell instead of a reference to it. The .address seems to fix that and ends up giving me the same as if I had just used Range("A1:D1").

+ 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. Loop through files in folder, copy range, Paste range to original workbook
    By knevil in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 03-09-2016, 05:33 PM
  2. [SOLVED] Add IF Statement formula to cells in varying range and paste to used range
    By Webbers in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-26-2015, 01:28 PM
  3. [SOLVED] Inputboxes to select range to copy and range to paste from different workbooks
    By graphicgoose in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-29-2014, 05:51 PM
  4. [SOLVED] Copy and paste range as values to specific sheet/range
    By lukestkd in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-06-2014, 09:48 PM
  5. Replies: 4
    Last Post: 03-17-2013, 05:58 AM
  6. Change range of cells within VBA macro and copy and paste to fixed cell range
    By Mannyny in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-20-2012, 11:51 AM
  7. Copy dynamic range from previous sheet and paste after last used row in a range
    By Mistweaver in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-04-2010, 06:24 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