Thursday, March 4, 2010

Excel VBA : Deploy Macros Found Elsewhere

Today I am sharing various ways to add macros found elsewhere to a Excel file where that macro is to be deployed. Will be covering 6 ways to add macro code to your excel file.

1. Excel VBA : Add code to a Regular Module
2. Excel VBA : Add code to WorkBook Module
3. Excel VBA : Add code to Worksheet Module
4. Excel VBA : Copy Module from Existing Workbook
5. Excel VBA : Copy Forms from Existing Workbook
6. Excel VBA : Import code from .bas file

Excel VBA : Add code to a Regular Module

-Copy code from the source
-Open the workbook where you need to deploy this code
-Press ALT+F11 (Keep ALT key pressed while pressing F11 key)
-Click Insert > Module (Shortcut ALT+I+M)
-Paste macro code Click Edit > Paste (Shortcut CTRL+V) in right window where cursor is

See screencast below for Alternate method using right click menu through mouse.
Excel VBA : Add code to a Regular Module

Excel VBA : Add code to WorkBook Module

Some macros are event based macros and get activated as soon as particular event happens. Events which are universally applicable to the entire workbook are called workbook events. These macros will run once a particular event happens in a particular workbook. Typical example of such macros are Auto Open / Auto Close macros

-Copy code from the source
-Open the workbook where you need to deploy this
-Press ALT+F11 (Keep ALT key pressed while pressing F11 key)
-Double Click on ThisWorkbook object
-Paste macro code Click Edit > Paste (Shortcut CTRL+V) in right window where cursor is

See screencast below
Excel VBA : Add code to WorkBook Module

Excel VBA : Add code to Worksheet Module

Some of event based macros get activated as soon as particular event happens on a particular worksheet within the workbook. These are the macros which will run on a event in particular worksheet. Typical example of such macros are Worksheet Select / Worksheet Change macros

-Copy code from the source
-Open the workbook where you need to deploy this
-Press ALT+F11 (Keep ALT key pressed while pressing F11 key)
-Double Click on Worksheet object (Choose particular worksheet where you want to deploy the code)
-Paste macro code Click Edit > Paste (Shortcut CTRL+V) in right window where cursor is

See screencast below
Excel VBA : Add code to Worksheet Module

Excel VBA : Copy Module from Existing Workbook

You may have the code in existing workbook (Source) with you or recently downloaded from Internet. Following steps will deploy it to new workbook (Target)
-Open both the Workbooks (Source and Target)
-Press ALT+F11 (Keep ALT key pressed while pressing F11 key)
-In Left window of project explorer find Source workbook
-Select the source module
-Drag it to the Target workbook

See screencast below
Excel VBA : Copy Module from Existing Workbook

Excel VBA : Copy Forms from Existing Workbook

Steps and requirement is similar to copy code from existing workbook

-Open both the Workbooks (Source and Target)
-Press ALT+F11 (Keep ALT key pressed while pressing F11 key)
-In Left window of project explorer find Source workbook
-Select the Form to be copied
-Drag it to the Target workbook

See screencast below
Excel VBA : Copy Forms from Existing Workbook

Excel VBA : Import code from .bas file

Some time you may get code in downloadable file with extention BAS. This is visual basic file .bas , you can import this to your workbook.

-Open your Workbook
-Press ALT+F11 (Keep ALT key pressed while pressing F11 key)
-In Left window of project explorer find Source workbook
-Click File > Import (CTRL+M)
-Choose downloaded VB file (*.BAS , *.FRM, *.CLS)
-Click Open

See screencast below
Excel VBA : Import code from .bas file

