Archive for the ‘VBA Basics’ Category.

A quick tip - Working with arrays with unknown bounds

Two recent instances required transferring data from one array to another. One was a solution to speed up the use of COUNTIF for each element in a large range testing against another large range. In the process I had to build several building blocks including a replacement for the native Excel TRANSPOSE function, [...]

Summing the Digits of a Number

Summing the digits of a number is a chore I’ve been doing alot lately. Originally I’d parse the number out over the columns. And since SUM() ignores text, I’d turn the characters into digits by applying an arithmatic identity operation, like this:

=- -MID($A10,COLUMN(),1)

That’s double minus signs before the MID() function. The reasons [...]

Russian Peasant Multiplication

The Daily WTF posted a challenge to code the Russian Peasant Multiplication. Here’s mine:
Function RussianPeasant(lFirst As Long, lSecond As Long) As Double
   
    Dim lDiv As Long
    Dim lMult As Long
    Dim lMod As Long
   
    lDiv = lFirst
    lMult = lSecond
   
    Do Until [...]

Graphics I Grok

As a big-time college basketball junkie (Villanova ‘96 grad school–Kerry Kittles et al), I thought the NY Times had two excellent NCAA basketball tourney graphics. No pie charts! I needed a tech assist from Dick to learn how to do this, so I’m not as timely as I should have been, and that [...]

Using a Class Property to do more than just Assign or Query a Value

The purpose of this article is to introduce various capabilities of a class property. It is not meant to serve as an introduction to classes and objects. See the references section for introductory pages.
The typical use of a Class Property is to assign a value or to query its value as in the [...]

Testing Strings Using Left

The “wrong” way:
Sub FindTotals()
   
    Dim rCell As Range
   
    For Each rCell In Sheet1.Columns(1).Cells
        If Left$(rCell, 5) = "Total" Then
            ‘Do something
       End If
    Next rCell
   
End Sub
The “right” way:
Sub FindTotals2()
   
    Dim rCell As [...]

Random Sorts

Red wants to have a kind-of lottery for his students. He will award them prizes based on a random drawing, but wants to weight each student based on the number of assignments turned in. Normally, I would accomplish this by typing the name of each student in column A one time for every [...]

Website update

This post introduces several new items on the www.tushar-mehta.com website.