VBA Snippet – GetTimezoneOffset()

One of the things I think is amazing about VBA is the fact that it is so incomplete in some ways. For instance, did you know that there is no native way to get the local timezone offset. Yesterday I was working on a VBA SOAP plugin for Excel and realized this horrible fact. Of course, my first instinct was to Google the answer. Unfortunately, not even Google could provide a satisfactory (meaning short) solution. Therefore, I came up with the following VBA function:

Public Function GetTimezoneOffset() As Integer
  Dim ie As New InternetExplorer
  ie.Visible = False
  ie.Navigate "about:blank"
  While ie.readyState <> READYSTATE_COMPLETE And Not ie.Busy
  Wend
  ie.Document.parentWindow.execScript "n = (new Date).getTimezoneOffset()"
  GetTimezoneOffset = ie.Document.parentWindow.n
  ie.Quit
End Function

Of course, there are downsides to this solution. The first (being minor) is the fact that you have to include a reference by going to Tools > References… and then selecting “Microsoft Internet Controls”. The second is the fact that every time this function is called, an instance of Internet Explorer is created and then quickly (speed depending on the computer) destroyed. Other than that, the function will work. Its purpose is to return the amount of minutes behind GMT (or UTC) time the computer is. This is the equivalent of doing the following in JavaScript:

var offset = (new Date).getTimezoneOffset();

Quick Excel Tip – Quickly AutoFit Columns & Rows

  1. Click on the top-left rectangle above the row numbers and to the left of the column names so that all of the cells in the worksheet will be selected.
  2. Move the mouse over the line between two column names (column headers) and double-click it. This will AutoFit all of the columns in the worksheet.
  3. Move the mouse over the line between two row numbers (row headers) and double-click it. This will AutoFit all of the rows in the worksheet.

Quick Excel Tip – Quickly Select Cells In A Range

One of the things that I still find funny is when I ask a user to select all of the values in a column (not the column itself) and then user starts from the top and drags the selection until they reach the end. Of course, for a couple hundred rows, this doesn’t take too long, but if you have thousands or tens of thousands, this can take a while. Therefore a quick tip to select a large range of non-empty cells in a column, you can do the following:

  1. Select the top-most cell in the desired range.
  2. Hold down CTRL (or if using a Mac COMMAND) and SHIFT on the keyboard and then press the down arrow key. This will select all of the cells in the range as long as there are no empty cells in the range.

If the range does contain empty cells, instead of dragging the selection from the top to the bottom, you can do the following:

  1. Select the top-most cell in the desired range.
  2. Scroll down to the end of range (which is usually the end of the sheet) by using the scroll bar.
  3. Press SHIFT and then select the last cell in the range.

If you know the address of the last cell, you can do the following:

  1. Select the top-most cell in the desired range.
  2. Type the address of the final cell of the range into the address box and press SHIFT + ENTER.

If you know the addresses of both the top-left and the bottom-right cells, you can enter those address delimited by a colon into the address box and then press enter. For example, entering A2:D8192 into the address box will select the cells from A2 to D8192.

There are probably other cool tricks as well, so if you have any to share with the world, you can post them in a comment. If your’s is good enough, I will definitely expand this article to include it and then give you credit. ;)

Excel – Coalesce Function?

Unfortunately, after googling for a while, I was unable to find a COALESCE function or something similar that natively exists within Excel. Therefore, I decided to write a quick-and-dirty formula that would approximate it. I used the following array formula to pull the first non-blank value from cells C2 to C10 in a worksheet:
=INDEX($C$2:$C$10,MATCH(FALSE,ISBLANK($C$2:$C$10),FALSE))

As you can see, I used three Excel functions get my desired result. It would be nice to have less, but you can’t expect everything from this poor man’s database. :D Anyway, the ISBLANK function does exactly what it sounds like it does: it indicates whether or not the specified cell is blank. Since this is an array formula, ISBLANK($C$2:$C10) evaluates to an array of 9 TRUE/FALSE values. The MATCH function searches for the first parameter in the specified array (the second parameter) and evaluates to the position of the first match. The reason I supplied FALSE as a third parameter was to make sure that if every value was blank, a #N/A error would be returned. The INDEX function takes an array as the first parameter, and the position in the array to evaluate to as the second parameter.

In order to make the the formula evaluate as an array formula, you must use the CTRL + ENTER key combination to instead of simply pressing ENTER after inputting the formula. If you are using a Mac, the key combination is COMMAND + ENTER.

In conclusion, this is not the optimal solution, but it works and it doesn’t require a UDF (AKA macro).

Excel – Day of the Week By Name

Have you ever had to work with a spreadsheet which contained a column full of dates, but you actually needed to see column full of the corresponding days of the week by name? Believe it or not, translating dates to the name of the day of the week is quite easy. Let’s say that we have the date 6/24/11 in cell A2. In order to find out what day of the week that day is, we can place this formula in another column on the same row:
=TEXT("DDDD", A2)

Now in this new cell, you will see the value Friday. As you have probably already guessed, if the entire column is filled with dates, you can drag this formula down through the entire column.