Wednesday, September 12, 2012

How to Calculate Macros in Excel


1. Click the 'File' menu's 'Options' button, then click the 'Formulas' link. Click the 'Manual' option, then 'OK' to close the 'Options' dialog box. This step turns off automatic calculation, which your macro will restore.
2. Type the following into cells A1 through C1 of the current worksheet: 5, 10, '=a1*a2.' Notice that the formula in C1 is incorrect, due to your selection of the manual option in step one. Your macro will restore automatic calculation.
3. Click the 'Developer' tab's 'Visual Basic' button to enter the Visual Basic programming environment. Paste the following program into the window that appears. This program uses the 'Range' object to enter values in two different worksheet cells, and a formula into another cell. The program has two statements that force the current worksheet to update its calculation. The first is 'Application.Calculation = xlCalculationAutomatic.' This statement sets the 'Calculation' property of the Excel application itself. The second is the one calling the 'Calculate' function of the 'ActiveSheet' object. This statement tells only the active sheet to calculate, not the entire workbook.Public Sub recalc()'Enter numbers in a cell'Enter a calulation that depends on those numbers'Force recalculationApplication.Calculation = xlCalculationAutomaticRange('a1') = 5Range('a2') = 10Range('a3').Formula = '=a1*a2'ActiveSheet.CalculateEnd Sub
4. Press 'F5' to run the program.
5. Click the 'Excel' icon on the Windows taskbar to return to Excel. Notice that the cell A3 has a function that requires updated calculation, and that the result of the calculation, the product of the worksheet's top two cells, is accurate.

Blogger news