inStrRev

Wimpieoh

New Member
Joined
Feb 19, 2025
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Cel K14 contains 'johan van der sterren'.

VBA Code:
Sub LastName()
    name = [K14].Value
    MsgBox name                         ' shows johan van der sterren
    spot = InStrRev(name, " ")
    MsgBox spot                          ' shows 22 but it schould be 14; find the last space and count from the beginning
    MsgBox Mid(name, spot)      ' shows space
End Sub

When I put the name hardcoded everything works fine

VBA Code:
Sub LastName()
    name = "johan van der sterren"
    MsgBox name                         '  shows johan van der sterren
    spot = InStrRev(name, " ")
    MsgBox spot                          ' shows 14; OK
    MsgBox Mid(name, spot)      ' shows sterren
End Sub

Anyone an idea why the first code goes wrong? Thanks and best regards
 
What happens if you replace your Name = line with this one:
VBA Code:
    Name = Replace([K14], ChrW(160), "")
 
Upvote 0
Welcome to the MrExcel board!

If Alex's suggestion does not solve it, what happens if you use this for the Name line?
VBA Code:
Name = Trim([K14].Value)

BTW, 'Name' is not a good as a variable name as 'Name' is already used in the vba language. Such double usage may cause unexpected errors in vba code.
 
Last edited:
Upvote 0
Solution

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