Objective
That year to see "Don't be afraid, Excel VBA is actually very simple" brief Encounter, read the first version of the electronic version, bought a paper version, and then send it to people. Then, found the second edition, Buy the collection. Later, the discovery of the Python programming language, is simply the inverse of the day, amateur non-programmer's gospel. Life is short, I use Python. A short few lines of code, to achieve a lot of their own dare not to think of features.
Self-learning VBA discovery for VBA and Python, I have a few ideas: (Just for automated office)
- Non-professional cousin, just ordinary office, I hope occasionally encounter point duplication of work can be automated. VBA for me, to remember things are too many, grammatical not concise. Each time you write a function, the code is too long.
- Although VBA has interfaces in many programs, the scope of the application is still slightly narrower, but there are too few scenarios for the average user to learn in depth. Have a task-driven, have the power to improve the level.
- Python is definitely not running fast, but it's definitely faster than running VBA in Excel.
- The Python language is concise (Python Dafa is good), the module is rich, and there are many great God-made wheels waiting for us to use.
- Python has a wide range of applications that can help me solve occasional tabular problems and connect with a variety of other software or platforms.
Use Python custom macros (Windows only)
- Preparatory work
- Installing the Xlwings module
- Download template file, Baidu Web link: xlwings custom macros and function templates
File list
Where the custom template can modify the name, MyMacro the python file as a custom macro, and myfun the Python file as a custom function.
- Set up the Excel Trust Center to Trust access to the VBA project object model
Trust Center Settings
- Custom macros
Open mymacro0 with Notepad and enter the following statement:import xlwings as xw@xw.subdef my_macro(): wb = xw.Book.caller() wb.sheets[0].range(‘A1‘).value = wb.name
A macro with a macro name of My_macro is written, and the name of the workbook is entered into cell A1. A custom macro requires only the first two sentences to be the same, and the content is written by itself, that is, the custom macro needs to start with the following statementimport xlwings as xw@xw.sub
Run as: First Alt+f8 run Importpythonudfs built-in macros, import our custom macros and functions, import, and then run the MY_MACRO macro I defined.
Perform
- Custom functions
The preparation is consistent, open myfun0, and write the first custom function:import xlwings as xw@xw.funcdef myfun(): wb = xw.Book.caller() return wb.name
Note that the start statement is @xw.func instead of @xw.sub, and then the function is relative macro, with a return value, so the return statement is used. After customizing the function, you can just use the same function as Excel itself. If you enter "=myfun ()" In cell A1, you will get the name of the workbook.
End
This paper presents a template for customizing macros and functions in the Xlwings module, and then gives a brief description of how to define macros and functions. Attention:
- A python file, can only define a macro or function, so the template has been given by default three macros and function files, easy to use directly.
- The name and relative file location of the Python file cannot be changed, the file location of the Excel file cannot be changed, and the name can be changed. For modifications, please refer to the official Xlwings documentation.
Luckyfrog
Links: Http://www.jianshu.com/p/7d6f53e3e6e9
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Plug in wings and let Excel fly--xlwings (Thu)