+ Reply to Thread
Results 1 to 19 of 19

Trying to find the right code to copy a column from one worksheet and paste to another

Hybrid View

  1. #1
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Trying to find the right code to copy a column from one worksheet and paste to another

    Hi guys,

    I'm trying to create a button that essentially copies a column of data from one worksheet (prime) and pastes this column of data in another worksheet (secondary).

    However, I want to be able to keep pressing this button so that the newly pasted data in secondary is pasted into the next column. So whatever new inputs in the prime column will be copy pasted into a new column to the right of the previous copy paste. So far, I have this code below:

    The problem is it only copies and pastes to one column...

    Sub CollateProcurement()
        Dim NextRow As Long
        With Worksheets("Trend Analysis")
            iLastColumn = .Range("H7").End(xlToRight).Column
            Sheets("Procurement Pre Mobilisation").Range("I4").Copy .Range("H7")
            Sheets("Procurement Pre Mobilisation").Range("I5").Copy .Range("H8")
            Sheets("Procurement Pre Mobilisation").Range("I6").Copy .Range("H9")
            Sheets("Procurement Pre Mobilisation").Range("I7").Copy .Range("H10")
            Sheets("Procurement Pre Mobilisation").Range("I8").Copy .Range("H11")
            Sheets("Procurement Pre Mobilisation").Range("I9").Copy .Range("H12")
            Sheets("Procurement Pre Mobilisation").Range("I10").Copy .Range("H13")
            Sheets("Procurement Pre Mobilisation").Range("I11").Copy .Range("H14")
            Sheets("Procurement Pre Mobilisation").Range("I12").Copy .Range("H15")
            Sheets("Procurement Pre Mobilisation").Range("I13").Copy .Range("H15")
            Sheets("Procurement Pre Mobilisation").Range("I14").Copy .Range("H17")
            Sheets("Procurement Pre Mobilisation").Range("I15").Copy .Range("H18")
            Sheets("Procurement Pre Mobilisation").Range("I16").Copy .Range("H19")
            Sheets("Procurement Pre Mobilisation").Range("I17").Copy .Range("H20")
            Sheets("Procurement Pre Mobilisation").Range("I18").Copy .Range("H21")
            Sheets("Procurement Pre Mobilisation").Range("I19").Copy .Range("H22")
            Sheets("Procurement Pre Mobilisation").Range("I20").Copy .Range("H23")
            Sheets("Procurement Pre Mobilisation").Range("I21").Copy .Range("H24")
            Sheets("Procurement Pre Mobilisation").Range("I22").Copy .Range("H25")
        End with
    End Sub
    Any help would be greatly appreciated!

    Thanks guys.

    Kind regards,
    M
    Last edited by mw91; 11-15-2013 at 06:58 AM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Wracking my brain over this VBA code. Need help!

    Try this code -
    Sub CollateProcurement()
    Dim lcol As Long
    With Worksheets("Trend Analysis")
        lcol = .Range("XFD7").End(xlToLeft).Column
        Worksheets("Procurement Pre Mobilisation").Range("I4:I22").Copy .Cells(7, lcol + 1)
    End With
    End Sub
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Wracking my brain over this VBA code. Need help!

    Arlu,

    I tried to code you've suggested but it's not working. Doesn't even paste the numbers into the Trend Analysis worksheet =/

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Wracking my brain over this VBA code. Need help!

    Do you have any merged cells? Can you attach a sample file?

    To Attach a File:

    1. Click on Go Advanced
    2. In the frame Attach Files you will see the button Manage Attachments
    3. Click the button.
    4. A new window will open titled Manage Attachments - Excel Forum.
    5. Click the Browse... button to locate your file for uploading.
    6. This will open a new window File Upload.
    7. Once you have located the file to upload click the Open button. This window will close.
    8. You are now back in the Manage Attachments - Excel Forum window.
    9. Click the Upload button and wait until the file has uploaded.
    10. Close the window and then click Submit.

  5. #5
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Wracking my brain over this VBA code. Need help!

    Hope this works.

    Essentially. What I want is for the data in column I on the first sheet to be copy pasted into the different month columns in sheet 2 by clicking the macro button.

    So each time new data is put in the I column I need only click the button in sheet 2 to have it automatically copy paste into the next available blank column.

    If for example nothing has changed and I pressed the button then it still copies the data in column I from the first sheet and pastes it in the next blank column even if the numbers are exactly the same.

    Hope this clarifies!

    Kind regards,
    Martin
    Attached Files Attached Files

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    If for example nothing has changed and I pressed the button then it still copies the data in column I from the first sheet and pastes it in the next blank column even if the numbers are exactly the same.
    So do you want the macro to check if the data is the same from the previous copy paste and only if it is not, it should copy?

  7. #7
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Nope, I don't mind if the data is the same.

    Because essentially it's supposed to see home much has been done on certain actions.

    So if one month the completion of a project is 40% and the next is 40% that is valuable data to query why it hasn't increased.

    I just want the button to pretty much copy paste whatever values are in column I on worksheet 1 into the columns from H-P in worksheet 2. So every month someone can access this worksheet and just click the button and the values are automatically inserted with the values that may or may have not changed dependant on someone else's input.

  8. #8
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    I copied the code to a module in the file and assigned it to the button and it worked. Did you do the same?

  9. #9
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    That's really weird...I just did the same on the sample file and it works. BUT I try the same code in the main real file I'm using and it doesnt do anything...

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Does your file have any other codes? Did you right click on that cute button and say "Assign Macro" ?

  11. #11
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Yeah, I mean. I've just copy pasted that code for another button for a different table beneath the first one and it works perfectly! It's just this first table that's being a pain...

    Sub ColHS()
    Dim lcol As Long
    With Worksheets("Trend Analysis")
        lcol = .Range("XFD33").End(xlToLeft).Column
        Worksheets("Health and Safety Pre Mob").Range("G4:G9").Copy .Cells(33, lcol + 1)
    End With
    End Sub
    The above works just like it should for this other table further down.

    But the same code doesn't seem to want to work for the other table..

    Sub CollateProcurement()
    Dim lcol As Long
    With Worksheets("Trend Analysis")
        lcol = .Range("XFD7").End(xlToLeft).Column
        Worksheets("Procurement Pre Mobilisation").Range("I4:I22").Copy .Cells(7, lcol + 1)
    End With
    End Sub
    I can't seem to figure out the issue!

    Thank you very much for your time with this by the way, I appreciate it
    Last edited by arlu1201; 11-15-2013 at 12:12 PM.

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    I have added code tags to your post. As per forum rule 3, you need to use them whenever you put any code in your post. Please add them in future. In order to put code tags, either type [CODE] before your code and [/CODE] at the end of it, OR you can highlight your code and click the # icon at the top of your post window.

    Do you have merged cells in the destination sheet? Can you upload a copy of the file?

  13. #13
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    I also just realised...it is actually working...

    but pasting the values in columns further to the right for some reason.

    So it should be pasting into H7-P7 columns but it's pasting it in columns under AE7-onwards

    Which is really weird

  14. #14
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Ah right, sorry. Been really hectic today. Thanks Arlu.

    In the destination sheet there are merged cells but it is essentially exactly the same as the sample xl. Just more tables below which seem to work fine with your code..

    Due to the commercially sensitive nature of this workbook ( has something like 30 tabs of info etc) I can't upload it unfortunately.

  15. #15
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Let me explain what the code does.

    It goes to the extreme last column of the file which is XFD and then moves back to the last occupied column in that row. Do you have data in AD7 or even a space? It is catching AD7 as the last occupied column in row 7 and then populating AE7.

  16. #16
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Ahhh, right. That makes sense! Yes I do have data in AD7!

    Should I just move whatever I have in that area to somewhere else then? Just to simplify things?

    Thank you!

  17. #17
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Yeah that will be better.

  18. #18
    Registered User
    Join Date
    11-15-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    31

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Ok, perfect! Thanks a lot for your help Alu

    Kind regards,
    M

  19. #19
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Trying to find the right code to copy a column from one worksheet and paste to another

    Based on your last post in this thread, its apparent that you are satisfied with the solution(s) you've received and have solved your question, but you haven't marked your thread as "SOLVED". I will do it for you this time.

    In future, to mark your thread as Solved, you can do the following -
    Select Thread Tools-> Mark thread as Solved.

    Incase your issue is not solved, you can undo it as follows -
    Select Thread Tools-> Mark thread as Unsolved.

    Also, since you are relatively new to the forum, i would like to inform you that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post which helped you. This adds to the reputation of the person who has taken the time to help you.

+ 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 - find next empty column, insert column, copy paste values & copy paste formulas
    By DoodlesMama in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-20-2012, 12:43 PM
  2. Code: Find, Copy, Paste not working
    By ufopilot3 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-26-2012, 03:40 PM
  3. Help with find, copy, and paste code using offset
    By mundellj in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-28-2011, 08:16 PM
  4. code to find, copy and paste until find new, then repeat
    By siddharthariver in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-12-2010, 04:02 PM
  5. Find Value and Copy/Paste Row from One Worksheet to Another
    By GravityInvert in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-15-2008, 02:56 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