+ Reply to Thread
Results 1 to 21 of 21

VBA code to paste to a new row in the same column ???

  1. #1
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    VBA code to paste to a new row in the same column ???

    Hi

    i would greatly appreciate your help with this.

    i am setting up a macro to copy names from a column in each of three tables and then paste them all together in one column in the same worksheet.

    i have attached my worksheet.

    so i want to be able to copy from cell A3 all the way down to end of col A, and paste that to S3.

    And then ALSO

    copy from cell G3 down to the end ( or it can be from G2 to include the NAME title if its easier ) .

    but the thing i am struggling with is now to paste it to the bottom of the column S ( that already has the pasted data that was done earlier ).

    so in effect i would have a macro that copies all names in the tables ( no other data from tables required ), and pastes those names into column s. ( to look like column y )

    really hope someone can help with this and is very much appreciated

    many many thanks

    steve r.
    Attached Files Attached Files

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    There are many ways of accomplishing this. Here's just one:
    Please Login or Register  to view this content.
    BSB

  3. #3
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    Add the below line, just before the End Sub line to get rid of the "marching ants" around the cells from the last copy.
    Please Login or Register  to view this content.
    BSB

  4. #4
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    THANKS badly speeled buoy,

    this works in your example.

    i have never used this listobject code before.

    the sheet that i provided was a test sheet from my actual sheet ( which i cannot provide due to data protection )

    although i will look into the use of the list objects.
    to get it going for now and simplify, if its assumed i can copy each column. what would be the code i could use to paste to a new row in an existing column

    so if i have already got the code

    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    that copies the data.

    what would be the code the pastes to a new row in column s ???


    many thanks for your help

  5. #5
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    I would sugest not using .Select in your code if at all possible. 99.9% of the time it is completely unecessary and will cause more trouble than it solves.
    But as you understand those three lines of code I'll gloss over that for now.

    Once they're copied you can just use the below line from my code to do the pasting.
    Please Login or Register  to view this content.
    Repeat your lines for copying other columns then mine again to tag the copied values at the end of column S.

    For info ListObjects is how you refer to tables in Excel. So my code just loops through each table in Sheet1 and copies the first column "ListColumns(1)" from each in turn.
    Thay way if you add more tables you don't have to amend the code.

    BSB

  6. #6
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    Hi, bedlySpelledBouy,

    thanks for the info on that - im looking into it regarding listobjects.

    i have tried to insert the code
    .Cells(Rows.Count, "S").End(xlUp).Offset(1).PasteSpecial xlValues

    exactly as it is to my code

    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy

    so the code is now
    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    .Cells(Rows.Count, "S").End(xlUp).Offset(1).PasteSpecial xlValues

    but its coming up with an error message
    compile error
    invalid or unqualified reference

    it doesnt highlight where though

    can you see anything wrong or as to why its not working, thanks, steve

  7. #7
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    Hi,
    regarding your code of ListObjects earlier.

    Does it work if the tables are actually the result of a power query. as mine are in my originol source ??

    i think thats why listobjects might not be working for me ?? let me know what you think on this , and if possible if the above error message on the coding

    Please Login or Register  to view this content.
    thanks steve.

    its all a learning curve when i do excel it seems !!!
    Last edited by steve78; 06-26-2022 at 11:03 AM.

  8. #8
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    Excel is all about the learning curve, and it's never ending

    In order to adhere to the forum rules you need to edit your posts above to wrap your VBA in code tags. Simply click "Edit post" bottom right of each one then highlight the VBA sections and click the # icon above your post before saving it.
    Moderators will tell me off for replying and you off for not sticking to the rules otherwise...

    Once you've done that I can tell you the simple reason why you're getting that error message and how to fix it.

    BSB

  9. #9
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    ok no probs, didnt know that. ill keep in mind

  10. #10
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    Remove the dot before the word Cells in the last line of code.
    Please Login or Register  to view this content.
    The error was caused by that dot/full stop as Excel was unsure of which sheet you were referring to the Cells of whilst it's there.
    I forgot to remove it from my code but it would have worked fine in my proposed solution using a With Block.


    Just to add to the learning, remember I mentioned earlier about not using .Select?
    Well there's the same code as above but rewritten to not use Select.
    Please Login or Register  to view this content.
    BSB

  11. #11
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    Thanks very much BSB,

    That does indeed now work. Cant believe it but Excel has thrown me another curveball problem that i didnt see coming and have not seen before.

    basically , as i said that all works. but when i come to do the next days data i would need to delete the destination data from the previous day ( my S column ) so just keeping the column header in s1 but deleting all other data in the table in s.

    but when i run the macro.

    instead of pasting to s2 and so on. ( as the macro intended )

    it , for some reason, pastes to the bottom of the PREVIOUS days entries , to s28 ( even though the rows above it i have since deleted , so i have 27 empty rows and then the new data ??? )

    for me its a mystery as to why it does that considering there is no data in s column apart from the header in s1.

    do you know why this is happening and how to rectify it ?? is it becuase i have converted column s to a table as i need it to form the basis of a power query ??

    thanks, any help you can give to this saga is more than appreciated ....

    confused ... steve r

  12. #12
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    I feel you're deleting the values from the new table, but not the table rows. So when you try to stamp in the next blank row Excel is stamping to the next blank row AFTER the table.

    Attach an up to date workbook showing all your code so far and I'll help you adjust it so it works as expected.

    BSB

  13. #13
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    Hi BSB,

    sure my workbook is attached

    and the macro code im using is described in cell AA

    but it is

    Please Login or Register  to view this content.
    so the code is ( was ) working, but its just pasting to the bottom of the table in S instead of in s3 and so on

    you must be correct in that even though im deleting the previous days data in col s. its not deleted properly in terms of what the code is seeing.

    i dont know how to deal or correct this though and i do need to delete previous days data in col s.

    pls let me know if you have anything to rectify, thanks steve.
    Attached Files Attached Files

  14. #14
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    This version will clear the old data (by deleting the table rows rather than just clearing the data they contain) and will then paste the values from column A into the table:
    Please Login or Register  to view this content.
    If you want to add all three tables to it then you'll be better off going back to my original suggested approach of looping through tables, like this:
    Please Login or Register  to view this content.
    Now this one will still leave a blank row, but only because your 2nd table has a blank row at the end. Sort that out and all should work as expected.
    If the blank could really be there and you don't want to include it then the code will need a further slight change:
    Please Login or Register  to view this content.
    Hopefully that covers all scenarios, but let me know if I've missed something.

    BSB
    Last edited by BadlySpelledBuoy; 06-26-2022 at 02:07 PM.

  15. #15
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    thanks for the detailed response BSB

    and thanks for the time and effort you have put into this.

    unfortunately as mentioned the worksheet that i have given is a working sheet that i am using as i cant post origonal due to data protectiona nd gdpr etc.

    and so i cant change the way the sheet is coded to include ListObjects, and futhermore all the code you have suggested.

    the only code i can write into the document would be without changing it to listobjects, etc

    i understand your response will work and is the best way to do it and as said i appreciate the effort you have gone to there. ( i indeed may look into this way when i have time to change how i arrange the worksheet )

    in respect of the code
    Please Login or Register  to view this content.
    would there be anything i could do to that code to enable it to paste into my table ( col s ) , using the offset+1, when i have deleted the rows in said table. ?

    what do you think? possible or not really ??

    thanks, steve

  16. #16
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    Quote Originally Posted by steve78 View Post
    i cant change the way the sheet is coded to include ListObjects
    I'm not sure what you mean by this, OR, you've misunderstood what ListObjects are.
    You won't have to change anything in your workbook other than the VBA, and you're doing that anyway.
    You already have Excel Tables in your workbook and you've added an aditional one, so you are making changes to the workbook.
    ListObjects is just how you refer to these tables with VBA, they are exactly the same thing, so I'm a tad confused as to why the suggestions won't work for you.

    BSB

  17. #17
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    Hi BSB,

    my origonal document has lots (well 3) more tables in the worksheet that i cannot show due to data gdpr and also the tables that i show are actually results of power queries ( when i tried ealier i dont think listobjects picks up the power queries as tables ) but maybe i am wrong on this, im not sure ??

    and so its not ideal that i cannot attach my actual document of course.

    i was just wondering if i could add something to the below code to get it to paste in S table in the next row.

    Please Login or Register  to view this content.
    not sure if there is anything i can do to it, thanks for any help you can give.

  18. #18
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    just thinking about it. maybe i could delete the table rows manually ( right click delete rows ) instead of just deleting the data in the table. then i can see that code you gave me earlier working ???

  19. #19
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    Quote Originally Posted by steve78 View Post
    just thinking about it. maybe i could delete the table rows manually ( right click delete rows ) instead of just deleting the data in the table. then i can see that code you gave me earlier working ???
    Hmmm... Not sure that would be an ideal solution. The idea of VBA for this type of thing is 'automation' not 'do a little bit manually and automate the rest'.

    Give this one a try:
    Please Login or Register  to view this content.
    It relies on your actual workbook being laid out as per your example file. If it's not then it may fall over, and goes to demonstrate the importance of representative examples.

    BSB

  20. #20
    Forum Contributor
    Join Date
    07-07-2019
    Location
    london
    MS-Off Ver
    2013
    Posts
    105

    Re: VBA code to paste to a new row in the same column ???

    thanks for this BSB, it does indeed work on my sheet. The issue is that i cant attach my exact sheet due to data protections but in sessence this obviosly works , thanks for the help.

  21. #21
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,944

    Re: VBA code to paste to a new row in the same column ???

    No problem. Happy to help and glad we got there in the end

    BSB

+ 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. VBA code that copies different sheets and paste in one sheet, column by column
    By leewk114 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-06-2019, 07:34 PM
  2. Need VBA Code Amended to Paste in the Next Available Column
    By YukonSoDank in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-03-2019, 01:47 PM
  3. [SOLVED] VB Code to copy paste data from column to another column based on content of cell
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-28-2019, 08:50 PM
  4. [SOLVED] VBA Code to lookup value in column then paste to right
    By Ollie7957 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-21-2017, 07:47 AM
  5. Replies: 5
    Last Post: 08-01-2014, 04:30 PM
  6. Macro Code Paste into Column
    By ryanb4614 in forum Excel General
    Replies: 1
    Last Post: 07-28-2012, 12:22 PM
  7. VBA Code to copy data from last filled column and paste to the next available column.
    By Pavman2473 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-09-2012, 09:07 PM

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