+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    02-27-2008
    Location
    Grand Prairie, Texas
    MS-Off Ver
    MS Office 2003 SP3
    Posts
    89

    copy column to new sheet

    I have a very simple task: I have a column listing a bunch of names on a sheet called "Names", and I want to include in a subroutine the code to copy those names into the last sheet starting at cell A2 and going down. When I write the code as 3 separate lines, it works just fine:


    Code:
    Worksheets("Names").Range("A1").Select
    Worksheets("Names").Range("A1", Selection.End(xlDown)).Select
    Selection.Copy Destination:=Worksheets(Sheets.Count).Range("A2")

    But when I try to put it all together as one line, it chokes:

    Code:
    Worksheets("Names").Range("A1", Selection.End(xlDown)).Copy Destination:=Worksheets(Sheets.Count).Range("A2")
    Now, I know that since I have the code working in 2 lines, I should just be happy an move on, but the perfectionist in me wants to know what about the one-line version I'm screwing up. Any suggestions?

    Thanks,
    Adam
    Last edited by ahartman; 11-02-2009 at 05:29 PM. Reason: SOLVED
    -Adam Hartman
    Mechanical Engineer
    Siemens Industry, Low Voltage Building Technology
    Grand Prairie, TX

  2. #2
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: copy column to new sheet

    hi Adam,
    I like your need for perfection - try this one out, it doesn't cut it down to 1 line, but is tidy & links the ranges to the worksheet* using the With statement.
    *rather than to the "selection" - in the search for perfection, "select" or "selection" often causes choking!

    Code:
        With ThisWorkbook.Worksheets("Names")
            .Range("A1", .Cells(1, 1).End(xlDown)).Copy Destination:=ThisWorkbook.Worksheets(Sheets.Count).Range("A2")
        End With
    Edit: Explanation, by removing the ...".select" of the first line, you are removing certainty of what the selection actually is, but certainty is needed with this code because of the use of "selection" in the second line of your original code.

    hth
    Rob
    Last edited by broro183; 11-02-2009 at 05:26 PM. Reason: provide explanation
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  3. #3
    Registered User
    Join Date
    02-27-2008
    Location
    Grand Prairie, Texas
    MS-Off Ver
    MS Office 2003 SP3
    Posts
    89

    Re: copy column to new sheet

    Quote Originally Posted by broro183 View Post
    hi Adam,
    I like your need for perfection - try this one out, it doesn't cut it down to 1 line, but is tidy & links the ranges to the worksheet* using the With statement.
    *rather than to the "selection" - in the search for perfection, "select" or "selection" often causes choking!

    Code:
        With ThisWorkbook.Worksheets("Names")
            .Range("A1", .Cells(1, 1).End(xlDown)).Copy Destination:=ThisWorkbook.Worksheets(Sheets.Count).Range("A2")
        End With
    Edit: Explanation, by removing the ...".select" of the first line, you are removing certainty of what the selection actually is, but certainty is needed with this code because of the use of "selection" in the second line of your original code.

    hth
    Rob
    Glorious! Thanks for the help, Rob.
    -Adam Hartman
    Mechanical Engineer
    Siemens Industry, Low Voltage Building Technology
    Grand Prairie, TX

  4. #4
    Forum Guru
    Join Date
    01-03-2006
    Location
    Basingstoke, England
    MS-Off Ver
    2007 (work & home)
    Posts
    2,240

    Re: copy column to new sheet

    I'm pleased I could help - thanks for the feedback & for adding to my reputation

    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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.2.0