+ Reply to Thread
Results 1 to 16 of 16

Code to have cells copied from one sheet and pasted under cell in another sheet

Hybrid View

  1. #1
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76

    Code to have cells copied from one sheet and pasted under cell in another sheet

    hi! I have data in column a in sheet 1. there is a number in cell a1, a16, a31 and so on... the cells are evenly spiliy by 15 blank rows in between them

    I would like to copy the cells I have in sheet 2 range a1:a14 and paste it under cell a2, then paste the same range (a1:a14) under cell a16 and so on... essencially taking the range from sheet 2 and pasting it under every cell with a number in it in sheet 1. there is a different number of cells with text in sheet 1 each time i would run the macro but the range that is to be copy and pasted from sheet 2 will be constant.

    can anyone help me with this code!??!?! please please please
    Last edited by fruitloop44; 10-29-2017 at 09:53 PM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    Test this.

    Sub tst()
        Dim lRow As Long, i As Long
        lRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
        For i = 1 To lRow Step 15
            Sheet1.Cells(i, 1).Offset(1).Resize(14).Value = Sheet2.Range("A1:A14").Value
        Next
    End Sub
    Last edited by bakerman2; 10-29-2017 at 10:08 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    @bakerman2 thank you, but unfortunately, this didn't work. it pasted the values not the formulas and it did not loop, meaning it pasted the values form sheet 2 under cell a1 but there was nothing pasted under cell a16 or a31 and so on

    please advise

  4. #4
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    or testing for values each time (this stops at the first blank)

    Sub copydown()
    i = 1
    While Cells(1 + 15 * i, 1).Value <> ""
    Range("a1:a14").Copy Destination:=Cells(2 + 15 * i, 1)
    i = i + 1
    Wend
    End Sub
    If you want something done right... find a forum and ask an online expert.

    Time flies like an arrow. Fruit flies like a banana.

  5. #5
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    @scottiex nothing happened when i ran this macro

  6. #6
    Forum Expert
    Join Date
    04-01-2013
    Location
    East Auckland
    MS-Off Ver
    Excel 365
    Posts
    1,343

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    is your sheet set up correctly? does "A16" have a value?
    it worked for me and kept formulas.

    maybe you want to upload your actual sheet.

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    1. You never said anything about formulas.

    2.Loop works just fine for me.

    Sub tst()
        Dim lRow As Long, i As Long
        lRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
        For i = 1 To lRow Step 15
            Sheet2.Range("A1:A14").Copy Sheet1.Cells(i, 1).Offset(1)
        Next
    End Sub
    Check SheetCodeNames for match or provide example file so we have something to work with.
    Last edited by bakerman2; 10-29-2017 at 10:32 PM.

  8. #8
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    this worked thank you so much!!!! i tweaked it a but to copy more coloumns:

    Sub zoo()
    Dim lRow As Long, i As Long
    lRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
    For i = 1 To lRow Step 15
    Sheet1.Cells(i, 1).Offset(1).Resize(14).Value = Sheet2.Range("A2:a15").Formula
    Sheet1.Cells(i, 2).Offset(1).Resize(14).Value = Sheet2.Range("b2:b15").Formula
    '2 represents which cell to paste it in
    Next
    End Sub

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    Please amend your previous post with Code Tags.

    Glad you got it sorted out. Thanks for rep+.

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

  10. #10
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    hi... i'm still working on this and ran across a problem. the macro works, but since it copies the formulas in the cell, all of the copied cells reference the same cells... meaning,

    in a normal sheet: if i copy cell a1 and it has a refence to cell a2, and i paste that cell into b1, then it will now refence cell b2 since copying and pasting changes reference cells and thats what i would like this macro to do. is there any way to achieve this? maybe theres a way it can just copy the cells (with formulas inside) instead of specifically formulas?

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,914

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    How does this work for you ?

    Sub zoo()
    Dim lRow As Long, i As Long
    lRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
    For i = 1 To lRow Step 15
        Sheet2.Range("A2:A15").Copy
        Sheet1.Cells(i, 1).Offset(1).Resize(14).PasteSpecial xlPasteFormulas
        Sheet2.Range("B2:B15").Copy
        Sheet1.Cells(i, 2).Offset(1).Resize(14).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
    '2 represents which cell to paste it in
    Next
    End Sub

  12. #12
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76
    This goes me an error saying “object doesn’t support this property or method”... it copies the cells but can’t paste them. It breaks on the first line that ends with “pastespecial xlpasteformulas”

    Quote Originally Posted by bakerman2 View Post
    How does this work for you ?

    Sub zoo()
    Dim lRow As Long, i As Long
    lRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
    For i = 1 To lRow Step 15
        Sheet2.Range("A2:A15").Copy
        Sheet1.Cells(i, 1).Offset(1).Resize(14).PasteSpecial xlPasteFormulas
        Sheet2.Range("B2:B15").Copy
        Sheet1.Cells(i, 2).Offset(1).Resize(14).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
    '2 represents which cell to paste it in
    Next
    End Sub

  13. #13
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,512

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    'you also can do use specialcell in sheet1
    After copy from sheet2
    Dim r as range
    Set r =sheet1.range("A:A").specialcells(2,1).areas
    And paste to r.count+1
    Sub zoo()
    Dim lRow As Long, i As Long
    lRow = Sheet1.Range("A" & Sheet1.Rows.Count).End(xlUp).Row
    On error resume next
    For i = 1 To lRow Step 15
        Sheet2.Range("A2:B15").Copy
        Sheet1.Cells(i, 1).Offset(1).Resize(14,2).PasteSpecial xlPasteFormulas
        Application.CutCopyMode = False
    '2 represents which cell to paste it in
    Next
    End Sub

  14. #14
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,512

    Re: Code to have cells copied from one sheet and pasted under cell in another sheet

    Sub Maybe()
    Dim r As Range
    Sheet2.[A2:B15].copy
    For Each r In Range("A1", Cells(Rows.Count, 1).End(xlUp)).SpecialCells(2,1).Areas
        r(r.Count + 1, 1).Resize(r.Count-1,2).pastespecial xlpasteformulas
    Next r
    Application.cutcopymode = False
    End Sub

  15. #15
    Registered User
    Join Date
    10-29-2017
    Location
    Miami, FL
    MS-Off Ver
    2010
    Posts
    76
    Same error it won’t paste and breaks on the same line

    Quote Originally Posted by daboho View Post
    Sub Maybe()
    Dim r As Range
    Sheet2.[A2:B15].copy
    For Each r In Range("A1", Cells(Rows.Count, 1).End(xlUp)).SpecialCells(2,1).Areas
        r(r.Count + 1, 1).Resize(r.Count-1,2).pastespecial xlpasteformulas
    Next r
    Application.cutcopymode = False
    End Sub

  16. #16
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,512
    Quote Originally Posted by fruitloop44 View Post
    hi! I have data in column a in sheet 1. there is a number in cell a1, a16, a31 and so on... the cells are evenly spiliy by 15 blank rows in between them

    I would like to copy the cells I have in sheet 2 range a1:a14 and paste it under cell a2, then paste the same range (a1:a14) under cell a16 and so on... essencially taking the range from sheet 2 and pasting it under every cell with a number in it in sheet 1. there is a different number of cells with text in sheet 1 each time i would run the macro but the range that is to be copy and pasted from sheet 2 will be constant.

    can anyone help me with this code!??!?! please please please
    You talk in range a1,a16 =number and in A2:A15.value=sheet2.[a1:a14]
    Run code in sheet1
    sub test ()
    Sheet2.[a1:a14].copy
    Dim r,r1 as range
    Set r1=range("A1",[A100000].end(3)).specialcells(2,1).areas
    For each r in r1
    Msgbox r(r.count+1,1) & "this cell to paste"
    r(r.count+1,1).PasteSpecial XLPasteFormulas
    Next r
    Application.cutCopyMode = 0
    End sub
    Last edited by daboho; 12-20-2017 at 03:57 PM.

+ 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: 0
    Last Post: 02-18-2016, 07:26 AM
  2. [SOLVED] Add/copy row in one sheet when another sheet is copied/pasted using VBA
    By wildonln in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-02-2016, 05:45 AM
  3. Replies: 2
    Last Post: 01-01-2015, 09:38 PM
  4. Code to allow user to choose a sheet a copied cell is pasted to
    By tancho321 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-28-2013, 01:11 PM
  5. Code not working on new sheet copied from a sheet that works
    By drewship in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 10-20-2010, 08:54 AM
  6. Replies: 1
    Last Post: 07-07-2005, 10:05 PM
  7. Delete the formulas of webpage, copied & pasted on excel sheet
    By Mustafa Abedin in forum Excel General
    Replies: 1
    Last Post: 06-19-2005, 10:05 AM

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