Meriv çawa pêveka xwe ya ji bo Microsoft Excel-ê biafirîne

Even if you do not know how to program, there are many places (books, websites, forums) where you can find ready-made VBA macro code for a huge number of typical tasks in Excel. In my experience, most users sooner or later collect their personal collection of macros to automate routine processes, whether it’s translating formulas into values, displaying sums in words, or summing cells by color. And here the problem arises – the macro code in Visual Basic needs to be stored somewhere in order to be used later in work.

The easiest option is to save the macro code directly in the working file by going to the Visual Basic editor using the keyboard shortcut alt+F11 and adding a new empty module via the menu Têxe - Modul:

However, there are several disadvantages with this method:

  • If there are a lot of working files, and a macro is needed everywhere, such as a macro for converting formulas into values, then you will have to copy the code in every book.
  • Must not be forgotten save file in macro-enabled format (xlsm) or in binary book format (xlsb).
  • When opening such a file macro protection will each time issue a warning that needs to be acknowledged (well, or disable protection completely, which may not always be desirable).

A more elegant solution would be to create your own add-in (Excel Add-in) – a separate file of a special format (xlam) containing all your “favorite” macros. The advantages of this approach:

  • It will be enough connect add-on once in Excel – and you can use its VBA procedures and functions in any file on this computer. Resaving your working files in xlsm- and xlsb-formats, thus, is not required, because. the source code will not be stored in them, but in the add-in file.
  • Parastinî you won’t be bothered by macros, either. add-ons are, by definition, trusted sources.
  • Dikarin bikin separate tab on the Excel ribbon with nice buttons to run add-in macros.
  • The add-in is a separate file. His ragirtina hêsan from computer to computer, share it with colleagues or even sell it 😉

Let’s walk through the entire process of creating your own Microsoft Excel add-in step by step.

Step 1. Create an add-in file

Open Microsoft Excel with a blank workbook and save it under any suitable name (for example MyExcelAddin) in add-in format with the command Pelê - Wek tomar bike or keys F12, specifying the file type Excel Add-in:

Please note that by default Excel stores add-ins in the C:UsersYour_nameAppDataRoamingMicrosoftAddIns folder, but, in principle, you can specify any other folder that is convenient for you.

Step 2. We connect the created add-in

Now the add-in we created in the last step MyExcelAddin must be connected to Excel. To do this, go to the menu Pelê - Vebijêrk - Pêvek (File — Options — Add-Ins), bişkojka bitikîne Ji dor (Çûyin) at the bottom of the window. In the window that opens, click the button Axaftin (Bigerin) and specify the location of our add-in file.

If you did everything right, then our MyExcelAddin should appear in the list of available add-ons:

Step 3. Add macros to the add-in

Our add-in is connected to Excel and works successfully, but there is not a single macro in it yet. Let’s fill it up. To do this, open the Visual Basic editor with the keyboard shortcut alt+F11 an bişkojka Visual Basic tab pêşvebirinê (pêşdebir). Ger tab pêşvebirinê not visible, it can be displayed through Pelê - Vebijêrk - Sazkirina Ribbon (Pel - Vebijêrk - Ribbon xweş bikin).

There should be a window in the upper left corner of the editor Rêvename (if it is not visible, then turn it on through the menu Dîtin - Projeya Explorer):

This window displays all open workbooks and running Microsoft Excel add-ins, including ours. VBAProject (MyExcelAddin.xlam) Select it with the mouse and add a new module to it via the menu Têxe - Modul. In this module, we will store the VBA code of our add-in macros.

You can either type the code from scratch (if you know how to program), or copy it from somewhere ready-made (which is much easier). Let’s, for testing, enter the code of a simple but useful macro into the added empty module:

After entering the code, do not forget to click on the save button (diskette) in the upper left corner.

Our macro FormulasToValues, as you can easily imagine, converts formulas to values ​​in a preselected range. Sometimes these macros are also called pêvajoyê. To run it, you need to select cells with formulas and open a special dialog box Macros ji tabê pêşvebirinê (Pêşvebir - Macro) an jî kurteya klavyeyê alt+F8. Normally, this window shows available macros from all open workbooks, but add-in macros are not visible here. Despite this, we can enter the name of our procedure in the field navê makro (Macro name)û paşê pêl bişkoka Rev (rev) – and our macro will work:

    

Here you can also assign a keyboard shortcut to quickly launch a macro – the button is responsible for this Parametreyên (Vebijêrk) in the previous window makro:

