Home » excel » excel – Search and Replace random cell text by first :

excel – Search and Replace random cell text by first :

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have a slew of rows (100K+) to search and modify the contents.

For example the cells contain similiar text as DGC9610411:DB:10:82

All of this text can change per row except that fact that the : means something to me.

In this and every other row, i need to remove the first : and all the text after so that the cell would look like this DGC9610411

Next I will be adding the contents of another cell to the end. I think that will be an easy step and I could most likely figure that out without much effort.

I have this code in a while loop for each row. so the code is looking at one row at a time.

I have searched but everyone seems to have a different set of needs.

How to&Answers:

Just use Find and Replace, no need for vba or formulas.

  1. Select the column containing the data that you need to modify
  2. Press Ctrlh to open the Find and Replace dialog.
  3. In the “Find what:” field, type :*
  4. In the “Replace with:” field, leave it blank
  5. Click Replace All

tigeravatar Find and Replace

EDIT: If it has to be VBA, this simple macro will accomplish the same thing. Be sure to change the A to your actual column letter.

Sub tgr()

    Columns("A").Replace ":*", ""

End Sub

EDIT: Upon request I am adding a formula solution. In cell B1 and copied down:

=LEFT(A1,FIND(":",A1&":")-1)

Answer:

Try this small macro:

Sub colonoscopy()
    Dim c As String
    Dim r As Range, I As Long
    For Each r In ActiveSheet.UsedRange
        v = r.Value
        I = InStr(1, v, ":")
        If I > 0 Then
            r.Value = Mid(v, 1, I - 1)
        End If
    Next r
End Sub