Pull out names from a string, into multiple columns?

HockeyDiablo

Board Regular
Joined
Apr 1, 2016
Messages
182
Trying to run a quick script to pull a few names out of a string and place them in the column next to the string location. Cant seem to get this going. Any tips would be greatly appreciated.

Sub james()
Dim celltxt As String
celltxt = ActiveSheet.Range("d4").Text


If InStr(1, celltxt, "Sue") Then
Range("e4").Value = "Sue"

ElseIf InStr(1, celltxt, "Kari") Then
Range("e4").Value = "Kari"

ElseIf InStr(1, celltxt, "Holly") Then
Range("e4").Value = "Holly"






Else


MsgBox ("error")
james2
End If
End Sub


Sub james2()
Dim celltxt2 As String
celltxt = ActiveSheet.Range("d4").Text


If InStr(1, celltxt2, "Sue") And Range("e4").Value <> "Sue" Then
Range("g4").Value = "Sue"

ElseIf InStr(1, celltxt2, "Kari") And Range("e4").Value <> "Kari" Then
Range("g4").Value = "Kari"

ElseIf InStr(1, celltxt2, "Holly") And Range("e4").Value <> "Holly" Then
Range("g4").Value = "Holly"






Else




End If
End Sub

JtlxI8


Thanks again.

James
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I think I have got it... seems to work great on testing, now if I can only figure out how to create a loop to do the next row....

Sub james()
Dim celltxt As String
celltxt = ActiveSheet.Range("A1").Value

If InStr(1, celltxt, "Sue", vbTextCompare) Then
Range("B1").Value = "Sue"

ElseIf InStr(1, celltxt, "Kari", vbTextCompare) Then
Range("B1").Value = "Kari"

ElseIf InStr(1, celltxt, "Holly", vbTextCompare) Then
Range("B1").Value = "Holly"

Else

MsgBox ("error")
End If
james2

End Sub
_________________________________________

Sub james2()

Dim celltxt As String
celltxt = ActiveSheet.Range("A1").Value


If InStr(1, celltxt, "Sue") And ActiveSheet.Range("b1").Value <> "Sue" Then
Range("C1").Value = "Sue"

ElseIf InStr(1, celltxt, "Kari") And ActiveSheet.Range("b1").Value <> "Kari" Then
Range("C1").Value = "Kari"

ElseIf InStr(1, celltxt, "Holly") Then
Range("C1").Value = "Holly"

Else

MsgBox ("error")
End If

End Sub
 
Upvote 0
One approach to looping is the For/Next construct as in the example below...

Code:
Sub james2()

Dim celltxt As String
[COLOR=#0000ff]Dim i As Long[/COLOR]

[COLOR=#0000ff]For i = 1 To 10[/COLOR]
    celltxt = ActiveSheet.Range("A" & [COLOR=#0000ff]i[/COLOR]).Value
    If InStr(1, celltxt, "Sue") And ActiveSheet.Range("b" & [COLOR=#0000ff]i[/COLOR]).Value <> "Sue" Then
        Range("C" & [COLOR=#0000ff]i[/COLOR]).Value = "Sue"
    ElseIf InStr(1, celltxt, "Kari") And ActiveSheet.Range("b" & [COLOR=#0000ff]i[/COLOR]).Value <> "Kari" Then
        Range("C" & [COLOR=#0000ff]i[/COLOR]).Value = "Kari"
    ElseIf InStr(1, celltxt, "Holly") Then
        Range("C" & [COLOR=#0000ff]i[/COLOR]).Value = "Holly"
    Else
        MsgBox ("error")
    End If
[COLOR=#0000ff]Next i[/COLOR]

End Sub

Cheers,

tonyyy
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,478
Members
448,967
Latest member
visheshkotha

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