+ Reply to Thread
Results 1 to 6 of 6

Copy/Paste in Row Before and After Cell

  1. #1
    Registered User
    Join Date
    08-08-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    14

    Copy/Paste in Row Before and After Cell

    The image below will serve as the basis for what I am trying to achieve. Using a macro, within a row (selected by the user) I would like to locate a cell with a value and paste that value in the cell before and the cell after it. For example, I would like to have "1" copied into A1 and C1, "2" copied into D1 and F1, etc. I have used the following code to place the value in the preceding cell, but it breaks down when I try to apply it to the subsequent cell.


    A B C D E F G H I
    1 2 3


    HTML Code: 
    Attached Images Attached Images
    Last edited by nicotob; 05-05-2014 at 07:01 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,720

    Re: Copy/Paste in Row Before and After Cell

    Your code doesn't have to literally copy & paste, you can just assign the value.

    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    08-08-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copy/Paste in Row Before and After Cell

    The code that you provided will result in 1's being assigned to every cell in the range due to the fact that we have assigned a value to a cell in front of the "for loop." It is this precise problem that I am trying to overcome.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,720

    Re: Copy/Paste in Row Before and After Cell

    Good point. I focused on the fact that you did not have code to assign the cell to the right. I hadn't tested it (always helps if you include your workbook for testing purposes).

    How about this:

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-08-2012
    Location
    Texas
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: Copy/Paste in Row Before and After Cell

    Thank you very much. That does it. I was hoping you might be able to help me understand ho the Selection.Special Cells(xlCellTypeConstants) achieved the desired result. Why does this additional code cause the for loop to miss the cells with values placed in cells ahead of the loop?

    Thanks again for your help.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,720

    Re: Copy/Paste in Row Before and After Cell

    The attribute SpecialCells(xlCellTypeConstants) returns a range of cells within Selection containing only constants (i.e., not empty and not formulas). This attribute is evaluated one time at the beginning of the For loop, so will be not be reevaluated as you fill in additional cells in the Selection range--it's locked in when you start.

+ 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: 07-27-2013, 09:02 AM
  2. Replies: 8
    Last Post: 07-08-2013, 06:03 AM
  3. [SOLVED] Copy cell A1 and paste value into B1 if B1 has content, paste to B2...etc
    By mhopke in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-09-2012, 09:39 PM
  4. How to select cell C1, copy paste then C2, copy paste then C3 etc
    By s45yth in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-07-2011, 07:15 AM
  5. Copy and Paste macro needs to paste to a changing cell reference
    By loulou in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-24-2005, 07:06 AM

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