Need help in VBA code for Instr function

vinoanbu

New Member
Joined
Oct 19, 2016
Messages
44
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm working in a Excel Macro, in which the following Instr function taking up the text string(Johan) as partial
but I need to make it as a whole.

VBA Code:
If Instr(Cell.value, "Johan,") >0  Or _
Instr(Cell.value, "Johan") >0 Then
cell.offset(0,2).value = "Johan"

For example in cell A1 the text string is "Johan, James, Jason, Johan" for this case the function works fine and returning the value "Johan" in B2
but in Cell A2 the text string is "Johan_VK, Roger" for this case also the function returning the value "Johan" in B2 this is not correct.

could anyone help to improve this Instr section to search as whole text

Regards,
Vino
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
try this:
VBA Code:
If InStr(cell.Value, "Johan,") > 0 Or _
Right(cell.Value, 5) = "Johan" Then
cell.Offset(0, 2).Value = "Johan"
End If
 
Upvote 1
That will work as long as the final name isn't something like VK_Johan. To be certain:

VBA Code:
If InStr(cell.Value, "Johan,") > 0 Or Right(cell.Value, 7) = ", Johan" Then
   cell.Offset(0, 2).Value = "Johan"
End If
 
Upvote 0
try this:
VBA Code:
If InStr(cell.Value, "Johan,") > 0 Or _
Right(cell.Value, 5) = "Johan" Then
cell.Offset(0, 2).Value = "Johan"
End If
Hi,

Thanks for the quick response.
for better understanding please refer the table which is the final output I needed.
the present code taking the text "Johan" as partial one, if it takes as whole text then the problem will be solved. I hope so.


1692551859726.png


Thanks,
Vino
 
Upvote 0
InStr is not what you want to use if you are looking for exact matches in your case examples.

Try the following:
VBA Code:
    Dim i                   As Long
    Dim SplitStringArray    As Variant
'
    SplitStringArray = Split(Cell.Value, ", ")
'
    For i = 0 To UBound(SplitStringArray)
        If SplitStringArray(i) = "Johan" Then
            Cell.Offset(0, 1).Value = "Johan"
            Exit For
        End If
    Next
 
Upvote 0
Solution
InStr is not what you want to use if you are looking for exact matches in your case examples.

Try the following:
VBA Code:
    Dim i                   As Long
    Dim SplitStringArray    As Variant
'
    SplitStringArray = Split(Cell.Value, ", ")
'
    For i = 0 To UBound(SplitStringArray)
        If SplitStringArray(i) = "Johan" Then
            Cell.Offset(0, 1).Value = "Johan"
            Exit For
        End If
    Next
Hi Johnny,

Thanks for the code. It worked perfect !

Regards,
Vino
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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