+ Reply to Thread
Results 1 to 15 of 15

Pasting via recorded macro problem.

  1. #1
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Pasting via recorded macro problem.

    I tried to record a macro copying some text from one workbook's worksheet to another workbook.
    If the text happens to have already been copied into memory, it works. But if I copy some other text then try to run the macro, it will paste whatever I've copied before and not the specified cell.

    Please Login or Register  to view this content.



    I thought maybe adding ActiveSheet.Copy after the line: Range("T8:W8").Select
    but then it just pastes the text ActiveSheet.Paste into the cell B5. I want to copy the data only from T8:W8.
    Can someone please explain what needs correcting? Does copy/paste work with macros in Excel?
    Last edited by foolios; 07-27-2011 at 07:32 PM. Reason: SOLVED

  2. #2
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    Hi foolios and welcome to the forum. Try the code now with cutcopymode equaling false.
    Please Login or Register  to view this content.
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  3. #3
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Pasting via recorded macro problem.

    Can you show me where you looked up cutcopymode? I am trying to find ways of working with excel at: http://msdn.microsoft.com/en-us/library/ff846392.aspx
    the reference library for Excel, but it is very difficult to wade thru.

    By the way, I get error 1004
    Paste method of worksheet failed.

    When I hit debug, error at line:
    ActiveSheet.Paste

    Only if I have the text Electric already copied to memory will it paste.

    P.S. I found it at: http://msdn.microsoft.com/en-us/library/ff839532.aspx
    Is that where you go looking for explanations?
    Last edited by foolios; 07-26-2011 at 08:43 PM.

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    Also, is the following correct?
    Please Login or Register  to view this content.

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    I have learned about cutcopymode through this forum. I just Googled it and here is a good webpage to look at. If you are up to learning how to write good macros, keep coming to this site with your problems and queries. A good starting habit to learn is to avoid using .Select and .Activate. Using them tends to slow the Run-Time dramatically, especially with large procedures. Give me a moment and I'll post a short procedure without .Select or Activate.

  6. #6
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    I was going to try and work with your code but I really don't know what you are trying to accomplish with it. Sorry, perhaps my mind is not doing what it should be doing. Are you wanting to copy the range of cells T8:W8 into B5?

  7. #7
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Pasting via recorded macro problem.

    Thanks so much in advance!


    It should have read: Windows("COPY_B.csv").Activate

    I added: Selection.Copy after

    Range("T8:W8").Select
    Selection.Copy
    And now it does paste well but it also pastes the formatting of the cell of which I don't want copied.

    EDIT:
    Ok, I found out that the problem was in that the macro was in the same workbook. So, I moved the macro to my personal workbook for macros.
    It works perfectly when run as a macro.
    But now the problem is that when I try to assign the macro to a button, it errors out.
    Sub or function not defined.
    When I had the macro in the same workbook I could use Macro2 in the sub but now that isn't working.
    So I tried PERSONAL.XLSB!Macro2 and that doesn't work either.
    How do I call the macro now?

    Private Sub CommandButton1_Click()
    Macro2
    End Sub
    Last edited by foolios; 07-26-2011 at 09:31 PM.

  8. #8
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    Can you upload a mock workbook with data that is not sensitive or private? This way I can look at where you problems may be.

  9. #9
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Pasting via recorded macro problem.

    Will do. Just a moment.

  10. #10
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    See if this somehow fits what you are trying to accomplish.
    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Pasting via recorded macro problem.

    I will try this.
    I had pm'd you the links to the sample workbooks you asked for.
    I was using the macro feature to accomplish the task.
    I will try to beak down your code and see what I can learn. EDIT: Nm, it's commented. Thanks for that!
    I will post back. It may be tomorrow night tho.
    Thanks for the help thus far.

  12. #12
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    Hi foolios,
    See the attached workbooks. The button will take data from book 5 and bring it to book 6. However, the data coming from book 5 is based on your range T8:W8 (or T8:AXY8, the column count doesn't matter). Look over the code and fit it to your needs. If you need help, ask the forum.

    Cheers
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    07-26-2011
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    13

    Re: Pasting via recorded macro problem.

    That is very nice. I added an extra cell's worth of copying to make sure I am following it correctly. I am going to post it for your review.

    What does Debug.Print do? When I right-click on it for definition, it doesn't come up.

    I am realizing that it would be very useful to add something else to this workbook.
    Should I ask my question here in this thread or post a new one?
    Attached Files Attached Files

  14. #14
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    Hi foolios,

    I'll take a look at your workbooks in a moment.

    Debug.Print can be commented out or deleted. What it does is enables you to view Range address, values, and and other goodies in the Immediate window the the VBE. The Immediate window is at the bottom of the VBE screen. If not enabled on your screen, press Ctrl + G and it will show up. It is very useful to use before placing values in the spreadsheet. However, once your code is doing what you want it to do, you should comment them out or delete them because they will still print in your Immediate window, even if your VBE is closed. Hopefully this makes sense!

    As for another question, if it is different than this one, mark this as solved and then start a new thread. It helps with regards to search engines.

  15. #15
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Pasting via recorded macro problem.

    Hi foolios, I have been having problems with saving you files to actually run the code. I looked at the code and I think everything looks good. If it meets your requirements then I would say that you are good to go. Let me know if there is anything else.

    PS. Don't forget to delete or comment out the debugs.

    Michael

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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