When assigning keys, keep in mind that they are case sensitive and keyboard layout sensitive. So if you assign a combination like Ctrl+Й, then, in fact, in the future you will have to make sure that you have the layout turned on and press additionally Tarloqîto get the capital letter.

For convenience, we can also add a button for our macro to the quick access toolbar in the upper left corner of the window. To do this, select File – Options – Quick Access Toolbar (Pel - Vebijêrk - Vebijêrk Amûra Gihîştina Zû Vebijêrin), and then in the drop-down list at the top of the window the option Macros. After that our macro FormulasToValues can be placed on the panel with the button Lêzêdekirin (Lêzêdekirin) and select an icon for it with the button Gûherrandinî (Weşandin):

Step 4. Add functions to the add-in

Lebê macro-procedures, hene jî function macros or as they are called UDF (User Defined Function = user defined function). Let’s create a separate module in our add-on (menu command Têxe - Modul) and paste the code of the following function there:

It is easy to see that this function is needed to extract VAT from the amount including VAT. Not Newton’s binomial, of course, but it will do for us as an example to show the basic principles.

Note that the syntax of a function is different from a procedure:

  • construction is used Function …. End Function di ber Sub … End Sub
  • after the name of the function, its arguments are indicated in brackets
  • in the body of the function, the necessary calculations are performed and then the result is assigned to a variable with the name of the function

Also note that this function is not needed, and it is impossible to run like the previous macro procedure through the dialog box Macros û bişkojk Rev. Such a macro function should be used as a standard worksheet function (SUM, IF, VLOOKUP…), i.e. just enter in any cell, specifying the value of the amount with VAT as an argument:

… or enter through the standard dialog box for inserting a function (button fx in the formula bar), selecting a category Bikarhêner Diyar kirin (Bikarhêner diyarkirî):

The only unpleasant moment here is the absence of the usual description of the function at the bottom of the window. To add it you will have to do the following:

  1. Open the Visual Basic Editor with a keyboard shortcut alt+F11
  2. Select the add-in in the Project panel and press the key F2to open the Object Browser window
  3. Select your add-in project from the drop-down list at the top of the window
  4. Right-click on the function that appears and select command Properties.
  5. Enter a description of the function in the window Terîf
  6. Save the add-in file and restart excel.

After restarting, the function should display the description we entered:

Step 5. Create an add-on tab in the interface

The final, though not mandatory, but pleasant touch will be the creation of a separate tab with a button to run our macro, which will appear in the Excel interface after connecting our add-in.

Information about the tabs that are displayed by default is contained within the book and must be formatted in a special XML code. The easiest way to write and edit such code is with the help of special programs – XML ​​editors. One of the most convenient (and free) is Maxim Novikov’s program Edîtorê Ribbon XML.

The algorithm for working with it is as follows:

  1. Close all Excel windows so that there is no file conflict when we edit the add-in XML code.
  2. Launch the Ribbon XML Editor program and open our MyExcelAddin.xlam file in it
  3. Bi bişkojk hilpekînên in the upper left corner, add the code snippet for the new tab:
  4. You need to put in empty quotes id our tab and group (any unique identifiers), and in awanasî – the names of our tab and a group of buttons on it:
  5. Bi bişkojk pişkov on the left panel, add a blank code for the button and add tags to it:

    — label is the text on the button

    — imageMso — this is the conditional name of the image on the button. I used a red button icon called AnimationCustomAddExitDialog. The names of all available buttons (and there are several hundred of them!) can be found on a large number of sites on the Internet if you search for the keywords “imageMso”. For starters, you can go here.

    - onAction – this is the name of the callback procedure – a special short macro that will run our main macro FormulasToValues. You can call this procedure whatever you like. We will add it a little later.

  6. You can check the correctness of everything done using the button with a green check mark on the top of the toolbar. In the same place, click on the button with a floppy disk to save all changes.
  7. Close the Ribbon XML Editor
  8. Open Excel, go to the Visual Basic editor and add a callback procedure to our macro KillFormulasso that it runs our main macro for replacing formulas with values.
  9. We save the changes and, returning to Excel, check the result:

That’s all – the add-in is ready to use. Fill it with your own procedures and functions, add beautiful buttons – and it will become much easier to use macros in your work.

  • What are macros, how to use them in your work, where to get macro code in Visual Basic.
  • How to make a splash screen when opening a workbook in Excel
  • Pirtûka Makro ya Kesane çi ye û meriv wê çawa bikar tîne

Leave a Reply