VBA: Search in column for specific text and copy to another tab

TaskMaster

Board Regular
Joined
Oct 15, 2020
Messages
75
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all and thank you in advance for looking at my query. I have tried to make a start with this but getting nowhere.

I have a tab called data which I currently have to manually format copy and paste to another tab. In column where the first 4 characters in a cell is = to "FB09" I want to copy the last 8 characters data into another tab called summary starting in cell A23. Could anyone give me some suggestions.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
And if cell A23 already has data in it?
 
Upvote 0
Hi thank you for your question. A23 would be empty at the start of the procedure and would like it to sequentially fill each row below for any further data items.
 
Upvote 0
One way.
VBA Code:
Sub FindAndCopy()
    Dim WS As Worksheet
    Dim rng As Range
    Dim S As String, SearchStr As String
    Dim LastRow As Long

    Set WS = Worksheets("data")
    SearchStr = "FB09"

    With WS.Range("A2", WS.Range("A" & WS.Rows.Count).End(xlUp))
        Set rng = .Find(what:=SearchStr, after:=.Cells(.Cells.Count), lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
    End With

    If Not rng Is Nothing Then
        If InStr(rng.Value, SearchStr) = 1 Then
            S = Right(rng.Value, 8)

            Set WS = Worksheets("summary")
            If WS.Range("A23").Value = "" Then
                WS.Range("A23").Value = S
            Else
                With WS
                    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                    .Range("A" & LastRow + 1).Value = S
                End With
            End If
        End If
    Else
        MsgBox "'" & SearchStr & "' not found", vbExclamation
    End If
End Sub
 
Upvote 0
One way.
VBA Code:
Sub FindAndCopy()
    Dim WS As Worksheet
    Dim rng As Range
    Dim S As String, SearchStr As String
    Dim LastRow As Long

    Set WS = Worksheets("data")
    SearchStr = "FB09"

    With WS.Range("A2", WS.Range("A" & WS.Rows.Count).End(xlUp))
        Set rng = .Find(what:=SearchStr, after:=.Cells(.Cells.Count), lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
    End With

    If Not rng Is Nothing Then
        If InStr(rng.Value, SearchStr) = 1 Then
            S = Right(rng.Value, 8)

            Set WS = Worksheets("summary")
            If WS.Range("A23").Value = "" Then
                WS.Range("A23").Value = S
            Else
                With WS
                    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                    .Range("A" & LastRow + 1).Value = S
                End With
            End If
        End If
    Else
        MsgBox "'" & SearchStr & "' not found", vbExclamation
    End If
End Sub

Unfortunately I get the error 'FB09' not found. If I do the find manually it is found. I recorded the following and this worked too. Do you have any ideas?


VBA Code:
    Columns("S:S").Find(What:="FB09", After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=True, SearchFormat:=False).Activate
 
Upvote 0
Please post your column S data in a form that can be copied so I can test my code against it. If the data contains anything sensitive, then replace it with dummy data.

I recommend using xL2BB.

 
Upvote 0
TEST.xlsm
ST
1NarrativeCustomer reference
2FB0947704537522FB0947704537522
3FB0960939455541FB0960939455541
4FB0956903531552FB0956903531552
5FB0983143801318FB0983143801318
6FB0967970038505FB0967970038505
7FB0917724215771FB0917724215771
8FB0952434554610FB0952434554610
9FB0989962911620FB0989962911620
10FB0931218720619FB0931218720619
11FB0980596412871FB0980596412871
12FB0918470040691FB0918470040691
13FB0910948105013FB0910948105013
14FB0985948066972FB0985948066972
15FB0979225549536FB0979225549536
16FB0973563849218FB0973563849218
17FB0925868354143FB0925868354143
18ST1118627945595ST1118627945595
19ST1126231450826ST1126231450826
20ST1187294739536ST1187294739536
21ST1112982251172ST1112982251172
22ST1151393551196ST1151393551196
23CV2484567100327CV2484567100327
24CV2487325486059CV2487325486059
Data
 
Upvote 0
Does rlv01's code work if you change the find range to be S instead of A ?
Rich (BB code):
    With WS.Range("S2", WS.Range("S" & WS.Rows.Count).End(xlUp))
 
Upvote 0
Does rlv01's code work if you change the find range to be S instead of A ?
Rich (BB code):
    With WS.Range("S2", WS.Range("S" & WS.Rows.Count).End(xlUp))
Hi yes!

This has worked for the first instance. But doesn't pick up any subsequent instances. Is there a way to add this in?

Many thanks
 
Upvote 0
Is it just column S that you want to search for FB09 in?
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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