Build an interactive worksheet in Excel 2011

By Chris Grover, Macworld |  Software, Excel 2011, Microsoft Office Add a new comment

Microsoft Office brings support for Visual Basic for Applications (VBA) macros back to the Mac. But if you haven't used VBA before, you might be wondering what you can actually use it for and how difficult it is to use. I've got a sample project that will provide some answers to both questions.

The project is a time-tracking spreadsheet that lets you quickly log the start- and finish-times for a task. It then calculates the total time spent on the task and applies your hourly rate to figure out the bill. It's a good demonstration of how easy it is to embed interactive and automated elements using VBA.

To start, you'll want to set up a standard worksheet laid out roughly as shown in the screenshot below. You can use any layout that works for your business as long as it includes columns for start time (column B), end time (column C), total minutes (column F) and billing (column G). In this example, column A is formatted to show just the month and year. Columns B and C show the date and time, including AM and PM. Column F, which calculates the total minutes spent on a task, displays just hours and minutes.

Creating a time-stamp button

The first step is to create a button that will insert the current date and time in a selected cell. To do so, open a new worksheet in Excel 2011 and enable the Developer tab in the Ribbon: In preferences (Excel -> Preferences), open the Ribbon pane and select Developer at the bottom of the Tab list.

Now go back to your worksheet and open the Ribbon's Developer tab. You should see four buttons on the left: Editor, Macros, Record and Relative Reference. Turn on Relative Reference (that means the recorded macro will work with any cell in your worksheet, as opposed to the cell that was selected when you recorded it). Now select a cell somewhere in the Start Time column in your worksheet and then click on the red Record button. Give your macro a name (InsertTime), a description, and a shortcut key (I used Option-Command-T), then click OK to start recording. Type this formula in the cell: =now(). Copy the cell, choose Edit -> Paste Special, then select Values. Click the black Record button to stop the recording.

To examine the VBA code you recorded, click on Macros on the ribbon's Developer tab, select your macro in the dialog box and click on Edit. The code (with automatically generated comments removed) should look like this:

Sub InsertTime()

ActiveCell.FormulaR1C1 = "=NOW()"

Selection.Copy

Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone,_

SkipBlanks:=False, Transpose:=False

End Sub

The now() function inserts the current time and date into a cell. Unfortunately, it also updates itself whenever the worksheet is recalculated; you don't want it to do that. That's why you add the copy/paste values step: That overwrites the now() formula with its current results. Test your macro to make sure it's working correctly--that it inserts the current time as a value in whatever cell you select.

You could use the shortcut key to trigger this macro. But it's more intuitive (especially if you're building this sheet for others to use) to trigger it with a button. On the Developer tab, under Form Controls, click on Button and then draw a button on your worksheet. In the Assign Macro dialog box, choose the InsertTime macro. Right-click on the button to reposition, resize or edit it.

To use the time-stamp button to record when you started on or finished a project, just select the appropriate cell in the Start or End column and click on the button.

Calculating with time

Now that you can record the times you worked on a project, the next step is to add formulas that will calculate the total time you spent and then apply that time to your hourly rate.

The first one is simple: In my example, the formula in column F subtracts the Start Time (column B) from the End Time (column C): =C6-B6.

The formula for calculating the billing is a little trickier. You can't just multiply your hourly rate by the total time in column F. That's because Excel expresses that time as a simple number. (Without the time/date formatting, Excel expresses one hour as 0.042106481.) You can't calculate your bill with that number. Instead, you have to translate the time value into hours and minutes and then apply the hourly rate to each portion.


Originally published on Macworld |  Click here to read the original story.

ITworld LIVE

SoftwareWhite Papers & Webcasts

White Paper

Activities Streams Base An Integrated Social Layer

The enterprise social software market is exploding thanks to converging trends of consumerization, cloud, and mobile. In this must-read report, "The Forrester Wave: Activities Streams, Q2 2012", Forrester Research Inc. evaluated five social software vendors with core strengths in the stream based on the overall strength of vendors' current offerings, a clear product strategy, and vendor market presence. In a detailed look at the space, Forrester named Yammer as a leader.

White Paper

ESG Lab Review: HP 3PAR Peer Motion Software

This ESG Lab review sponsored by HP + Intel documents hands-on testing of HP 3PAR Peer Motion Software's distributed volume.Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.

White Paper

ESG Lab Review: HP 3PAR Peer Motion Software

This ESG Lab review documents hands-on testing of HP 3PAR Peer Motion Software's distributed volume management with a focus on federated workload balancing, asset management, and thin provisioning.Intel and the Intel logo are trademarks of Intel Corporation in the U.S. and/or other countries.

White Paper

Deliver Cost-Effective Business Continuity with Extreme Capacity

IBM DB2 provides application cluster transparency technology that equips organizations running OLTP applications with the ability to deliver high availability and continuous uptime for transactional data, plus the flexibility and capacity they need to remain competitive.

White Paper

What Developers Want: The End of Application Redeploys

Eliminate application restarts in Java with JRebel! JRebel is a JVM plugin that eliminates application redeploys from the Java development cycle, a process that takes over 10 minutes of coding time away from developers each working hour, according to a recent survey. Just code, refresh and see everything instantly.

See more White Papers | Webcasts

Ask a question

Ask a Question