+ Reply to Thread
Results 1 to 3 of 3

VBA Code to open sourc workbook, copy columns, past to target workbook, and close source.

  1. #1
    Registered User
    Join Date
    10-04-2011
    Location
    Vancouver, BC
    MS-Off Ver
    Excel 2003
    Posts
    5

    VBA Code to open sourc workbook, copy columns, past to target workbook, and close source.

    Still pretty new to VBA, I have tried many different methods with no success. I am looking to click a button, with a macro assigned that will use Application.GetOpenFileName to choose a source file, open it, copy some columns (F for example), paste them to the current (target) workbook, and then close the source workbook. Does anyone have any suggestions? I am getting runtime error 9. Thanks. Here is my current code:



    Sub Macro2()

    'Assign variable name to Target workbook
    Var1 = ActiveWorkbook.Name
    'Assign variable name to Target range
    Var1R = "H1"

    'Open Source WorkBook
    sPath = Application.GetOpenFilename
    Workbooks.Open Filename:=sPath

    'Assign variable name to Source workbook
    Var2 = ActiveWorkbook.Name
    Var2R = "WORKSHEET-1"

    'Copy from Source to Target
    Sheets(Var2R).Columns("F").EntireColumn.Copy _
    Destination:=Workbooks(Var1).Sheets("Sheet1").Range(Var1R)

    'Close Source WorkBook wo/Save
    Workbooks(Var2).Close False


    End Sub

  2. #2
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: VBA Code to open sourc workbook, copy columns, past to target workbook, and close sour

    Try this,
    Please Login or Register  to view this content.

  3. #3
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,288

    Re: VBA Code to open sourc workbook, copy columns, past to target workbook, and close sour

    @mattm99

    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.

    Click on Edit to open your thread, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (This thread should receive no further responses until this moderation request is fulfilled, as per Forum Rule 7)
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

+ 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. Copy from first workbook, close open new workbook paste close
    By johncarr1952 in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 08-04-2021, 08:50 AM
  2. [SOLVED] Issue with Open, copy, close Macro - when trying to close workbook Clipboard error
    By seanpcorbett1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-13-2016, 05:14 PM
  3. [SOLVED] Macro to find data in source workbook and copy paste to target workbook
    By D.Lovell in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 04-23-2014, 06:21 AM
  4. Replies: 1
    Last Post: 12-26-2013, 04:55 AM
  5. copy text from a source workbook to target workbook
    By JMJ123 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-02-2013, 10:24 AM
  6. Replies: 2
    Last Post: 09-11-2012, 09:42 AM
  7. Save an open workbook, then open template workbook and close the saved workbook
    By ondvirg in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2009, 10:20 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