Macro to find a text string within a larger body of text, in each cell of a column of cells, then copy and paste that text string into adjacent cells

Drivelservant

New Member
Joined
Apr 15, 2016
Messages
2
I need to write a macro that will search each cell in a column for the text "col", then copy that text and the seven characters to its right, and paste it into the cell adjacent and to the right of the searched cell. The search cannot be case sensitive. Any copy-and-pastable suggestions.

Hope I've explained this clearly. Thank you in advance for your time.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
You can try this.

Change the "A" to whatever column you desire, leave the "", they are needed.

Code:
Sub Drivelservant()
Dim rngFound As Range, strFirst As String
Application.ScreenUpdating = False
    With ActiveSheet.Columns("A")
        Set rngFound = .Find(What:="col", LookAt:=xlPart, SearchDirection:=xlNext, After:=.Cells(1), MatchCase:=False)
            If Not rngFound Is Nothing Then
                strFirst = rngFound.Address
                rngFound.Offset(, 1).Value = Mid(rngFound, InStr(1, rngFound, "col"), 10)
            Else
                MsgBox "No matches found"
                Application.ScreenUpdating = True
                Exit Sub
            End If
        Do
            Set rngFound = .FindNext(rngFound)
                If Not rngFound Is Nothing And strFirst <> rngFound.Address Then
                     rngFound.Offset(, 1).Value = Mid(rngFound, InStr(1, rngFound, "col", vbTextCompare), 10)
                Else
                    Application.ScreenUpdating = True
                    Exit Sub
                End If
        Loop
    End With
End Sub
 
Last edited:
Upvote 0
If you have less than, say, 5000 rows of original data (as a rough guess), then this should work for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub CopyConPlus7()
  Dim LastRow As Long, Cons As Variant
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Cons = Split(Application.Trim(Join(Application.Transpose(Evaluate(Replace("IFERROR(MID(A1:A#,SEARCH(""con"",A1:A#),10),"""")", "#", LastRow))))))
  Range("B1").Resize(UBound(Cons) + 1) = Application.Transpose(Cons)
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0
You should use this version instead of the above version.

Code:
Sub Drivelservant()
Dim rngFound As Range, strFirst As String
Application.ScreenUpdating = False
    With ActiveSheet.Columns("A")
        Set rngFound = .Find(What:="col", LookAt:=xlPart, SearchDirection:=xlNext, After:=.Cells(1), MatchCase:=False)
            If Not rngFound Is Nothing Then
                strFirst = rngFound.Address
                rngFound.Offset(, 1).Value = Mid(rngFound, InStr(1, rngFound, "col", vbTextCompare), 10)
            Else
                MsgBox "No matches found"
                Application.ScreenUpdating = True
                Exit Sub
            End If
        Do
            Set rngFound = .FindNext(rngFound)
                If Not rngFound Is Nothing And strFirst <> rngFound.Address Then
                     rngFound.Offset(, 1).Value = Mid(rngFound, InStr(1, rngFound, "col", vbTextCompare), 10)
                Else
                    Application.ScreenUpdating = True
                    Exit Sub
                End If
        Loop
    End With
End Sub
 
Upvote 0
Thanks to both Rick and skywriter, both these solutions work perfectly. You have no idea how much time this has saved me, thanks again chaps.
 
Upvote 0
My pleasure, thanks for the feedback. :cool:

In my post #4, I meant you should use that version of my code if you so choose.

When I read it now it looks like I'm saying don't use Rick's. (y)
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,603
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top