InStr for middle name

Fiske

Board Regular
Joined
Jun 15, 2015
Messages
82
Hi i am doing a code on InStr and got stuck at this.
The Data in column B is the name while the code will input the middle name in Column C
Example: John_p.Doe result will p
i got a code but it keep saying invalid procedure and i dont know why please help thanks!
Code:
Cells(i, "C").Value = Trim(Mid(partno, InStr(1, partno, " ") + 1, InStr(InStr _
(1, partno, "_") - 1, partno, " ") - InStr(1, partno, ".")))
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about posting ALL the code please??
 
Upvote 0
How about posting ALL the code please??
Code:
Sub rev()

Dim partno As String
Dim i As Integer


    Worksheets("sheet1").Activate
        
    For i = 1 To Range("B" & Rows.Count).End(xlUp).Row
        partno = Cells(i, "B").Value
        Cells(i, "C").Value = Trim(Mid(partno, InStr(1, partno, " ") + 1, InStr(InStr _
(1, partno, "_") - 1, partno, " ") - InStr(1, partno, ".")))
    Next
      
End Sub
i am using excel 2003 btw.
 
Upvote 0
Hi

This is all you need :-
Code:
Cells(i, "C").Value = Trim(Mid(partno, InStr(1, partno, "_") + 1, InStr(1, partno, ".") - (InStr(1, partno, "_") + 1)))
to extract the "p" in your sample.

If other samples fail this statement feel free to come back.

hth
 
Upvote 0
Can't test, but I think this will get you going

Code:
Sub rev()
Dim partno As String, i As Integer
    Worksheets("sheet1").Activate
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Cells(i, "C").Formula = "=Mid(" & Cells(i, "B").Address & ", Find(""_""," & Cells(i, "B").Address & ") + 1, Find("".""," & Cells(i, "B").Address & ", 1) - Find("".""," & Cells(i, "B").Address & ", 1) + 1)"
        Cells(i, "C").Value = Cells(i, "C").Value
    Next i
End Sub
 
Upvote 0
Hi Mike! thanks for the reply, your code works great! thanks again.
As i am still a student self-learning VBA, could you explain why my code did not work thanks! :biggrin:
 
Upvote 0
Can't test, but I think this will get you going

Code:
Sub rev()
Dim partno As String, i As Integer
    Worksheets("sheet1").Activate
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
        Cells(i, "C").Formula = "=Mid(" & Cells(i, "B").Address & ", Find(""_""," & Cells(i, "B").Address & ") + 1, Find("".""," & Cells(i, "B").Address & ", 1) - Find("".""," & Cells(i, "B").Address & ", 1) + 1)"
        Cells(i, "C").Value = Cells(i, "C").Value
    Next i
End Sub
Hi Micheal! thanks for the reply! your code works great too but it looks much more complicated. If it is alright, could you explain to me the difference between yours and mine and the one mike gave, as i am still a learning student and would hope to learn more from you guys thanks! ^^
 
Upvote 0
Here is a way to do it without using any loops...
Code:
Sub MiddleInitial()
  Dim LastRow As Long
  Worksheets("Sheet1").Activate
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("C1:C" & LastRow) = Evaluate(Replace("IF(B1:B#="""","""",RIGHT(" & _
                            "LEFT(B1:B#,FIND(""."",B1:B#)-1)))", "#", LastRow))
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,978
Messages
6,122,545
Members
449,089
Latest member
davidcom

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