Find and Replace ### ### with just ###

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3158313%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3EFind%20%26amp%3Bamp%3B%20Replace%20%23%23%23%20%23%23%23%20with%20just%20%23%23%23%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3158313%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EWhat%20I%20am%20looking%20at%20is%20a%20column%20consisting%20of%20numbers%20with%20seven%20characters%20(%23%23%23_L%23%23%2C%20where%20%22L%22%20is%20a%20letter%2C%20%23%20is%20a%20number%2C%20and%20%22_%22%20is%20just%20a%20space).%20However%2C%20there%20are%20sometimes%20numbers%20that%20are%20(%23%23%23_%23%23%23).%20However%2C%20in%20these%20numbers%2C%20they%20are%20repeating%20the%20first%20three%20%23's.%20The%20rest%20are%20duplicates%20I%20want%20to%20get%20rid%20of%20with%20the%20space%2C%20too.%20The%20column%20can%20be%20a%20hundred%20rows%20long.%20First%2C%20I%20need%20to%20know%20how%20I%20can%20%22find%22%20the%20%23%23%23_%23%23%23.%20Second%2C%20%22replace%22%20with%20the%20first%20three%20%23%23%23's.%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EFor%20example%2C%20I%20have%20a%20column%20with%20one-hundred%20rows%20of%20numbers.%2090%20are%20700%20C99.%20Five%20are%20100%20100%2C%20while%20another%20five%20are%20200%20200.%20I%20want%20the%20quickest%20way%20to%20find%20the%20latter%2010%20and%20get%20rid%20of%20the%20repeating%20number%2C%20so%20that%20it%20only%20says%20100%20and%20200%2C%20respectively.%20How%20do%20I%20do%20that%3F%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3158313%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EExcel%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3EFormulas%20and%20Functions%26lt%3B%5C%2Flingo-label%26gt%3B%3CLINGO-LABEL%3EMacros%20and%20VBA%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E
Occasional Visitor

What I am looking at is a column consisting of numbers with seven characters (###_L##, where "L" is a letter, # is a number, and "_" is just a space). However, there are sometimes numbers that are (###_###). However, in these numbers, they are repeating the first three #'s. The rest are duplicates I want to get rid of with the space, too. The column can be a hundred rows long. First, I need to know how I can "find" the ###_###. Second, "replace" with the first three ###'s. 

 

For example, I have a column with one-hundred rows of numbers. 90 are 700 C99. Five are 100 100, while another five are 200 200. I want the quickest way to find the latter 10 and get rid of the repeating number, so that it only says 100 and 200, respectively. How do I do that?

2 Replies

@statejj16 

 

If you are okay with a vba suggestion, you could try this (change the range reference to whatever your actual range is). And, be sure to back up your data before trying to ensure it's what you're looking for.

 

Sub test()
     Dim cell As Range
     
     With CreateObject("VbScript.RegExp")
          .Pattern = "(\d{3}) \1"
     
          For Each cell In Range("A1:A100")
               If .test(cell.Value) Then
                    cell.Value = .Replace(cell.Value, "$1")
               End If
          Next cell
     End With
     
End Sub

 

@statejj16 Alternatively, and in case you prefer not to use VBA a simple, but perhaps not very elegant, formula comparing the first three characters with the last three can create the numbers how you want them. Then copy/paste as values the end result on top of the original column. With the numbers starting from in A1 that could be:

=IF(LEFT(A1,3)=RIGHT(A1,3),LEFT(A1,3),A1)

 and copy down.

If your Excel version supports dynamic arrays and the numbers are in A1:A100, try this:

=IF(LEFT(A1:A100,3)=RIGHT(A1:A100,3),LEFT(A1:A100,3),A1:A100)