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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.