VBA to trim all letters after a space

WednesdayC

Board Regular
Joined
Nov 7, 2010
Messages
201
Office Version
  1. 2016
Platform
  1. MacOS
Hi All

I apologise for posting twice in one day, but I am under pressure to finish a project.

I am trying to work out how to trim the contents of an InputBox

For instance, assuming I have a variable called FirstName which is read from the string posted to an InputBox and the user enters, say John Doe, instead of John - how can I trim out the Doe and just retrieve John.

Also, if they entered, say, John H Doe, how could I trim out the Doe and just retrieve John H?

Once again, many thanks in advance.

Regards
Wednesday



Regards
Wednesday
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
So would it be fair to say you want to show the text before the LAST space?

Try

Code:
Sub test()
Dim MyString As String, x As Long
MyString = "John H Doe"
x = InStrRev(MyString, " ")
If x = 0 Then x = Len(MyString)
MsgBox Trim(Left(MyString, x))
End Sub
 
Upvote 0
Hi Jonmo

It works! Just what I wanted. A big thank you.

I realise I made the question rather convoluted!!


You guys rock!

Many thanks

Wednesday
 
Upvote 0

Forum statistics

Threads
1,203,600
Messages
6,056,200
Members
444,850
Latest member
dancasta7

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