+ Reply to Thread
Results 1 to 11 of 11

Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Values

Hybrid View

  1. #1
    Registered User
    Join Date
    03-26-2013
    Location
    VA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Values

    This seems like it should be easy, but I am having a brain freeze;

    I'm trying to make a macro that copy and pastes certain values to one particular cell. However I want the macro to function that whenever I run it it will select the row directly underneath the last row.

    I.e., I want to run the macro and it to copy cell A1 and place the info into cell B1, then for a second time I would like to push the button and for it to copy cell A2 and paste it in cell B1, then for a third time I want to push the button and it will copy cell A3 and paste into cell B1 and so on. Once it hits my last dedicated cell (e.g., A5) I would like for it to start over at A1, each time replacing cell B1 with the copied info from those cells. Thanks in advance..

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Try this:

    Option Explicit
    
    Dim r&
    
    Sub test()
    
      r = r + 1
    
      If r = 6 Then r = 1
    
      Cells(1, 2) = Cells(r, 1)
    
    End Sub
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    03-26-2013
    Location
    VA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Thanks for the reponse Abousetta, it worked great. Quick follow up question, if I needed to integrate that into;

    Sub Macro2()

    Range("B13").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-10]C[6]"
    Range("B24").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-21]C[1]"
    Range("C24").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-21]C[-1]"
    Range("B26").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-23]C[-1]"
    Range("B31").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-28]C[2]"
    Range("B33").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-30]C[3]"
    Range("B35").Select
    ActiveCell.FormulaR1C1 = "='Data Template'!R[-32]C[5]"
    Range("B36").Select
    End Sub

    Would I just replace ActiveCell.FormulaR1C1 with the above?

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Please add code tags (#) like this:

    Sub Macro2()
        Range("B13").Formula = "='Data Template'!R[-10]C[6]"
        Range("B24").Formula = "='Data Template'!R[-21]C[1]"
        Range("C24").Formula = "='Data Template'!R[-21]C[-1]"
        Range("B26").Formula = "='Data Template'!R[-23]C[-1]"
        Range("B31").Formula = "='Data Template'!R[-28]C[2]"
        Range("B33").Formula = "='Data Template'!R[-30]C[3]"
        Range("B35").Formula = "='Data Template'!R[-32]C[5]"
        Range("B36").Select
    End Sub
    Now it's not clear what you want to do? Each time you click copy the contents to B1 (e.g. value in B13 then B24, etc)?

  5. #5
    Registered User
    Join Date
    03-26-2013
    Location
    VA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Sorry for the formatting/confusion; each Range selection represents a fixed cell. For each one, I want it to copy and paste from another worksheet (Data Template) to the same cell, but again each copy and paste would shift down to the cell below...

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Sorry, still not completely understanding what you are after.

    So the code above works because it brings the data from the other workbook to the current one. Then do you want to be able to cycle through those ranges as follows:

    Option Explicit
    
    Dim rng
    
    Sub Macro2()
    
      Dim Rng, myRng
    
        Range("B13").Formula = "='Data Template'!R[-10]C[6]"
        Range("B24").Formula = "='Data Template'!R[-21]C[1]"
        Range("C24").Formula = "='Data Template'!R[-21]C[-1]"
        Range("B26").Formula = "='Data Template'!R[-23]C[-1]"
        Range("B31").Formula = "='Data Template'!R[-28]C[2]"
        Range("B33").Formula = "='Data Template'!R[-30]C[3]"
        Range("B35").Formula = "='Data Template'!R[-32]C[5]"
     
        myRng = Array("B13", "B24", "C24", "B26", "C31", "C33", "C35")
    
        If rng = 7 or rng = vbnullstring then rng = 0
    
        Range("B1").value = Range(myRng(rng)).value
    
        rng = rng + 1
    
    End Sub
    Last edited by abousetta; 09-22-2014 at 03:30 PM. Reason: Changed approach

  7. #7
    Registered User
    Join Date
    03-26-2013
    Location
    VA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    So B13 from the Main Sheet is suppose to copy and paste from H3 of the Data Template worksheet. Here's where your code comes in, each time you run the macro it is suppose to copy and paste from the subsequent column down, i.e., first time you run the macro, it copies and pastes from H3, second time H4, third time H5, etc.

    This is the same logic for all the Ranged Cells in the code. Albeit they have difference beginning source columns (i.e., B24 pulls from C3 from the Data Template worksheet).

  8. #8
    Registered User
    Join Date
    03-26-2013
    Location
    VA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Anybody besides Abousetta?

  9. #9
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Hi,

    Try this:

    Option Explicit
    
    Dim r&
    
    Sub test()
    
      If r = 5 Then r = 0
    
        Range("B13").Formula = "='Data Template'!R[" & -10 + r & "]C[6]"
        Range("B24").Formula = "='Data Template'!R[-21]C[1]"
        Range("C24").Formula = "='Data Template'!R[-21]C[-1]"
        Range("B26").Formula = "='Data Template'!R[-23]C[-1]"
        Range("B31").Formula = "='Data Template'!R[-28]C[2]"
        Range("B33").Formula = "='Data Template'!R[-30]C[3]"
        Range("B35").Formula = "='Data Template'!R[-32]C[5]"
        
      r = r + 1
    
    End Sub

  10. #10
    Registered User
    Join Date
    03-26-2013
    Location
    VA
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    Worked like a charm, thanks Abou!! I can follow it pretty well based on how clearly its written, don't mean to be bothersome, but can you give a quick explanation behind logic (specifically the use of r)? Thanks a lot!!

  11. #11
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Copy & Paste Values to Same Cell, While Selecting Different Cell for Copy and Paste Va

    It's really simple actually....

    r is a number from 0 to 5. The first time you open the workbook and run the code it is automatically given a value of zero. Since r is put outside the 'sub' it will keep the last value even when the sub is complete. Therefore each time you run the sub it will begin from the last value that was saved.

    Now as for the r being used in the formula, all it does is offset the first cell by r cells (e.g. 0, 1, 2...). Therefore the first time you run it the formula is -10 + 0... second time is -10 + 1... and so on.

    Hope this helps.

    abousetta

    P.S. If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. Selecting sheets to copy/paste values to a new workbook
    By gharding14 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-13-2013, 11:10 AM
  2. [SOLVED] Copy down and paste cell values from another cells
    By Rocky2013 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-16-2013, 06:53 AM
  3. Replies: 8
    Last Post: 07-08-2013, 06:03 AM
  4. Replies: 0
    Last Post: 09-06-2012, 04:06 AM
  5. Copy & Paste one or more cell values to
    By scott5 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-26-2008, 05:35 PM

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.6.0 RC 1