+ Reply to Thread
Results 1 to 11 of 11

Change range of cells within VBA macro and copy and paste to fixed cell range

  1. #1
    Registered User
    Join Date
    07-18-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Change range of cells within VBA macro and copy and paste to fixed cell range

    I have to following code:

    Please Login or Register  to view this content.
    How can I accomplish this?
    Last edited by Cutter; 07-20-2012 at 11:44 AM. Reason: Added code tags

  2. #2
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    why are you using a loop here?

    you can accomplish the same with this

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-18-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    I do not want to copy the range C41:F57 as a chunk. I want to copy as chunks of 4 cells a row at a time. That is copy C40:F40 and paste it to C26:F26. Then run some calculation on C26:F26 (unrelated to this discussion). Then I want to repeat this sequence on C41:F41 copy and paste to C26:F26. Then do the same for C42:F42 upto C57:F57.

    So I basically need a loop to copy and paste 4 cells each and go down to the next row and repeat it 17 times.

  4. #4
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    See if this works for you


    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    07-18-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    fredlo2010, This is great. It worked.

    What if I need to just copy 1 cell (NOT a range) with the cell being a variable (just like you did it - say from cells(i, 7) to cells(i, 8) ).

    But the copy from cells(i, 7) needs to be a paste special to cells(i, 8)

    I tried this:

    Please Login or Register  to view this content.
    Here I am trying to copy from B34 and B35 to G47 and H47 and repeat the same through rows 48 to 57.
    This code does not work. It copies B34,B35 through range G47:H57?

    Also I need this to be paste special.

    Thanks
    Last edited by Cutter; 07-20-2012 at 11:44 AM. Reason: Added code tags

  6. #6
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    I am not following you man.

    Can you give me a sample of your workbook. Also you can go through the code by pressing "F8" it will go lineby line, so you can see what the code is doing.

  7. #7
    Registered User
    Join Date
    07-18-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    I have a range of data C40:F57.
    I need to copy C40:F40 to C26:F26, run some calculations and copy cell B34 and B35 and paste special to G40:H40.

    Your code above let me copy C40:F40 to C26:F26 and then repeat the copy steps for C41:F57. How do I accomplish:

    Copy cell B34 and B35 and paste special (values) to G40:H40. Note this needs to be a loop too same loop where paste special needs to go to the range
    G40:H57. First paste special will be to G40:H40, then to G41:H41 and go to G57:H57.

    Code I have so far is:


    Please Login or Register  to view this content.
    Last edited by Cutter; 07-20-2012 at 11:45 AM. Reason: Added code tags

  8. #8
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    You are resizing when there is no need to. You are copying only one cell. Ex

    If you want to copy range "A1:B2" then you pic the start and resize it. Range("A1").resize(0,2) which is a little ambiguous.

    So here is the code take a look and tell me if it works. I modified it to match the Values only.

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-18-2012
    Location
    NY
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    fredlo2010

    Code:

    Please Login or Register  to view this content.
    Didnt work as expected. It not only copies B34 and B35 to G40 and H40 but goes through G40:H57 and copies to the whole range.
    Not sure how the loop from 40 to 57 interferes here. Every time it goes through the loop, B34:35 are overwritten through out the range
    G40:H57 and NOT just its respective row cells.

    Thanks
    Last edited by Cutter; 07-20-2012 at 11:45 AM. Reason: Added code tags

  10. #10
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    @ Mannyny

    Welcome to the forum.

    Please notice that code tags have been added to your post(s). The forum rules (Rule #3) require them so please keep that in mind and add them yourself whenever showing code in any of your future posts.
    Thanks.

  11. #11
    Valued Forum Contributor fredlo2010's Avatar
    Join Date
    07-04-2012
    Location
    Miami, United States
    MS-Off Ver
    Excel 365
    Posts
    762

    Re: Change range of cells within VBA macro and copy and paste to fixed cell range

    Quote Originally Posted by Mannyny View Post
    fredlo2010

    Code:

    Please Login or Register  to view this content.
    Didnt work as expected. It not only copies B34 and B35 to G40 and H40 but goes through G40:H57 and copies to the whole range.
    Not sure how the loop from 40 to 57 interferes here. Every time it goes through the loop, B34:35 are overwritten through out the range
    G40:H57 and NOT just its respective row cells.

    Thanks
    I am totally lost now. The code works according to what your explanation says.Maybe we are not understanding each other (it happens sometimes) Can you provide a copy of your workbook with a before and after sheet?

    Thanks

+ 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