+ Reply to Thread
Results 1 to 4 of 4

Improving existing macros

  1. #1
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Improving existing macros

    I've produced a few macros now, by a combination of using recording and help from here, which all work ok, but I want to improve their "efficiency" by reducing the amount of activating and selecting during copying between workbooks. I've put together a simple macro just to try and work out the correct syntax, but am struggling to get it right.

    The example has 3 workbooks, code.xls containing the macro, booka.xls containing some source data, and bookb.xls is the workbook to which I want to copy data, all in the same folder:

    Please Login or Register  to view this content.
    This works ok.

    However I want to get to the point where the macro is in booka.xls, and both booka.xls and bookb.xls are already open, with the correct sheets selected before the macro is run, but I can't get the right syntax for this. Obviously the Set wkbkFrom and Set wkbkTo lines fail because the workbooks are already open. I also think it should also be possible to lose the .Copy in the last two lines and use an =

    It's the workbook and worksheet events (I think) that I'm struggling to understand their use.

    If someone could give the correct coding, and maybe explain how it's actually working, I might start to understand it a bit more.

  2. #2
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Improving existing macros

    Hi David,

    See the code below...
    I have modified it to assume that the code exists in BookA so that you can eliminate the workbook called Code.xls
    There is no need to ensure that the correct sheets are selected as the code has exact references to the needed sheets.
    Also, the copy option is better that the "=" options as any formatting will be transferred by the copy (but NOT with the = option)

    Please Login or Register  to view this content.
    Last edited by RudiS; 05-18-2014 at 10:59 AM.
    Regards,
    Rudi

  3. #3
    Valued Forum Contributor Miroslav R.'s Avatar
    Join Date
    05-16-2013
    Location
    NMnV, Slovakia
    MS-Off Ver
    Excel 2007
    Posts
    479

    Re: Improving existing macros

    Hi there
    If the workbooks are open,do not use fullpath,just only wnk name.Then You do not need the wbk.open rows.I rather use wbk name as string and then use it like 'workbooks(mywbkstring)...'.
    The fullpath use only in case the workbooks are not open.

    send from my xperia z via tapatalk
    Regards
    Miroslav R.

    (If You like my solutions, feel free to add reputation.)

  4. #4
    Registered User
    Join Date
    06-03-2010
    Location
    Hertfordshire, England
    MS-Off Ver
    Excel 2010
    Posts
    51

    Re: Improving existing macros

    Thanks RudiS, works ok when I've changed the wksTo and wksFrom references, which you'd got mixed up a little.

    It doesn't work however when I change the Range definitions to Cells(x,y) which was the next step in trying to understand all this.

    e.g.

    wksFrom.Range(Cells(a, b), Cells(a, c)).Copy wksTo.Range(Cells(x, y))

    i.e. not specifying the end Cells reference in the wskTo range, as you can when using the wksTo.Range("B2") method.

    I'll need to rethink this.

+ 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] Improving existing VBA code and removal of unwanted row
    By Narasimharao Nandula in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-03-2013, 09:56 AM
  2. Adding conditions to my existing macros
    By kishoremcp in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-05-2012, 06:20 AM
  3. Replies: 0
    Last Post: 05-25-2011, 09:28 AM
  4. employee planner, improving existing model
    By hole_alex in forum Excel General
    Replies: 1
    Last Post: 04-01-2007, 02:51 PM
  5. [SOLVED] how do i transfer existing excel macros to a new computer?
    By rwr2333 in forum Excel General
    Replies: 2
    Last Post: 07-22-2005, 10:05 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