# Streamline Math in VBA

It’s possible to streamline some calculation procedures in Excel and add calculation capabilities that don’t exist in Word. Unfortunately, most textbooks on VB for Office 97 are focused on nonmathematical features such as text formatting or graphic displays. This situation creates a significant challenge for those who want to use VB macros for their superior mathematical capabilities, because the method for referencing numerical values in a cell of a Word table is significantly different from referencing a cell in an Excel spreadsheet. If you’re sufficiently persistent and a little clairvoyant, you can figure out the correct syntax from the Help found in the VB editors for Excel and Word. You can also get an almost complete set of hints on the subject from Microsoft Office 97: Visual Basic Programmer’s Guide by The Microsoft Corporation (Microsoft Press, 1997, ISBN: 1572313404). However, these are the only examples I know of that spell it out explicitly. They calculate the sum of cubes of the values for the first eight cells of column 1 and place the result in the ninth cell of column 1.

For Word VBA:

`` Sub columnmath()	Dim x As Long, i As Long	Dim myTable As Table	Dim myStr As String	x = 0	Set myTable = ActiveDocument.Tables(1)	For i = 1 To 8		x = x + myTable.Cell(i, 1).Range. _			Calculate ^ 3	Next i	myStr = Str(x)	myTable.Cell(9, 1).Range.InsertAfter (myStr)End SubFor Excel VBA:Sub columnmath()	Dim x As Long, i As Long	Sheets("Sheet1").Activate	x = 0	For i = 1 To 8		x = x + Cells(i, 1).Value ^ 3	Next i	Range("a9").Value = xEnd Sub``

Although this calculation can be done entirely within the capabilities of Excel, it would take up an extra column. The calculation can’t be done at all within Word. In trying to make sense out of it all, consider that Word VBA has a nonintuitive syntax for referencing cells. To read the value of a cell, the terminal word must be Calculate. To write a value into a cell, it must first be converted to a string, and the nonintuitive terminal InsertAfter must be used. Tables are numbered consecutively in a Word document; therefore the Word example deals with the first table in the document. Each sheet of an Excel workbook is just one big table; therefore, the Excel example deals with the first sheet of the workbook. In the Excel example, the Range(“a9”) could be replaced by Cells(9,1), or one of several other variants involving the use of Range.

See also  How to Create and Deploy QR Codes Online: A Comprehensive Guide

## About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.