+ Reply to Thread
Results 1 to 10 of 10

Copy specific columns from an external worksheet to current workbook

  1. #1
    Registered User
    Join Date
    03-01-2015
    Location
    Northants
    MS-Off Ver
    2007
    Posts
    15

    Copy specific columns from an external worksheet to current workbook

    Hi

    I am trying achieve the following using VBA (must be VBA as it is part of a larger process).

    Copy columns A:B in 'Sheet1' of external worksheet 'SourceData.xlsx' to the same columns in existing worksheet 'Upoad' in the the current workbook 'Master' (where all the VBA exists).

    The data in 'Sheet1' of 'SourceData' has been created by a 3rd party application so we have no control over its format: there is text in column A and a date/time stamp in column B.

    No user prompts or message boxes are required. This seems like a fairly straight forward process as it is always the same columns of data coming from the same file to the same destination.

    I am not a VBA specialist so would welcome any kind of assistance.

    Many thanks

  2. #2
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Copy specific columns from an external worksheet to current workbook

    Here's my go at things:

    I would create a folder for the external doc, because I like to know where my toys are. I'll call it 'SourceFolder' here. Put the SourceData workbook in the SourceFolder each time, then run the following code from your 'Master' workbook:

    (N.B.: My program is designed to delete 'SourceData' when it's done, clearing the folder. If you don't want that, then exclude the noted line below)

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    03-01-2015
    Location
    Northants
    MS-Off Ver
    2007
    Posts
    15

    Re: Copy specific columns from an external worksheet to current workbook

    Hi. Thanks for this - I will test it out tomorrow.

    I won't delete the file, but I will copy it to an archive folder with a time/date stamp appended to the name, then I will clear the content of Sheet1 so it can be re-used. I think I can do this as I have done it before on another app - its just a case of copying the code over and making the necessary changes.

    Thanks again for your help - I will let you know how I get on.

  4. #4
    Registered User
    Join Date
    03-01-2015
    Location
    Northants
    MS-Off Ver
    2007
    Posts
    15

    Re: Copy specific columns from an external worksheet to current workbook

    Hi

    I tried your code, and it works OK to a point. I get the dreaded 'Run-time error 1004, Application-defined or object-defined error'.

    This occurs when it tries to execute "rng.SpecialCells(xlCellTypeConstants).Select".

    I single-stepped through the code and it opens the data file OK but then it stops at the above command.

    I have slightly modified your code to target a specific file as there will also be a control file (.xlsx) in the same folder. I did, however, test your code before making any changes (other than putting the correct folder name in). Your code is also preceded by a user prompt asking if they want to update this data to start with.

    My code is as follows.

    Please Login or Register  to view this content.
    I hope you can help with this.

    Many thanks and regards.

  5. #5
    Registered User
    Join Date
    03-01-2015
    Location
    Northants
    MS-Off Ver
    2007
    Posts
    15

    Re: Copy specific columns from an external worksheet to current workbook

    Forgot to include the 'End Sub' in the code snippet.

  6. #6
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Copy specific columns from an external worksheet to current workbook

    Are there any merged columns in SourceData? Perhaps the time/date stamp?

  7. #7
    Registered User
    Join Date
    03-01-2015
    Location
    Northants
    MS-Off Ver
    2007
    Posts
    15

    Re: Copy specific columns from an external worksheet to current workbook

    Hi.

    No, there are no merged columns.

    A brief bit of information about the Excel file.

    It was created as an empty file for dumping records in to. We have a barcode reader that works on Wi-Fi. One of its features is that it can send the barcode data over Wi-Fi to a designated file on a server. This is the file it ends up in.

    The output from the barcode app can be in either CSV or Excel format. Sending it in CSV compacts it into a single field per row, which is difficult to separate as the first field is the customer number and barcode number and they are of a variable length. Also, the app does not give any flexibility on the CSV format (eg deliimiters). The older version of this program worked well and it was pulling in a CSV file happily, but I don't understand enough about it to modify the code.

    Another option I thought of was to copy the worksheet in in its entirety to a new sheet (eg DataIn), then copy the data from the DataIn worksheet to the Upload worksheet. Then, delete the DataIn worksheet. This may be simpler.

    Your thoughts would be appreciated.

    Regards

    Tom

  8. #8
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Copy specific columns from an external worksheet to current workbook

    In truth, copying with "select" commands is an ill-advised practice. I shouldn't be so lazy. 'Select' has a few advantages, but it tends to be slower and less predictable. A possible alternate route would be to replace:

    Please Login or Register  to view this content.
    with:

    Please Login or Register  to view this content.
    Assuming there's no unwanted data in SourceData...

  9. #9
    Forum Expert
    Join Date
    05-20-2015
    Location
    Chicago, Illinois
    MS-Off Ver
    2016
    Posts
    2,103

    Re: Copy specific columns from an external worksheet to current workbook

    The other alternative would be to replace:

    Please Login or Register  to view this content.
    with:

    Please Login or Register  to view this content.
    The non-VBA users I work with seem to like the 'select' method, because it's easier for them to find and edit the range in the code on their own. More seasoned folks prefer this last method because it's the most efficient (and fastest to run).

  10. #10
    Registered User
    Join Date
    03-01-2015
    Location
    Northants
    MS-Off Ver
    2007
    Posts
    15

    Re: Copy specific columns from an external worksheet to current workbook

    Hi

    Sorry not to have got back to you before, but I have been working on other projects.

    I had a go at my solution of just copying in the whole worksheet and that seemed to work, but thank you very much for your efforts - they are appreciated.

    Regards

+ 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. [SOLVED] Copy a Worksheet from a Closed Workbook to the Current Opened Workbook
    By glennchung in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-28-2014, 12:55 PM
  2. Replies: 3
    Last Post: 12-04-2013, 09:59 PM
  3. VBA code to copy data from specific worksheets from current workbook to a new one
    By Uday2013 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-01-2013, 12:15 PM
  4. Replies: 0
    Last Post: 05-08-2013, 02:47 PM
  5. [SOLVED] Copying data from external workbook into current workbook based on cell value
    By Brontosaurus in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-07-2012, 11:17 AM
  6. [SOLVED] copy from external file, paste in current workbook
    By rodich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-16-2012, 04:29 AM
  7. copy entire worksheet to current workbook
    By AJHWilliams in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 09-16-2011, 08:47 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