Compile an Excel macro

Source: Internet
Author: User

Author: Zhu Jincan

Source: http://blog.csdn.net/clever101

 

When a friend used EXCEL to create a table, he asked me to write a calculation formula in one column and automatically calculate the calculation result in the other column, for example:


I told him that this can be implemented by writing a macro. The procedure is as follows:

1. Find the macro (m) under the macro (m) menu under the "tool" menu bar, as shown in:


2. Enter the name calc in the pop-up dialog box and click Create, as shown in the following figure:

3. delete all the code in the pop-up interface and enter the following code:

Sub Calc()a = [A1].Value[B1] = Evaluate(a)a = [A2].Value[B2] = Evaluate(a)a = [A3].Value[B3] = Evaluate(a)a = [A4].Value[B4] = Evaluate(a)End Sub


As shown in:


Here is a simple explanation of the Code:

Sub calc () is to define a function, and end sub is to end a function.

A = [a1]. value indicates that the expression in table A1 is assigned to variable A, [B1] = evaluate (A), which is used to calculate the value of expression, assign the value of this expression to Table B1. The above Code only calculates the values of the four grids A1, A2, A3, and A4. If you want to calculate the values of other grids, such as A5, A6, and A7, you can modify this code.

 

4. Click the Save button to save the macro, as shown below:



5. Close the macro editing interface as follows:


6. Enter four formulas in the four grids A1, A2, A3, and A4, for example:


7. Find the macro (m) under the macro (m) menu under the "tool" menu bar, as shown in:


8. Select the calc macro and click the execute button, as shown in:


9. The execution result is shown in:


 

Now I find that the Office macros are basically the same as the Visual Studio macros, so I have to admire Microsoft's architecture. You only need to understand one and others.

 

References:

 

1. Use the evaluate () function in Excel to calculate the calculation result of a text Mathematical Expression






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.