28 comments:

  1. Excellent Job... but how do you do it on Excel for Mac? regards, Amin. My email: amintera@gmail.com
    ReplyDelete
  2. Hi Sir,
    This is jacintha. Excellent works done. but i have not able convert numbers into words. kindly help me ASAP. my email id is Jacintham@shoppersstop.com
    ReplyDelete
  3. Hi Sir,

    i have visited your site & read all the procedures for converting numbers in to words in excel.

    But i have failed to do it on my own, even after the following procedures.

    Kindly help me in this.

    My Email ID - Vishwasrao.vishal@gmail.com
    or vsvishwasrao@gmail.com

    Vishal
    ReplyDelete
  4. Sir,
    How can we make this function to work in all worksheets of excel.
    ReplyDelete
  5. i am not able to do converting number to words can u please help me my mail id is navagevinay@gmail.com
    ReplyDelete
  6. dear sir,

    How can we make this function to work in all worksheets of excel.

    kindly help me ASAP.
    mukesh.dabhani.4u@gmail.com
    ReplyDelete
  7. i am not able to do converting number to words can u please help me my mail id is
    vijay.chaudhari@ibibo.com
    ReplyDelete
  8. i am not able to do converting number to words can u please help me my mail id is :
    cute.miao@hotmail.com
    ReplyDelete
  9. There appears to be some issue with 64bit Excel... upon loading up the downloaded example I'm presented with a Compile Error...

    "The code in this project must be updates for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute."
    ReplyDelete
  10. Hi sir,

    its best......its working......but in some numbers its not correct..for example--- 100,250.00
    convert in -Ruppees One Lakh Thousand Two Hundreds Fifty Only.

    plz help me on this error....
    my id is---momin.rauf@gmail.com
    ReplyDelete
  11. Thank you very much.
    Now i want to conver Rs.100000(One lakh only)
    how can i?
    ReplyDelete
  12. hi,

    i read all web article ,but i failed to convert digits into words. so how can we do this. i follow each step & information given in this web but unfortunately i failed.
    Any body help me plz e-mail me, my email address as given below:-
    gulfraz_tcs@hotmail.com
    Any body help me... plz ... plz... awaiting for response.
    ReplyDelete
  13. hi sir,

    i'm working in Govt. sector in A.P. thank u for helping us in excel. i have tried Ur VB module code to convert number to currency but unable to run that. so please send me the excel file with that function to convert number to currency.

    thank you!
    ReplyDelete
  14. Hi sir,

    Realy good and faboulas am using well
    ReplyDelete
  15. How can we add spellcurr function without adding as it as a macro. As there is a sum function , can it be aailable to all excel work books
    ReplyDelete
  16. i am not able to do converting number to words IN MS OFFICE 2003

    can u please help me my mail id is

    SERVICEADMIN@TEJASWIMOTORS.NET
    ReplyDelete
  17. dear sir
    this is excellent, but i do for one time,
    i want to do this any time when i open excel worksheet but it failed to run this formula if i do this job again that time is OK. please give me a solution my mailid is narotttamsaha2000@gmail.com
    I waiting for your reply
    Thanks,
    ReplyDelete
  18. when i enter whole in module there is problem with please sathya.patro@gmail.com.

    please send the programme step by step.

    pls...............
    regards,
    Sathya
    ReplyDelete
  19. Its really helpful thanks..
    ReplyDelete
  20. hi yogesh
    m using spell currency but every time i open the particular file its shows #NAME? error and again i have to repeat it the same process to copy the module and paste it but yeah 1 thing it prompts me 1 message which read:- the following features cannot be save in macro-free workbooks :VB project
    to save file with these features click no and the choose a macro -enabled file type in file type list
    to continue saving as a macro - free workbook click yes"

    Please guide for the same and enable
    ReplyDelete
  21. Its really good, but it is giving value in millions not in lacs
    ReplyDelete
  22. Its really good, but it is giving value in millions not in lacs
    ReplyDelete
  23. hi, after using VB it is working when the sheet is opened, and it is not working after the Excel Book is closed and reopened.
    Plese guide me how to get this permanent
    ReplyDelete
  24. sir i cant convert number above 1 lakh its nt working for amount above one lakh
    ReplyDelete
  25. sir, thanks!
    this is a excellent job.
    ReplyDelete
  26. namaste sir,
    I wish to add this code permanent in ms excel.
    how we can do it?
    my email id is sikander.mca@gmail.com
    ReplyDelete