Questions
About MrExcel
Consulting Services
Learn Excel Resources
Challenge of the Month
MrExcel Seminars
Message Board
MrExcel Store
Podcast
Search
Media
Contact
Home
Having an Excel Emergency?


Replace only the first 000 in each Excel cell

 

Jean asked this week's Excel question. I am trying to convert a supplier price book in Excel to match their UPC codes to our UPC code system. Their price book has a column in Excel with a series of numbers eg. 000004560007 OR cel000612004. What I have to do is two fold. I need to take the first three zeroes out without deleting any other 0's in the cell. Next, I have to append a 3 digit code "upc" before the first numeric digit in the cell. This will be an ongoing need. I will have to train several people to use the Excel system.

It sounds like Jean already considered using Edit-Replace to get rid of the three zeroes. This would not work because doing an edit replace on "000004560007" would cause both occurrences of 000 to disappear.

First I want to suggest that Jean insert a temporary column next to the current price codes, write a formula to do the transformation, then use Edit-Copy, Edit-PasteSpecial-Values to copy the formula back over the original price codes.

The poorly documented REPLACE() function will do the trick in this case. I was disappointed with the implementation of REPLACE(). I would have thought it would ask for a specific text to replace, but instead it asks for the user to figure out character positions to replace. REPLACE will replace part of a text string with a new string. The four arguments that you pass to the function are the cell containing the old text, the position of the character in old text you want to replace, the number of characters to replace, and the new text to use.

My simplifying assumption is that the three zeroes represent the onset of the UPC code in the string. Thus, there is no need to search for the first numeric character in the cell. Once the formula finds the three zeroes, it can replace those three zeroes with the string "upc".

To find the location of the first occurrence of "000" in the text, you would use this formula:

	=FIND(A2,"000")
The formula that Jean would need to enter in cell B2 would be:
	=REPLACE(A2,FIND("000",A2),3,"upc")

Although this would work, Jean mentions that the answer will have to be used by many people, and I suspect that not all of them are Excel gurus who want to have to be entering complex formulas. It is possible to take most Excel functions and use them in a macro. The catch is that converting an Excel function for use in a macro is not consistent. In some cases, the VBA macro language has an exact replacement for the function built in. In other cases, you can only use the function by qualifying it with the "Application.WorksheetFunction." prefix. The third possibility is that VBA has a similarly named function which behaves differently than the Excel function. As it turns out, REPLACE is a function which has a superior VBA equivalent, so we can actually simplify the function in the macro.

My thought is to have the pricing analysts highlight a range of cells and then invoke this macro. The macro will use a loop with "For each cell in Selection" at the beginning. With this loop, "cell" becomes a special range variable. You can use cell.address or cell.value or cell.row to find the address, value or row of the current cell in the loop. Here is the macro:

Public Sub Jean()    
' This macro will replace the first occurrence of "000"    
' in each selected cell with the string "upc"    
' copyright 1999 www.MrExcel.com
    For Each cell In Selection
        cell.Value = Replace(cell.Value, "000", "upc", 1, 1, vbTextCompare)
    Next cell
End Sub

Note that this is a destructive-type macro. When the user highlights a range of cells and runs the macro, those cells will be changed. It goes without saying that you want to thoroughly test your macro on test data before you set it loose on real production data. When you have a situation like Jean's where you will constantly be required to transform a column using a complex formula, writing a simple macro like the one illustrated here can be an effective and time-saving tool.

  

If you like the tip in this page, you will love the book:
Guerilla Data Analysis Using Microsoft Excel Book
Guerilla Data Analysis
Using Microsoft Excel

Buy Now button
spacing bar
More Info button

There are over 150,000 pages at MrExcel.com. Use Google to search our site:




MrExcel Consulting can be hired to implement this concept, or many other cool applications, with your data.



Privacy Policy

Excel is a registered trademark of the Microsoft® Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.

All contents Copyright 1998-2008 by MrExcel Consulting.