Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12

Thread: Copy & Pasting cells using a number in a cell

  1. #1

    Copy & Pasting cells using a number in a cell



    Register for a FREE account, and/
    or Log in to avoid these ads!

    Evening all,

    I would like to copy a range of cells, lets say A1:B10 and then copy this range of cells a defined number of times into different locations each time.

    The "number of times" will be determined by a number in a cell, lets say D1.

    Location of the copied cells can be A20, D20 & G20. (3 locations so therfore the number in cell D1 will be 3)

    Can anyone help me with this?

    Many Thanks
    Nick

  2. #2
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,356
    Articles
    0
    Excel Version
    365
    try:
    Code:
    Sub blah()
    For i = 1 To Range("D1").Value
      Range("A1:B10").Copy Cells(20, (i - 1) * 3 + 1)
    Next i
    End Sub
    Works on the currently active sheet.

  3. #3
    Great thankyou.

    How can I apply this macro so when I open this excel document and enter a number into the D1 cell it changes the number of times it pastes the cells in range A1:B10? ie, it is always active.

    If the number is above 3 then a new position based on a pattern will need to be set. The next location will be J20 for example.

    Hope you can help

  4. #4
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,356
    Articles
    0
    Excel Version
    365
    Quote Originally Posted by Nick_Somerset View Post
    If the number is above 3 then a new position based on a pattern will need to be set. The next location will be J20 for example.
    The next location already is J20, no new pattern needed there.





    Quote Originally Posted by Nick_Somerset View Post
    How can I apply this macro so when I open this excel document and enter a number into the D1 cell it changes the number of times it pastes the cells in range A1:B10? ie, it is always active.
    This in the sheet concerned's code module:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$1" Then
      For i = 1 To Range("D1").Value
        Range("A1:B10").Copy Cells(20, (i - 1) * 3 + 1)
      Next i
    End If
    End Sub

  5. #5
    So I have the first bit of code in book1 code window and the second bit of code in the sheet concerned code module.

    I was hoping that when I open the document and type a number into D1 that many pastes are made in the cells below and If I changed that number in cell D1 then the pastes below would change again. Each time I did it without having to run the macro.

    Is this possible?

    Thanks
    Nick

  6. #6
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,356
    Articles
    0
    Excel Version
    365
    Quote Originally Posted by Nick_Somerset View Post
    So I have the first bit of code in book1 code window and the second bit of code in the sheet concerned code module.
    you only need the code the sheet's code module. You should delete the code anywhere else.



    Quote Originally Posted by Nick_Somerset View Post
    I was hoping that when I open the document and type a number into D1 that many pastes are made in the cells below
    it does this.



    Quote Originally Posted by Nick_Somerset View Post
    and If I changed that number in cell D1 then the pastes below would change again.
    It does this, but bear in mind that no deleteing is done prior to the pasting, so if data is already there it will overwrite it and if the number in D1 is reduced the previous pastes will not be deleted.



    Quote Originally Posted by Nick_Somerset View Post
    Each time I did it without having to run the macro.
    Excellent!



    Quote Originally Posted by Nick_Somerset View Post
    Is this possible?
    It already is happening.

  7. #7
    Haha! Yes! It works... as you know. Thankyou.

    So next step...

    If i type 1 in the D1 box it pastes 1. OK.

    If i type 5 it pastes 5. OK.

    Can we get it to go from 5 to 4 for example? ie delete the already pasted cells?

    Go on... i know you can do it!?

  8. #8
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,356
    Articles
    0
    Excel Version
    365
    if you're happy clearing entire rows 20:30 on the sheet prior to pasting then:
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$1" Then
    Rows("20:30").Clear ' or .ClearContents
      For i = 1 To Range("D1").Value
        Range("A1:B10").Copy Cells(20, (i - 1) * 3 + 1)
      Next i
    End If
    End Sub

  9. #9
    Good answer. Boom!

    So lets say the range of cells (A1:B10) were all called "TEST" for example. Id now like to call one of those cells "TEST1".

    So lets say we call cell A1 "TEST1".

    Every time it pastes id like TEST1 t become TEST2, TEST3 etc...

    Hope this makes sense? Can this be done?

  10. #10
    Super Moderator p45cal's Avatar
    Join Date
    Dec 2012
    Posts
    1,356
    Articles
    0
    Excel Version
    365
    Code:
    Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$D$1" Then
      Rows("20:30").Clear  ' or .ClearContents
      For i = 1 To Range("D1").Value
        Set Destn = Cells(20, (i - 1) * 3 + 1)
        Range("A1:B10").Copy Destn
        Destn.Value = Destn.Value & i
      Next i
    End If
    End Sub

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •