+ Reply to Thread
Results 1 to 15 of 15

Code to paste values from different workbook

  1. #1
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Code to paste values from different workbook

    Hello.
    I've been doing quite a bit of searching for the following issue but i cannot seem to figure out the issue.
    Normally when a code copies a cell value from Workbook-A, closes Workbook-A and then pastes the value into Workbook-B it works flawlessly. However, i noticed that this doesn't work in all case, for example when a code repeats itself with the "For - Next" function then when the code copies from one workbook to the other it causes an error.
    here is my code and i made the part of the code that is red is where the issue lies.
    Please Login or Register  to view this content.
    Last edited by kosherboy; 06-12-2014 at 02:31 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    First things first, you should indent loops, ifs, and withs so that you can read your code better. Like this
    Please Login or Register  to view this content.
    Second, I'm glad you have been working hard at figuring this out. We could use more of that around here. At the end of the day, what's important isn't that you get one task over with, but that you develop a skill and learn for the future. That said, it would probably be both easier for us and more educational for you, if you posted a sample workbook(s), with an example of the result you wanted, and then let one of us come up with a solution. My first question on here, I asked for help with code, someone insisted I post a sample workbook, and then they came back with code that looked alien to me, but worked wonderfully. It took me a while to walk through their code, google pieces, try running pieces, and figure out how they did it, but I'm really glad that I had that example, because I code much better for it.
    Last edited by k64; 06-16-2014 at 02:25 PM.
    1. Include a sample workbook with an example of the result you want
    2. Use [CODE] and [FORMULA] wrappers for your VBA code or excel formulas
    3. If your question has been answered, mark the thread as SOLVED and click on the "Add Rep" star to thank whoever helped you.

  3. #3
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    Hi k64.
    It occurred to me that there might be an alternative solution to this issue.
    What if i were to "DIM C as long" and make C = Range("H16") of the workbook the code will create?
    Maybe the code can somehow refer to this workbook that hasn't been created yet?
    I don't know, what do you think?
    And like this there won't (to my humble opinion) be any issues with pasting, see modifications:
    Please Login or Register  to view this content.

  4. #4
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    k64 i don't know how i did it but i was able to extract just the issue and start a brand new workbook with a completely restructured code!
    You are right, it is easier even for me to focus on the problem as now the workbook is not so complicated. Here is the workbook rCell.xlsm
    In order for the code to actually execute you have to select the green cell

    Please Login or Register  to view this content.
    Attached Files Attached Files

  5. #5
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    Oh, okay. I see where you're hitting the error. Can you explain what the eventual goal is? This is doing something for a single row. Are you not going to use the other rows? Are you going to have a new workbook created for each row? There are a couple ways to fix your error, but what's best depends on what your ultimate goal is.

  6. #6
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    Yes! Precisely what I am trying to accomplish. I would like to create a new workbook for each row. Remember that this is not my real workbook...

  7. #7
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    I'm curious. Can you tell me why you want to do that?

    Sent from my SPH-D710 using Tapatalk

  8. #8
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    Good question. In my real workbook I have a customer database with the customers file path. I also have a code that can make copy of a one of the tabs called "invoice templates" (oh, and before it creates a copy it fills in yhe feilds all the right information pertaining to that customer via lookup) then saves the workbook in the correct file path of the customer. So if I have 3 customers that I would need to create invoices I would normally have to do each one manually. If this code I posted accomplishes what I want then I would incorporate the code into my official workbook and have it simultaneously create an invoice for each of these 3 customers who all have one thing in common, they shar e the same PO number.

  9. #9
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    Ok that makes sense. And I'm guessing you want the invoices to be excel files, not pdfs? Have a good sabbath

    Sent from my SPH-D710 using Tapatalk

  10. #10
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    Hi!
    Yes, the invoices to be in excel.
    Also, i was in a rush with my last post and i didn't mention the most important part of what the ultimate result of the code should be.
    The cell in the copied workbook that reads "THIS CELL WILL GET COPIED", so in my real workbook this cell is actually the address of the copied workbook and so i need this cell copied and pasted into the original workbook where the code originated from and to be used as a hyperlink to the copied workbook (via further coding which is not the focal point here). That's why this cell is so important that it gets pasted properly.
    (I can post a workbook where this system actually works flawlessly and i know i am repeating myself but i believe the reason why the code in this thread doesn't work is because the code gets repeated. In my "flawless" workbook the code doesn't get repeated.)

  11. #11
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    Here kosherboy, try this code. You'll have to change the file path.
    Please Login or Register  to view this content.
    Last edited by k64; 06-16-2014 at 02:23 PM. Reason: Copy only relevant data

  12. #12
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    Hi k64.
    The code definitely took care of the pasting issue and i appreciate your efforts, however, something in the outcome of the code changed, instead of the code repeating itself for all cells that have the same "PO" numbers it repeated itself throughout the whole range!

  13. #13
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    Yeah, maybe I'm confused about what you want. I thought you wanted to make one invoice for each PO No. with all the items for that PO No. Are you saying you want a separate spreadsheet for every item for every PO No? Or do you only want it to work for one PO No?

  14. #14
    Valued Forum Contributor
    Join Date
    11-27-2013
    Location
    Brooklyn, NY
    MS-Off Ver
    Office 365
    Posts
    1,172

    Re: Code to paste values from different workbook

    I took some time to play around with my original code and guess what? I DID IT!!
    It works! I decided not to use "With - End With" functions and i just activated the cell. Don't ask me why this worked but it did! I marked in blue the part of the code which i switched to active cell.
    I would like to thank you for all your efforts and help on this.

    Please Login or Register  to view this content.

  15. #15
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: Code to paste values from different workbook

    You are welcome. Glad you could make it work! BTW, conventional coding wisdom says to avoid using ActiveCell and Select. They are okay for small operations, but there are too many ways that things can go wrong with them. Most of the time, instead of using ActiveCell, you can use a variable to track where you are, so that the user clicking somewhere won't mess things up. You can also avoid most .Select statements, and make your code more efficient by elimintating the trip to the clipboard and back when copying. For example, you can make this section of the code cleaner and faster.
    Please Login or Register  to view this content.
    by writing it like this:
    Please Login or Register  to view this content.
    Or this code
    Please Login or Register  to view this content.
    Can be written as this
    Please Login or Register  to view this content.
    Glad that things worked out for you. Let me know if you want any more help
    Last edited by k64; 06-16-2014 at 04:41 PM.

+ 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] Code to cut cell from one workbook, close same workbook, and paste in different workbook
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 03-13-2014, 04:01 PM
  2. [SOLVED] Code to copy data from a closed workbook and paste in active workbook using named range.
    By paullie1912 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-28-2014, 02:38 AM
  3. [SOLVED] Changing Current Code from Paste Formulas to Paste Values
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-24-2013, 12:33 PM
  4. VBA code to open new workbook and copy and paste special values
    By oddinho2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-12-2013, 11:24 PM
  5. Code to paste values and save workbook with another name, extension
    By madaboutgolf in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-14-2011, 05: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