+ Reply to Thread
Results 1 to 14 of 14

Beginner - Need to copy data between columns

  1. #1
    Registered User
    Join Date
    12-06-2016
    Location
    Paris, France
    MS-Off Ver
    2012
    Posts
    27

    Beginner - Need to copy data between columns

    Im trying to select a range, and then copy the formulas inside it to the next row on the right

    I wrote this with the help of the macro recorder and some code I found online but it doesn't work.

    The last line makes it crash.

    Can somebody help me with this ?

    Please Login or Register  to view this content.
    Last edited by DaChocapic; 12-07-2016 at 08:18 AM. Reason: title

  2. #2
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    Your code goes all the way to the last column to the right... selects a few rows copies and then tries to paste one column to the right... that is past the right-most column.

    I don't know what range you want to copy but the code below would copy A6 to A7 and paste it in B6 (and below).

    Please use code tags when posting code.

    Please Login or Register  to view this content.
    Last edited by Arkadi; 12-06-2016 at 11:20 AM.
    Please help by:

    Marking threads as closed once your issue is resolved. How? The Thread Tools at the top
    Any reputation (*) points appreciated. Not just by me, but by all those helping, so if you found someone's input useful, please take a second to click the * at the bottom left to let them know

    There are 10 kinds of people in this world... those who understand binary, and those who don't.

  3. #3
    Registered User
    Join Date
    12-06-2016
    Location
    Paris, France
    MS-Off Ver
    2012
    Posts
    27

    Re: Beginner, my code doesnt work and I don't understand why

    Thank you for your reply.
    What I would like to do is not to copy A6:A7 in B6 but to copy the last column that is not empty, in the next empty column. How would I do that?

    Thanks for the tip about the code tags as well.

  4. #4
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    What row would be used to decide the last column with data?

  5. #5
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    This code would find the last column with data based on row 1 and copy the whole column to the next one to the right.

    Please Login or Register  to view this content.

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,873

    Re: Beginner, my code doesnt work and I don't understand why

    @DaChocapic
    Welcome to the forum. It appears that you did not take the time to read our few forum rules when you joined. Please note the following issues with your post and take time to read and understand our forum rules.



    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)


    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution.

    Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.

    To change a Title go to your first post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  7. #7
    Registered User
    Join Date
    12-06-2016
    Location
    Paris, France
    MS-Off Ver
    2012
    Posts
    27

    Re: Beginner, my code doesnt work and I don't understand why

    @Arkadi

    Thank you, this seems to work.

    Can you help me understand it ?

    Please Login or Register  to view this content.

    Did I get this right?

    @alansidman got it, thank you for your help, I'll make sure to be more carefull for my next post.

  8. #8
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    DaChocapic:

    These 2 lines just define the variables we are going to use. lastCol is a number (long integer), and ws is a worksheet object
    Please Login or Register  to view this content.
    Then we use "Set" for the worksheet object, which means we make ws in the code represent the same as "Worksheets("Sheet1")". It makes it easier to code using "ws" instead of typing it out each time. Also easier to edit if you need to change the sheet name, since you only need to change it in one place:
    Please Login or Register  to view this content.
    the lastCol variable is used to reference the last column with data... the formula starts on the right, and finds the last one with data by moving to the left. Same idea can be used for the last row in a column:
    Please Login or Register  to view this content.
    Then we copy the column number assigned to "lastCol", and when you use the copy function you can specify the destination next to it.
    Please Login or Register  to view this content.
    Columns(lastCol).Offset(,1) will move one column right from Columns(lastCol), notice the blank before the comma... which means we don't offset row, only column.

    You did great using the code tags there, thanks. Please also note that the description should be more indicative of what you actually need, something more like "Need to copy data between columns"... or something like that.
    Last edited by Arkadi; 12-06-2016 at 12:37 PM.

  9. #9
    Registered User
    Join Date
    12-06-2016
    Location
    Paris, France
    MS-Off Ver
    2012
    Posts
    27

    Re: Beginner, my code doesnt work and I don't understand why

    Thank you very much for your help

  10. #10
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    My pleasure

  11. #11
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,929

    Re: Beginner, my code doesnt work and I don't understand why

    DaChocapic and Arkadi please take note that when a senior member requests that some action be taken (thread title change in this case), that complying with that request is not optional, and the tread should receive no further posts until the required action is carried out./
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  12. #12
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    @ FDibbins... I noticed his code tags and had forgotten myself... I did edit my post to request for a change in description as well, I should have deleted my post I guess. I apologize, the intent was not to ignore the request by alanisdman and have received my infraction accordingly.

    Again, I apologize to you both.

  13. #13
    Forum Expert Arkadi's Avatar
    Join Date
    02-13-2014
    Location
    Smiths Falls, Ontario, Canada
    MS-Off Ver
    Office 365
    Posts
    5,059

    Re: Beginner, my code doesnt work and I don't understand why

    Ooops, double posted.

  14. #14
    Registered User
    Join Date
    12-06-2016
    Location
    Paris, France
    MS-Off Ver
    2012
    Posts
    27

    Re: Beginner, my code doesnt work and I don't understand why

    Sorry, I did want to do it and then got carried away by my problem. Will do now

+ 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. the code doesnt work with macro event
    By samisamih in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 04-21-2015, 03:41 AM
  2. ToggleButton1_Click doesnt work if code is located Module1!
    By HerryMarkowitz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-14-2015, 03:56 PM
  3. [SOLVED] VBA code to read last filled row of data column, works, but doesnt work
    By mDevel in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2013, 01:34 AM
  4. [SOLVED] Code working when using F8, but doesnt work properly through button click.
    By emilyloz in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-01-2013, 09:58 AM
  5. code doesnt work after upgrade to 2010
    By jmckee in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-12-2011, 09:57 AM
  6. Why doesnt this line of code work???
    By joseclar in forum Excel General
    Replies: 1
    Last Post: 05-05-2009, 02:01 PM
  7. 3 line code...my trim function doesnt work..
    By Piyush in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-01-2005, 12:13 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