Excel vba Extract specific text from a cell containing multiple line

muzaffas

New Member
Joined
Nov 5, 2014
Messages
5
Hello,
I have a column (col D- starts from cell D2). The Cells D2, D3 ........DXXX, each cell contains multiple lines of data. There are about 40+ individual lines in each cell with different values
Cell D2:
Date: 10-21-2008
First Name: Tommy
Last Name: Hughes
Transaction: 1
Inhouse: No
Job:Yes

Cell D3:
Job:Yes
First Name: Diana
Last Name: Princess
Transaction: 1
Inhouse: No
Date: 10-21-2008

How do I extract dynamically any text after First Name: i.e. "Tommy" (the length of the name is dynamic)
How do I extract dynamically any text after Last Name: i.e. "Princess" (the length of the name is dynamic)
The line positions of First Name or Last Name is different in each cell. Sometime First Name is Line 2 in one cell and Line 20 in another cell

Appreciate if please someone give me VBA code
1. run through the loop to read all populated cells in Col D
2. Extract specific string (dynamic length).
3. Once I have the First Name value then I can paste into another cell
4. Once I have the Last Name value then I can paste into another cell

Thanks
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
This should extract the first and last names.

Code:
Sub FirstLast()
    Dim firstName As String
    Dim lastName As String
    Dim a As Variant
    Dim dRange As Range, r As Range
    
    Set dRange = Range("D2", Cells(Rows.Count, 4).End(xlUp))
    For Each r In dRange
        a = Split(r.Value, "First name:")
        If UBound(a) > 0 Then
            firstName = Trim(Left(a(1), InStr(1, a(1), Chr(10))))
            a = Split(ActiveCell.Value, "Last name:")
            lastName = Trim(Left(a(1), InStr(1, a(1), Chr(10))))
        
            'do something with first and last names
            
        End If
    Next r
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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