+ Reply to Thread
Results 1 to 24 of 24

Copy/Paste Special Macro

  1. #1
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Post Copy/Paste Special Macro

    I have created a macro to copy data that is inserted into one workheet into a different worksheet using the pastespecial command to only insert the values. The macro also takes into account if there is already data in that column (cells in that column); if so, it then moves one column to right and checks it for data.

    My problem is this; I have had it working before (i believe when i was using 2003) but now it is not working. It appears to copy the data correctly and check the columns on the other worksheet but it never pastes the data. Also, if I try to run it more than once, I receive the ever annoying message of "Run-time error '1004': Method Range' of object '_Global' failed."

    Like I said, I've had this working before, but I wouldn't think that 2007 would cause it to not work because I had other people using 2007 run it before. Below is the macro, and yes just to confirm, there is a sheet called %s. Thanks in advance for any help.
    Please Login or Register  to view this content.
    Last edited by dwnocturnal; 11-12-2010 at 05:45 PM. Reason: Solved

  2. #2
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    I just wondered something; is it a problem with paste special in 2007? Could a paste resolve this?

    Eh, anyways, it still wouldn't resolve the 1004 error message. Thinking out loud here.

    Edit - No, the paste still copies the formula from the %s sheet, which means the values change and I need them to remain constant on the Peak Statistics sheet once pasted.
    Last edited by dwnocturnal; 11-12-2010 at 08:43 AM.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    So in words, each time the macro runs it copies A1:A49 from sheet "% s" into the next empty column of Peak Statistics starting at row 9 pasting as values only?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    Assuming that is correct, try this:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    So in words, each time the macro runs it copies A1:A49 from sheet "% s" into the next empty column of Peak Statistics starting at row 9 pasting as values only?
    Correct. I don't think this poses a problem, but there are also "spacer cells" (empty cells) on sheet "%s".

    I tried your code, same result. I verified that it did in fact copy the data as I could paste it after running the macro, but the macro itself does not paste anything. Below is an example of what it "should" paste from cells A1:A49 from sheet "%s":

    0%
    0%
    0%

    49%
    46%
    46%


    464230
    485672
    485230

    0
    0
    0
    0
    0
    0
    0
    1
    712
    868
    541
    164
    146
    57
    125
    100


    1.2%
    4.2%
    0.6%
    0.6%
    0.0%
    3.9%
    2.6%
    0.9%
    1.3%


    4.0%
    4.4%
    9.3%
    1.5%
    0.0%
    0.1%
    0.0%

    Thanks for the help here.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy/Paste Special Macro

    or
    Please Login or Register  to view this content.



  7. #7
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Trying that code, I receive the following error:

    "Subscript out of range"

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook. Let's look at this together.

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy/Paste Special Macro

    Check the sheetnames:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Click GO ADVANCED and use the paperclip icon to post up a desensitized copy of your workbook. Let's look at this together.
    Sorry, not sure what you mean here.

    Tried the latest code, no error this time but not data shows on the Peak Statistics sheet in the columns after running.

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy/Paste Special Macro

    Please Login or Register  to view this content.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    Quote Originally Posted by dwnocturnal View Post
    Sorry, not sure what you mean here.

    Tried the latest code, no error this time but not data shows on the Peak Statistics sheet in the columns after running.
    Maybe there's some stray garbage in row 9. Look way off to the right, see if it's pasting the data into columns you're not checking.

  13. #13
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Maybe there's some stray garbage in row 9. Look way off to the right, see if it's pasting the data into columns you're not checking.
    I double checked, nothing there unfortunately. Something weird though, I did a search on everything in the workbook for a 0% and it did not return the 0%'s in the "%s" sheet...

    If it would make it easier, I can email you a copy of the spreadsheet with my macros?

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    In your next post, Click the GO ADVANCED button and use the paperclip icon to post up a desensitized copy of your workbook.

  15. #15
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Alright, here is the xls with the macros included (I renamed your sub to PasteSpecial).

    If you were to go through the whole process, this is what I do:

    1. Open xls, go to Web Query sheet
    2. alt+f8 to open macros (or ctrl+q to run macro), run Sheet3.WebQueryMacro
    3. Sit back and let it do it's thing.

    The WebQueryMacro basically goes out and pulls the stats from internal sites on statistics every hour (manually run the macro every hour) and copies data from specified cells in the Web Query sheet to the %s sheet. It then calls the PasteSpecial macro which, as you know, tries to take the values on %s sheet and paste them into Peak Statistics.

    Note - If you run the WebQueryMacro, it is going to delete what is currently there (it wipes the sheet before running) because you will not be able to contact my local sites. I mainly included it so you could check it out if need be.
    While I have been testing this with you, I have just run the PasteSpecial macro from the %s sheet.

    Sorry if this complicates things; the only thing I am having trouble with is pasting the data from %s to Peak Statistics, everything else is beautiful.

    Thanks again for the help.
    Attached Files Attached Files

  16. #16
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy/Paste Special Macro

    Activating my code in your spreadsheet for the first time it populates column B in sheet "Peak..."; the second time it populates column C in that sheet. So it does exactly what is designed for.
    Did you have a good look in worksheet "Peak statistics' ?

    I hope you realise that refreshing a Webquery doesn't trigger an event in the workbook.
    Last edited by snb; 11-12-2010 at 01:38 PM.

  17. #17
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    ......

    After your post, I tried it from another PC and it does work fine...whenever I run it from my pc, I get no data in the Peak Statistics sheet....I'll look into this pc and see what is going on...

    I hope you realise that refreshing a Webquery doesn't trigger an event in the workbook.
    Every time I run the WebQuery Macro, it should access the site listed and copy the information from that site into the Web Query sheet. This data can update constantly on the site. I know that it does not update what I see in the sheet until I query the site again.

  18. #18
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy/Paste Special Macro

    When refresheing it takes time to load the data. If the macro to copy the result starts too early, nothing will be copied. The slower your PC, the more likely nothing will be copied.

  19. #19
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Gotcha. Is there a way to allow the macro to "pause" before calling the PasteSpecial sub?

  20. #20
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    Every time I run my macro, it adds the stats to the next column properly formatted.
    Attached Files Attached Files

  21. #21
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Sorry, my last post was a little confusing. You are correct, I can get your Macro to paste the data every time. My question to that is; is there something different I should do to the Sheet3.WebQueryMacro to get it to call that macro? That way once the query is finished it will execute the next macro as well.

  22. #22
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    It's never a good idea to name a function or macro with the exact same name as an Excel function. Try renaming it something else. Since it's in a module, you should be able to simply issue the call command.
    Please Login or Register  to view this content.

  23. #23
    Registered User
    Join Date
    11-12-2010
    Location
    Kentucky
    MS-Off Ver
    Excel 2007
    Posts
    11

    Re: Copy/Paste Special Macro

    Alright. Thanks to both of you for the help; been great! I'd consider this issue resolved.

  24. #24
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy/Paste Special Macro

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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