+ Reply to Thread
Results 1 to 18 of 18

VBA Code To Copy From One Sheet & Paste To Another Sheet

  1. #1
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    VBA Code To Copy From One Sheet & Paste To Another Sheet

    Hi

    I know there have been plenty of threads on this, but just couldn't find exactly what I was after.

    I need to create some VBA code to copy data from an active worksheet to a sheet in a different file. I have 10 macros written which get run on a newly downloaded each day. The macros hide columns and rows of unwanted selections, leaving only selections for the day. I then have some code at the bottom of the macro to highlight and center the selections for manual copying and pasting, but I'd now like to try and automate the process a little more.

    This is the code I have at the bottom of each macro, just before the End With and End Sub lines.

    Please Login or Register  to view this content.
    This next code is my full macro and the 2 red lines are what I copied from an existing thread here on this forum. I have tried to tweak them to meet my needs, but it doesn't appear they are correct. I wonder if someone might pass their eye over them to see what is wrong. The code works uo to those 2 red lines. Those lines should copy the data from A2 to the last visible cell in BR and paste it into the first available cell in column A of the file New Results File.xlsm and the sheet Safe Bets

    Please Login or Register  to view this content.
    Thanks so much in advance

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Remember that destination workbook needs to be open otherwise you will have to add code to open the destination workbook prior to pasting the data.

    Also you don't need to select each column to hide it.
    Please Login or Register  to view this content.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Quote Originally Posted by AlphaFrog View Post
    Please Login or Register  to view this content.
    Thanks so much for your reply AlphaFrog

    Unfortunately when I change the code to the above and run it, I am presented with a Run-time error '9': Subscript out of range. The line it takes me to when debugging is the Workbooks line. I am wondering whether the fact that it is opened via Dropbox has anything to do with it. There a handful of people in the team and all have access to the majority of files, so they are centrally stored and accessed via Dropbox. The New Results File is open when I run the macro, so not sure what is causing this.

    Any thoughts at all?

    Regards

  5. #5
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Is the file open when you run the code?

    Add the file extension to the name.

    Workbooks("New Results File.xlsm")

  6. #6
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    As I indicated above, the file is open when I run the code and adding the extension made no difference. I had already tried that prior to my last post. I even tried saving the file locally instead of opening it from the central Dropbox location, but it did not change anything except now there is no error message popping up, just the debugger showing the Workbooks line highlighted.

    Here is the new VBA code with the columns now changed to your suggested wording rather than using Selection

    Please Login or Register  to view this content.
    Any idea why that is still not running correctly?

    cheers

  7. #7
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    This has to be one line of code

    Please Login or Register  to view this content.

    Like this...
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Same result unfortunately.

    Would the fact that is it Excel for Mac affect it at all? I know that with some VBA code I needed in an earlier post, something needed to be changed slightly based on that
    Last edited by honkin; 02-19-2020 at 08:29 PM.

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Is the New Results File.xlsm workbook opened in the same instance of Excel?

    As a test, try this...

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    cheers AlphaFrog. That has fixed the issue. All 10 macros now work fine, except when there is nothing to copy, then the macros copy and paste the header row. In the end, I guess my code from earlier which selects everything bar the header row and then centers it for manual copying and pasting, doesn't help here now. Now that copying and pasting is not a manual thing, it is probably best to either hide the header row instead, or have something say if the cells below it are empty, do nothing.

    I think I can add code to hide the header row easily enough (Rows("1:1").EntireRow.Hidden = True) but I am not quite sure how to change the line below Dim LR As Long and the line Range ("A" & Rows.Count) etc.

    Thanks so much for your help to date, as it is so close now

  11. #11
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    It's uncommon to need to select or activate objects.

    Can you try this for me.
    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    ah bakerman2

    That is absolutely ideal. I have now altered it so it suits all 10 macros, as some had different columns to hide and used different filters, but it works perfectly, even when there is nothing to be copied

    Thanks so much for your effort

    Regards

  13. #13
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    One quick question re adapting this for something which requires no filters at all.

    I deleted all of the code to do with filters and hiding columns, but kept the line on centering, as still need to do that. I need this to paste into the next available row in column B, so changed the Range to be B instead of A

    Please Login or Register  to view this content.
    When I run this as a macro I get a run-time error 91 message. The line it highlights on debugging is the .Autofilter line. The error message says Object variable or block variable not set, yet all that has been removed are the lines concerning filters to be run and the columns to be hidden. The rest is identical

    Any thoughts on what is stopping it from running now?

    cheers

  14. #14
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    If I understand correctly this should do. It copies all cells except header row to new file.

    Please Login or Register  to view this content.
    Last edited by bakerman2; 02-20-2020 at 05:28 AM. Reason: Missed End With

  15. #15
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    cheers bakerman2. It was missing an End With, so added that second one it all appears to be fine.

    Again thanks so much for your time. I was close. I can see what you did removing the End With after HorizontalAlignment and dumping .AutoFilter.Range before the offset.

    Cheers mate

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    Ooops, Sorry Edited my previous post.

    Glad to help.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.

  17. #17
    Registered User
    Join Date
    03-26-2012
    Location
    Melbourne, Australia
    MS-Off Ver
    Excel 16.34 (20020900)
    Posts
    23

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    cheers mate. Have now marked this as solved and clicked the star on various answers.

    Thanks again

  18. #18
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,271

    Re: VBA Code To Copy From One Sheet & Paste To Another Sheet

    You're welcome and thanks for rep+.

+ 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. Code to copy and paste values on a sheet to another sheet
    By Forum2000s in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-07-2017, 09:33 AM
  2. [SOLVED] VB Code Copy and Paste in different sheet
    By rizmomin in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-09-2015, 03:09 PM
  3. VBA Code to Copy entire sheet and paste in another sheet
    By molly13 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-28-2015, 11:45 AM
  4. VBA code to search from different sheet > Copy the row > Paste on different sheet.
    By qpywsqp in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-26-2013, 11:13 AM
  5. Replies: 1
    Last Post: 03-28-2013, 02:49 PM
  6. [SOLVED] Copy and paste data from sheet 2 to sheet 1 based on specific criteria on sheet 1
    By VBADUD in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-05-2012, 04:18 AM
  7. Sheet code to copy & paste a new row of Info to another sheet
    By matrixpom in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-23-2012, 05:31 PM

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