+ Reply to Thread
Results 1 to 5 of 5

Copy several columns into a specific range (new workbook)

  1. #1
    Registered User
    Join Date
    02-09-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Copy several columns into a specific range (new workbook)

    Hi there,

    I have been lately working with VBA and I got stuck, selecting the data I am interested in and copy it into a new workbook. I have seen examples and youtube videos but I have clearly missed something.

    I have posted part of the code, as the rest is working fine:


    Please Login or Register  to view this content.


    Basically I want to copy several (but not all) columns from the opened file, into specific columns inside the new workbook.

    Any help is highly appreciated

    Many Thanks

    Ricardo
    Last edited by Barrigudo; 02-09-2016 at 12:47 PM.

  2. #2
    Forum Contributor
    Join Date
    12-05-2015
    Location
    Akron, OH
    MS-Off Ver
    15.0
    Posts
    424

    Re: Copy several columns into a specific range (new workbook)

    I eliminated some duplicate declarations (and duplicate code), moved your declarations up to the top, and re-wrote the portion of the code that copies columns to use .Resize and (...).Cells.Value = r1.Cells.Value methods instead of the .copy method. I did not add any variables and I tried to leave most of your code b/f the copy attempt was made.

    If at all possible, avoid the .copy method unless you want to copy over formatting and don't want to spend time to write additional code to carry over the formatting. It's orders of magnitude slower.

    Please Login or Register  to view this content.
    ...it compiles -- but I had to add a "Next NFile" because it looks like this is only an excerpt from your code.

    Hope this helps.
    Last edited by joe31623; 02-09-2016 at 01:25 PM.
    <---If my answer helped, please click *

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Copy several columns into a specific range (new workbook)

    Hi Ricardo and welcome to the forum,

    I can do your problem without needing any VBA in about 10 mouse clicks. Here is how......

    Excel realized a few version ago that users wanted to import all kinds of data into Excel. They created a tool they call now Power Query. I have it built into my 2016 version of Excel and if you don't have it you can download it for your version.

    Power query is like a pre-processor of data from other file types. Here is how I'd do you problem:

    1. Open a new and blank Excel Workbook
    2. In 2016 I click on the Data Tab and then the "New Query" dropdown.
    3. Select From File -> From Workbook
    4. This shows a dialog to pick the file I want to import the columns from. Select the file and click the Import button.
    5. You will see a dialog asking which sheet(s) you want to import columns from. Select a sheet.
    6. Now click on the EDIT button on the lower right of this "Navigator Dialog Screen"
    7. Click on the Column Headings you want to import, until they are all selected.
    8. Right click on any of the selected column heads and then on "Remove Other Columns"
    9. Click on the "Close & Load" icon in this window.

    DONE!!!! No VBA needed as I used Power Query to import columns from another workbook.

    Read more about it at:
    https://support.office.com/en-us/art...5-89F6269CD605
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    02-09-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Re: Copy several columns into a specific range (new workbook)

    Thanks for the quick reply,

    In fact, I tried to improve the code, by using more ranges to capture several columns, but somehow the data copied from these ranges never reaches the new workbook and most of the transmission is lost, being able to insert the last headers only:

    Please Login or Register  to view this content.

    I think this method is better, but no clue what kind of mistake I am doing, as the program runs without errors.

    Thanks for your patience

  5. #5
    Registered User
    Join Date
    02-09-2016
    Location
    London
    MS-Off Ver
    2013
    Posts
    3

    Re: Copy several columns into a specific range (new workbook)

    Thanks a lot Marvin.

    I will take a look into that new way of transferring data

    Cheers

+ 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: 07-24-2014, 09:11 AM
  2. [SOLVED] Copy Two Specific Columns from One Spreadsheet to Another in the Same Workbook
    By Deathwreaker in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-29-2014, 05:04 PM
  3. [SOLVED] Copy a specific range from one workbook to another
    By ArnolddG in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-13-2014, 08:07 AM
  4. how to copy specific columns form one workbook to another
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2014, 11:53 PM
  5. how to copy specific columns form one workbook to another
    By amethystfeb in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-03-2014, 10:49 PM
  6. Copy specific columns from one workbook to another workbook
    By Tegpreet in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2013, 07:55 AM
  7. Copy specific columns and save into another workbook
    By stoey in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-12-2008, 04:02 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