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

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Ok. Try this
VBA Code:
Sub FindAndCopyM()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim R As Range, SearchRange As Range
    Dim S As String, SearchStr As String ,FirstAddr As String
    Dim LastRow As Long
    Dim FirstPass As Boolean

    Set WS1 = Worksheets("data")
    Set WS2 = Worksheets("summary")

    SearchStr = "FB09"

    Set SearchRange = WS1.Range("S1", WS1.Range("S" & WS1.Rows.Count).End(xlUp))

    With SearchRange
        Set R = .Find(what:=SearchStr, after:=.Cells(.Cells.Count), lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
    End With

    If Not R Is Nothing Then
        FirstAddr = R.Address
        FirstPass = True
    End If

    Do While Not R Is Nothing
        If Not FirstPass Then
            Set R = SearchRange.Find(what:=SearchStr, after:=R, lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
        End If

        If Not FirstPass Then
            If R.Address = FirstAddr Then
                Set R = Nothing
                Exit Do
            End If
        Else
            FirstPass = False
        End If
        
        If Not R Is Nothing Then
            If InStr(R.Value, SearchStr) = 1 Then
                S = Right(R.Value, 8)
                If WS2.Range("A23").Value = "" Then
                    WS2.Range("A23").NumberFormat = "@"
                    WS2.Range("A23").Value = S
                Else
                    With WS2
                        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                         .Range("A" & LastRow + 1).NumberFormat = "@"
                        .Range("A" & LastRow + 1).Value = S
                    End With
                End If
            End If
        End If
    Loop
End Sub
 
Upvote 0
Solution
Ok. Try this
VBA Code:
Sub FindAndCopyM()
    Dim WS1 As Worksheet, WS2 As Worksheet
    Dim R As Range, SearchRange As Range
    Dim S As String, SearchStr As String ,FirstAddr As String
    Dim LastRow As Long
    Dim FirstPass As Boolean

    Set WS1 = Worksheets("data")
    Set WS2 = Worksheets("summary")

    SearchStr = "FB09"

    Set SearchRange = WS1.Range("S1", WS1.Range("S" & WS1.Rows.Count).End(xlUp))

    With SearchRange
        Set R = .Find(what:=SearchStr, after:=.Cells(.Cells.Count), lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
    End With

    If Not R Is Nothing Then
        FirstAddr = R.Address
        FirstPass = True
    End If

    Do While Not R Is Nothing
        If Not FirstPass Then
            Set R = SearchRange.Find(what:=SearchStr, after:=R, lookat:=xlPart, MatchCase:=True, searchdirection:=xlNext)
        End If

        If Not FirstPass Then
            If R.Address = FirstAddr Then
                Set R = Nothing
                Exit Do
            End If
        Else
            FirstPass = False
        End If
       
        If Not R Is Nothing Then
            If InStr(R.Value, SearchStr) = 1 Then
                S = Right(R.Value, 8)
                If WS2.Range("A23").Value = "" Then
                    WS2.Range("A23").NumberFormat = "@"
                    WS2.Range("A23").Value = S
                Else
                    With WS2
                        LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
                         .Range("A" & LastRow + 1).NumberFormat = "@"
                        .Range("A" & LastRow + 1).Value = S
                    End With
                End If
            End If
        End If
    Loop
End Sub
This is perfect thank you :)
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,191
Members
453,021
Latest member
pingpong7117

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