Results 1 to 6 of 6

Thread: Copying cells from one workbook to another

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Suffolk, UK
    Posts
    12

    Resolved Copying cells from one workbook to another

    Ok, I have two files, one an excel spreadsheet, the other a text file. I open both in excel and want to copy cells from the text file to the spreadsheet using VBA. The code I have used is as follows:

    VB Code:
    1. Workbooks("conversion.xls").Sheets("Bintext3").Range("A1:DX64") = Workbooks("conversion.txt").Sheets(1).Range("A1:DX64")

    But nothing happens! No error is returned but then again nothing is copied - I'm guessing it must be something to do with the Workbooks property???
    Last edited by Alib; Apr 21st, 2005 at 08:41 AM.

  2. #2
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Copying cells from one workbook to another

    You would need to do a copy and paste.

    record the macro using Excel, and it will provide the code for you.

    e.g.

    open excel
    start macro recording
    open text file
    copy text file
    paste into excel
    stop recording
    if you fail to plan, you plan to fail

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Suffolk, UK
    Posts
    12

    Re: Copying cells from one workbook to another

    Are you sure that's the only way? I'd prefer not to move anything onto the clipboard, unless there's a way of removing it afterwards? but thanks anyway

  4. #4
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Copying cells from one workbook to another

    the other way would be to assign all of the items in the text file to variables, before writing to the excel sheet.

    e.g. an array...
    if you fail to plan, you plan to fail

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Suffolk, UK
    Posts
    12

    Smile Re: Copying cells from one workbook to another

    Hmm...I think I'll stick with the copy and paste method.

    Thanks very much!

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2005
    Location
    Suffolk, UK
    Posts
    12

    Resolved Re: Copying cells from one workbook to another

    Incidentally, just in case anyone else reads this thread, I have since found out that the following code works:

    VB Code:
    1. ThisWorkbook.Sheets("Sheet1").Range("A1:D10").Value = Workbooks("New Text Document.txt").Sheets(1).Range("A1:D10").Value

    The mistake I was making before was assuming that ".Value" was assumed. If ".Value" is omitted then it doesn't work!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width