Extracting a string out of another string

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

In the following example:

Responsibility: Name1 / Name2 / Name3

How would I extract out Name1 in a macro and put it an an adjacent cell?

Thank you,

Steven
 
In the following example:

Responsibility: Name1 / Name2 / Name3

How would I extract out Name1 in a macro and put it an an adjacent cell?

Thank you,

Steven

How about this

Sub extract_first_name()
' where the cell A1 contains "name1/name2/name3"
Dim sResponsibility As String
Dim sFirstName As String
sResponsibility = Cells(1, 1)
sFirstName = Left(sResponsibility, InStr(1, sResponsibility, "/") - 1)
Cells(1, 2) = sFirstName
End Sub

Or if you have a whole column of cells containing multiple names
then ....
Sub extract_first_name_colum()
Dim sResponsibility As String
Dim sFirstName As String
Dim myRow As Integer
myRow = 1

Do Until Cells(myRow, 1) = ""
sResponsibility = Cells(myRow, 1)
sFirstName = Left(sResponsibility, InStr(1, sResponsibility, "/") -
1)
Cells(myRow, 2) = sFirstName
myRow = myRow + 1
Loop
End Sub

Cath
 
Without any other instruction from you, I'll simply assume that text is in
the ActiveCell. Put this code in your macro...

With ActiveCell
.Offset(0, 1).Value = Trim(Split(Split(.Value, ":")(1), "/")(0))
End With

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top