First Name only

blee4372

New Member
Joined
Apr 25, 2017
Messages
41
I have a macro that get me Date / time & the username's full name in this format "Doe, John". What I want is to just get the first name "John". Here is the macro I am using:

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim ws As Worksheet
Set ws = ActiveSheet
With ws.Range("G1")
.Value = Now
.NumberFormat = "ddmmmyy hh:mm"
End With
Set WSHnet = CreateObject("WScript.Network")
UserName = WSHnet.UserName
UserDomain = WSHnet.UserDomain
Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
UserFullName = objUser.FullName
ws.Range("H1") = objUser.FullName

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
try this maybe

Code:
Dim firstName as String
Dim space As Integer
space = InStrRev(UserFullName, " ")
firstName= Right(UserFullName, Len(UserFullName) - space)
 
Last edited:
Upvote 0
This should return what you want:
Code:
FirstName= Trim(Mid(UserFullName, InStrRev(UserFullName, ",") + 1))
 
Upvote 0
That would depend if all users that could use the file had the same format of name ie Last, First. But something like this does your format:

Code:
ws.Range("H1") = Trim(Split(UserFullName, ",")(1))
 
Upvote 0
That would depend if all users that could use the file had the same format of name ie Last, First. But something like this does your format:

Code:
ws.Range("H1") = Trim(Split(UserFullName, ",")(1))

Thanks Steve the fish, this was what I was look for :)
Thank everyone for their reply
 
Last edited:
Upvote 0
That would depend if all users that could use the file had the same format of name ie Last, First. But something like this does your format:

Code:
ws.Range("H1") = Trim(Split(UserFullName, ",")(1))
@steve the fish
WHOA!!!
Please tell me that the (1) in your code returns the second index of the split...... cuz if thats true then that is freaking amazing!!!! i never knew that!
 
Last edited:
Upvote 0
Please tell me that the (1) in your code returns the second index of the split...... cuz if thats true then that is freaking amazing!!!! i never knew that!
Yeah, I forgot about the SPLIT function. It definitely is the most efficient way of doing it.
And yes, 1 returns the second field in the split, as the index is zero-based (so 0 would return the first field).
 
Last edited:
Upvote 0
Yeah, I forgot about the SPLIT function. It definitely is the most efficient way of doing it.
And yes, 1 returns the second field in the split, as the index is zero-based (so 0 would return the first field).

:eek: Mind....Blown
 
Upvote 0

Forum statistics

Threads
1,214,521
Messages
6,120,018
Members
448,937
Latest member
BeerMan23

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