Wordman s Production Corner By Dick Eassom, AF.APMP Three Excel Tricks...Just for a Change Three Problems One of my colleagues has the good fortune to have to manage marketing mailing lists. Since the address fields are in separate columns on an Excel worksheet, she needs a simple way of combining these fields into one address field, and then exporting the combined address to another workbook. What s the best way to do this? Another colleague wants to add stop light formatting to column cells in an Excel worksheet. Excel 2003 s Conditional Formatting feature colors the cells according to their value, but he would prefer something more visually attractive, such as a colored circle, i.e. a true stop light. However, he s finally upgraded from Office 2003 to Office 2007, so can the new version help him? I m a big fan of Farmville; some would say an addict. If you haven t heard of this game, then you re probably not on Facebook. With more than 75 million players worldwide, Farmville, by Zynga Game Network, Inc., is one of the fastest growing social media games, allowing Facebook users to manage a virtual farm. Part of the tactics of Farmville is deciding which crops to plant. Each type of crop takes a different amount of time to grow before it can be harvested, provides a different profit margin, and different experience points or XPs. To advance in the game, you need more XPs, but also the cash to generate them. That s probably more about Farmville than you cared to know, but I set myself the task of finding out which crops provide the most XPs per hour at the most profit per hour. Having collected the data, I created an Excel X-Y scatter plot to show my results. But how can I get each value to display the crop name? It appears there is no way to do this! Three Solutions Concatenating Address Fields A typical address list might have these columns in an Excel worksheet: APMP Perspective 30 Volume 20, Issue 1, 2010
Here s a typical address: Address 1 4695 MacArthur Court Address 2 8th Floor Address 3 City Newport Beach State CA Post Code/ZIP 92660 Country USA Whereas you can add the numeric values of cells in a worksheet using the + operator, or the SUM function, joining text values, or concatenating, is done using the & operator. So if we use column H for the combined address, we could use the formula: The logical_test is evaluated as True or False, and we can use the LEN function to test if there is any text in the Address 2 or Address 3 fields. If there is text (LEN(Cell)>0), we ll add a comma and a space (, ), otherwise we ll add nothing ( ). So our formula now becomes: =A4&, &IF(LEN(B4)>0,B4&,, )&IF(LEN(C4)>0,C4&,,, )&D4&, &E4&, &F4&, &G4 Using this, our formula returns 4695 MacArthur Court, 8th Floor, Newport Beach, CA, 92660, USA You can copy this formula into all your address rows in your worksheet. But if we try and copy the composite address column to another worksheet, it will just copy the formulas, so make sure to use Paste Values: =A4&B4&C4&D4&E4&F4&G4 Using our typical address above, the resulting value in column H will be: 4695 MacArthur Court8th FloorNewport BeachCA92660USA We need some commas and spaces to make this usable, so now our column H formula becomes: =A4&, &B4&, &C4&, &D4&, &E4&, &F4&, &G4 Better, but still not perfect. Since there is no Address 3 value, our formula is going to return: 4695 MacArthur Court, 8th Floor,, Newport Beach, CA, 92660, USA Excel 2007 Conditional Formatting You ve probably used Excel s conditional formatting feature at some time to automatically color cells depending on their value. Let s take a simple example: a group of cells with the values 1 through 10: We need to consider that there may not be an Address 2 or Address 3 field, and we can do that using the Excel IF and LEN functions. The general form of the IF function is: =IF(logical_test,[value_if_true],[value_if_false]) APMP Perspective 31 Volume 20, Issue 1, 2010
To apply conditional formatting in Excel 97-2003, select the range (A1:J1), and then click Format > Conditional Formatting. In the Conditional Formatting dialog, we can add up to three levels, e.g. color cells green for values between 8 and 10, yellow for cell values between 4 and 7 and red for cell values from 1 to 3: Excel 2010, which will be released this year, has a further feature, called sparklines, intense, simple, word-sized graphics, as their inventor Edward Tufte describes them in his book Beautiful Evidence (available on Amazon at http://www.amazon.com/beautiful- Evidence-Edward-R-Tufte/dp/0961392177). Here s an example, taken from the official blog of the Microsoft Excel product team (http://blogs.msdn.com/excel/archive/2009/07/17/sparklines-inexcel.aspx): However, Excel 2007 gives us many more options! Select the cells and click Conditional Formatting on the Home tab: Sparklines are not floating charts but appear like text in a cell. Personally, I can t wait to use them! Adding Labels to Scatter Plots Here s a sample of the data from the Farmville game: There are some very cool options under Data Bars, Color Scales and Icons Sets that certainly solve my colleague s problem as stated in my introduction to this article. As there are so many rich formatting options here, I suggest you take some time to explore them yourself! However, here are a few ideas: I wanted to plot the last two columns against each other as an X-Y, or scatter, chart. The crops at the top right of the chart offer the best return on investment for both profit and experience points. I would like the first column to be the label for each point, obviously. Selecting the K and L columns and inserting a scatter chart displays the data the way I wanted to see it, but APMP Perspective 32 Volume 20, Issue 1, 2010
Excel gives me no way to add the data in column A as labels without messing up the chart. However, I found a solution in a Microsoft Support article, How to use a macro to add labels to data points in an xy scatter chart or in a bubble chart in Excel, available online at http://support.microsoft.com/kb/213750. Those of you who have been following my Wordman articles know that I m a big fan of using macros to add functionality to Microsoft Office applications. Creating extra functionality using Microsoft Visual Basic for Applications (VBA) within Excel is just as easy as doing it within Word but slightly different. If you ve been following previous Wordman articles about using VBA, you ll remember that when you create a simple macro in Word, it gets stored in your personal Normal.dot; the same file that Word uses to store other customizations such as your toolbar changes. Excel has its equivalent personal file, named personal.xlsb (Excel 2007), or personal.xls (Excel 97-2003). Start Excel. In Excel 2007, if the Developer tab is not displayed on the ribbon, click the Office Button, click Excel Options, and under Popular, check Show Developer tab in the ribbon. Click OK to close the Excel Options dialog. Select the now visible Developer tab and then click Visual Basic (or press Alt+F11) to display the Visual Basic (VB) editor. (In older versions of Excel, go to Tools > Macro > Record New Macro.) If your personal file, e.g. personal.xlsb, is not displayed in the Project pane at the top left of the editor, you ll need to create a dummy macro to create it. To do this, in Excel, on the Developer tab, click Record Macro. In the Record Macro dialog, select Personal Macro Workbook under Store macro in, name the macro Dummy Macro and click OK. Now click Stop Recording on the Developer tab. (In older versions of Excel, click Stop Recording on the Stop Recording toolbar.) In the VB editor Project pane, you ll see that Personal.xlsb has been created. Double click Personal.xlsb to expand it, and then click Modules > Module 1. The empty Dummy Macro has been created. You can now delete this dummy macro and enter the code listing at the end of this article. I ve enhanced the original code posted by Microsoft with some features to make it more robust and to allow you to select the column that contains the data labels. How does it work? After creating some variables to store some data, we ll need during code execution, the first part of the code checks that we ve selected the chart before we run the macro. Next, the code checks that the chart is the correct type, i.e., an X-Y scatter chart. If both those checks pass, then the code gets the formula for the first data series. In my chart, this looks like: =SERIES(Seeds!$M$1,Seeds!$L$2:$L$48,See ds!$m$2:$m$48,1) By using VB s text string manipulation functions (Left, Mid, and Instr), the code parses out the formula until only Seeds!$L$2:$L$48 remains. The code then activates the worksheet containing the data, i.e. Seeds, and displays an input box that allows you to select the column containing the data labels: APMP Perspective 33 Volume 20, Issue 1, 2010
Clicking OK allows the code to continue. It calculates the offset of the data label column from the data column, activates the chart, and then applies the relevant data label to each data point in the chart. Below is the final result, showing that I should plant blackberries for best return on investment, assuming I could play FarmVille 24 hours per day! (I ve added a white background to the data labels and moved some to make the chart clearer.) Since we ve saved the code in your personal macro storage, you can call this macro at any time by clicking Macros on the Developer tab, selecting PERSONAL. XLSB!AttachDataLabels in the Macro dialog, and then clicking Run. (In older versions of Excel, go to Tools > Macros > Macro to display the Macro dialog.) Summary I hope you found some useful Excel tips in this Wordman article. I ll be presenting another Wordman workshop at the 21st Annual APMP International Conference & Exhibits at the Walt Disney World Dolphin, in Orlando, Florida, from June 1 4, 2010. As usual, there is no set agenda: please bring your Word problems, and we ll sort them out! We might even have some time for Excel and PowerPoint! You can reach Wordman via Dick Eassom, AF.APMP, at dick.eassom@apmp.org. Wordman is 2001 2010, Dick Eassom, and used with permission by APMP. Thanks to Sean Jones (www.knitestudios.com) for the Wordman artwork. APMP Perspective 34 Volume 20, Issue 1, 2010
Public Sub AttachDataLabels() Dim intpoint Dim strchartname Dim strvals Dim rngdatalabels Dim wkshdata Dim chtchart Dim intcoldata Dim intoffset As Integer As String As String As Range As Worksheet As Chart As Integer As Integer Const c_intrangetype As Integer = 8 Check the chart is active If ActiveChart Is Nothing Then Call MsgBox(Prompt:= Select chart, then run again, _ Title:= Add Data Labels, Buttons:=vbCritical + vbokonly) Exit Sub End If Store chart object Set chtchart = ActiveChart Test if it s a scatter chart with markers Select Case chtchart.charttype Case xlxyscatter, xlxyscatterlines, xlxyscattersmooth Do nothing Case Else Call MsgBox(Prompt:= This is not an scatter chart with markers, _ Title:= Add Data Labels, Buttons:=vbCritical + vbokonly) Exit Sub End Select Store the formula for the first series in strvals strvals = ActiveChart.SeriesCollection(1).Formula Extract the range for the data from strvals strvals = Mid(strVals, InStr(InStr(strVals,, ), strvals, Mid(Left(strVals, _ InStr(strVals,! ) - 1), 9))) strvals = Left(strVals, InStr(InStr(strVals,! ), strvals,, ) - 1) Do While Left(strVals, 1) =, strvals = Mid(strVals, 2) Loop Get column for data intcoldata = Range(strVals).Columns(1).Column Get worksheet name containing data Set wkshdata = Worksheets(Left(strVals, InStr(strVals,! ) - 1)) Get column for data labels. Ignore error if user clicked Cancel On Error Resume Next: Application.DisplayAlerts = False wkshdata.activate Set rngdatalabels = Application.InputBox(Prompt:= Select the data label column, _ Title:= Data Labels, Type:=8) On Error GoTo 0: Application.DisplayAlerts = True Exit if no range returned If rngdatalabels Is Nothing Then Exit Sub Get offset from data to data label columns intoffset = rngdatalabels.columns(1).column - intcoldata Activate the chart, and attach a label to each data point chtchart.activate For intpoint = 1 To Range(strVals).Cells.Count With chtchart.seriescollection(1).points(intpoint).hasdatalabel = True.DataLabel.Text = Range(strVals).Cells(intPoint, 1).Offset(0, intoffset).value End With Next intpoint End Sub APMP Perspective 35 Volume 20, Issue 1, 2010