How to Concatenate fields in Excel

39 downloads 61600 Views 2MB Size Report
process with the example Excel file shown to the right. ... function/process for one record as we will copy the formula to the other records (records 3 through 5.).

TM

“How to Concatenate & Trim fields in Microsoft Excel” Written by: Sean Bair, Program Manager Crime Mapping & Analysis Program

National Law Enforcement & Corrections Technology Center, a Program of the National Institute of Justice

All rights reserved. No part of this book may be reproduced or copied in any form or by any means graphic, electronic or mechanical, including photocopying, taping, or information storage and retrieval systems without permission of the Crime Mapping and Analysis Program, Denver, Colorado.

How to Concatenate & Trim fields in Excel Open Excel. Open the file that contains fields you wish to combine into one field. I will walk you through this process with the example Excel file shown to the right. It contains 4 fields that we want to combine into one new “Address” field. There are 4 records to perform the concatenation against.

Insert a new variable that will contain the combined fields. Call it “Address.”

Click on the cell in your new variable that is directly to the right of the fields you wish to concatenate. If the example to the right, we would click in the E2 cell because we want to join cells A2 through D2 for record #2. We only need to do this function/process for one record as we will copy the formula to the other records (records 3 through 5.)

Select the function button from the toolbar. Select the “Text” function category and “CONCATENATE” from the function name list. Click OK.

The “CONCATENATE” dialog box should appear. You should move the dialog box to where you can see the row you wish to concatenate. In our case, we want to be able to see row 2, as this is the row we want to concatenate. To move the dialog box, simply click on the dialog box (anywhere), hold your left mouse button down and drag the dialog to its new location. Release the mouse to set the dialog’s new position.

We now want to select those variables we want to join together. You could either type the fields into the Text boxes or use your mouse to select them in the proper join sequence. For instance, click in the “Text1” text box located on the concatenate dialog. Now click on Cell A2 from the grid. A2 will be placed in the Text1 box. Now click Text2 on the concatenate dialog. Insert a blank space by typing “ “. If you do not perform this step, your data may not contain spaces between the concatenated words. Repeat these steps to include all the fields you wish to join. You can see the results from the formula on the bottom center of the concatenate dialog box. Click OK to complete the formula and join the fields.

Notices that the results show in E2 contain the concatenated (joined) fields. However, the formula viewer doesn’t show the address but the formula that created the address. If you were to save the results out at this time to a dbase IV file, only the formula would display in the Address variable. We need to copy the formula to the other cells (E3 through E5) and then copy the results and paste them into a new variable containing only the values.

To automatically copy the formula from E2 to the remainder of your data (in our case E5), move your mouse cursor over the bottom rightmost corner of cell E2. A crosshair cursor icon will appear. While you have the crosshair icon visible, double-click the left mouse button. This will ‘auto-populate’ the remainder of the cells to the end of the data with the concatenate formula.

The resulting auto-populate function should return a concatenated result for each record in your data.

We must now copy the results of our concatenate into a new field containing only the results and not the formulas. Click on the column header for column E. The entire column should highlight. Select Copy from the Edit menu.

Click on the column header for column “F.” Column F should now be highlighted. While over any portion of column F, click your right mouse button. The Edit menu popup will appear. Select the “Paste Special….” Command from the available choices.

The “Paste Special” dialog will appear. Select “Values” from the Paste radio buttons. This will paste only the text values of the copied concatenate field into the new column – thus eliminating the formula used to create the field.

Now we must delete the original field used to concatenate. So, click the column header for column “E.” The column will highlight. While over any portion of column “E”, click the right mouse button. The “Edit” menu popup will appear. From the list of available choices, click the “Delete” option. Column “E” will be deleted from the spreadsheet and the other columns will take its place in the order of variables.

At this point you have successfully concatenated fields in Excel. We will demonstrate one more feature in Excel that may be necessary if your data contains extra spaces. This may have occurred if one of the concatenated fields was empty during the process. If so, there will be extra spaces. We will use the “Trim” function to remove extra spaces from our address field.

We must first create a new variable that will contain the “trimmed” address information. In column “F” at row “1”, type “Address.” Now move the cursor and click in cell F2. This cell will accept the trimmed address formula.

This process uses the exact same steps as performed in the concatenate function. First, select the “Fx” or function button from the toolbar. Select “Text” from the function category box to the left and “TRIM” from the function name list box on the right. Click OK.

If necessary, move the trim dialog beneath your work area as shown in the picture to the right. Click in the “Text” box located on the Trim dialog. Now select the field you want to trim – in our case it is cell E2 that contains the address field. Click OK.

Now that you have a new value in the F2 cell, move your mouse cursor over the rightmost bottom corner of cell F2. Again, the crosshair icon will appear. Once the crosshair icon appears, double-click the left mouse button. This will automatically populate all the remaining cells in column “F’ with the formula in F2.

The results of the previous step are shown to the right. Each cell in column “F” should contain a ‘trimmed’ address from column “E.”

We must now copy the contents of column F (which contain not only the trimmed values but the underlying formula) and paste the values into a new column. Click the column header for column “F.” While your mouse cursor is over any portion of column “F”, click the right mouse button. From the “Edit” popup menu, select “Copy.”

Click in the column header for column G to highlight the entire column. While over any portion of column G, click the right mouse button. From the popup menu choose “Paste Special…”

From the “Paste Special…” dialog, select “Values” from the Paste radio button. Click OK.

Now we want to remove the two “Address” fields as they contain formula behind the scenes. So, click the column header to column E and while holding down the left mouse button, move it to the right and select column F. You should now have both columns highlighted as shown in the example to the right. While over any portion of the highlighted area, click the right mouse button. From the popup “Edit” menu, click the “Delete” option. The results of deleting both columns should be similar as in the example to the right. Now we must increase the width of column “E” so that it is as wide as the widest value in our new “Address” variable. To do this, move the mouse pointer over the column header divider between columns E and F. The mouse pointer will change from an arrow to a double “left-right” arrow. While this mouse pointer icon is visible, double-click the left mouse button. The column width of field E will auto size to fit the largest value in column E. Finally, click anywhere in the grid to remove the column highlight. At this point you could save the contents of the spreadsheet to a dbase IV file accessible from ArcView.

The processes just shown, “Concatenate” and “Trim”, can be performed on all types and formats of data brought into Excel. If you find yourself performing this or any Excel process/function on a routine basis, you may want to research the “Macro” function. Macros will record your keystrokes while inside Excel and allow you to play them back later against other data. The trick to Macros is to assure that the Macro is recording the keystrokes against data that will be formatted identically each and every time. For instance, you wouldn’t want to record a keystroke that instructed Excel to delete column E containing the “Address” if the next time you brought in data the “Address” variable was contained in column F. The Macro will only perform the sequence of keystroke events as it relates to Excel. It cannot distinguish between values or variables in the data. There are ways to have it perform these types of complex decisions, but that is for another article ;-)