+ Reply to Thread
Results 1 to 7 of 7

Just trying to copy a cell from one open workbook to another in a macro

  1. #1
    Registered User
    Join Date
    04-25-2015
    Location
    Pointe au Baril, ON
    MS-Off Ver
    2013
    Posts
    11

    Just trying to copy a cell from one open workbook to another in a macro

    Hi,

    I have two open workbooks - one is a macro-enabled workbook (daily invoice file) created from a template and has a new name each day, and the other is a regular workbook (customer file) with a fixed name. Both are always open during the day. Depending on the customer selected in the invoice, I need to generate the next sequential number from a list, which I can do with formulas. The list is stored in the customer file (Sheet2) because the invoice file is reset each day. My problem is copying the generated number from cell "K4" of the invoice file (and it could be on any of several sheets) to the next available line of Sheet2 (column A) on the customer file, so the next time a new number is needed, it gets the next one. I tried assigning a variable, but since I won't know the filename or sheetname of the invoice file, I didn't know how to do that. I can select the range ("K4") but cannot get it to copy to the last line of the customer file without getting the runtime error '1004' method of range class failed. This is the section of code I've been using (I know it's clunky, but I'm learning as I go):

    Please Login or Register  to view this content.
    Thank you in advance for any help that is offered. It is very much appreciated!

  2. #2
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Just trying to copy a cell from one open workbook to another in a macro

    Try this macro from your customer file:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-25-2015
    Location
    Pointe au Baril, ON
    MS-Off Ver
    2013
    Posts
    11

    Re: Just trying to copy a cell from one open workbook to another in a macro

    Good morning, Thank you so much for the quick response. I still get an error, I think because the code runs from the file with the source data, not the 'copy to' file. Also, I need this all to happen behind the scenes and not require user input.

    The closest I get to having this work is when I assign a variable to the sequence number to avoid using copy and paste. Unfortunately, the variable changes to '0' and replaces my source cell, but it does copy the '0' to the destination list.
    Any thoughts??

    Thank you

  4. #4
    Registered User
    Join Date
    04-25-2015
    Location
    Pointe au Baril, ON
    MS-Off Ver
    2013
    Posts
    11

    Re: Just trying to copy a cell from one open workbook to another in a macro

    Got it! I changed my variable setting to 'As String' and it no longer overwrote the value with zeroes. I don't know why, but it's working. Thank you for your help!

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Just trying to copy a cell from one open workbook to another in a macro

    I need this all to happen behind the scenes and not require user input.
    To ensure this, you must have all the variables already known. As I understand, we know the workbook to copy from, we also know the cell that needs to be copied (K4), but we do not know which worksheet that cell belongs to. So, unless there is a clear concise logical way of deducing the worksheet name, a user input for the same shall be necessary.
    The closest I get to having this work is when I assign a variable to the sequence number to avoid using copy and paste. Unfortunately, the variable changes to '0' and replaces my source cell, but it does copy the '0' to the destination list.
    I'm sorry, but not able to follow this. Are you able to attach sample workbooks and highlight how it should work.

  6. #6
    Registered User
    Join Date
    04-25-2015
    Location
    Pointe au Baril, ON
    MS-Off Ver
    2013
    Posts
    11

    Re: Just trying to copy a cell from one open workbook to another in a macro

    Hi, Thank you so much for your help. Your code that declared variables 'As String' gave me the idea to declare the sequence number as that. I was declaring it as an Integer, and it seemed to take the value in cell K4 and reset it to '0'. But then I move the Dim statement to the top of the subroutine, and used 'As String' and now it works. Just to clarify, the code ran inside the source workbook with cell K4. That was the unknown filename and worksheet name. The known name was the copy destination. It now goes like this:


    Please Login or Register  to view this content.
    Again, thank you for being so diligent and helpful. It's wonderful to know that help is only a few clicks away!

  7. #7
    Valued Forum Contributor
    Join Date
    12-02-2012
    Location
    Melbourne, VIC
    MS-Off Ver
    Excel 2016
    Posts
    750

    Re: Just trying to copy a cell from one open workbook to another in a macro

    Happy to know that you were able to work it out yourself. Good work!

+ 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. Macro VBA to copy and paste cells from one open workbook to another open workbook
    By bloomingcarrot in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2014, 07:55 PM
  2. macro to copy cells(Range) from an open Workbook to another open Workbook.
    By jbester in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-22-2013, 09:47 AM
  3. Macro to open a closed workbook and select copy cells into an open workbook
    By helloganesh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-10-2013, 02:00 PM
  4. Macro, copy UNLOCKED cell range of sheet in open workbook to workbook template
    By NicksDad in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-31-2012, 11:18 AM
  5. [SOLVED] Macro to copy data from open workbook to next open line of new workbook
    By nhtodd in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2012, 09:42 PM
  6. Macro to copy data from other workbook - let user choose which open workbook
    By ssu95bm in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-12-2011, 11:17 AM
  7. Code to open a workbook , copy info and return it to another workbook using a cell v.
    By england79m in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-31-2008, 11:22 AM

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