Extract Name after comma (Username)

Andresuru

New Member
Joined
Sep 6, 2021
Messages
33
Office Version
  1. 365
Platform
  1. Windows
Hi Team,

Please note I am trying to put in place a welcome message in userform (label) but the application username needs to be restricted to only show the Name of the user ( That it is after the coma)

The code is working correct but unfortunately I need to extract only the value after the "," that application. Username generates

This needs to be dynamic since usernames can be larger or smaller but imagine a user that his name is Mata Oviedo , Carlos so the value to extract is "Carlos" (since it is after the coma)

VBA Code:
Private Sub Workbook_Open()

        If Hour(Now) > 12 Then
        
            UserForm1.Hello.Caption = " Good Afternoon " & [B]Application.UserName[/B]
            
        ElseIf Hour(Now) < 12 Then
                
                UserForm1.Hello.Caption = " Good Morning " & [B]Application.UserName[/B]
                   
          
        End If

           UserForm1.Show

Hope this can be clear for you
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
User Name may be as simple as just the first name depending on certain entries made during computer setup. I'd test the user name to ascertain if it contains a comma. Like this:
VBA Code:
Sub test()
Dim Nam
Nam = "Mata Oviedo , Carlos"
If InStr(Nam, ",") > 0 Then
    MsgBox Right(Nam, Len(Nam) - InStrRev(Nam, ",") - 1)
Else
    MsgBox "User's name has no comma in it"
End If
End Sub
 
Upvote 0
Another option that you could try

VBA Code:
Private Sub Workbook_Open()
  UserForm1.Hello.Caption = " Good " & IIf(Hour(Now) < 12, "Morning ", "Afternoon ") & Trim(Mid("," & Application.UserName, InStrRev("," & Application.UserName, ",") + 1))
  UserForm1.Show
End Sub
 
Upvote 0
Solution
User Name may be as simple as just the first name depending on certain entries made during computer setup. I'd test the user name to ascertain if it contains a comma. Like this:
VBA Code:
Sub test()
Dim Nam
Nam = "Mata Oviedo , Carlos"
If InStr(Nam, ",") > 0 Then
    MsgBox Right(Nam, Len(Nam) - InStrRev(Nam, ",") - 1)
Else
    MsgBox "User's name has no comma in it"
End If
End Sub
Hi Joe,

Thanks for reply but I already check the path of the usernames in my workplace and all users has the name after the coma
 
Upvote 0
Another option that you could try

VBA Code:
Private Sub Workbook_Open()
  UserForm1.Hello.Caption = " Good " & IIf(Hour(Now) < 12, "Morning ", "Afternoon ") & Trim(Mid("," & Application.UserName, InStrRev("," & Application.UserName, ",") + 1))
  UserForm1.Show
End Sub

Thanks Peter this works perfectly.
 
Upvote 0
Thanks Peter this works perfectly.
You're welcome. Glad we could help. :)

I already check the path of the usernames in my workplace and all users has the name after the coma
In that case my code could be simplified a little as I had also built in a safety in case there was no comma.

Rich (BB code):
UserForm1.Hello.Caption = " Good " & IIf(Hour(Now) < 12, "Morning ", "Afternoon ") & Trim(Mid("," & Application.UserName, InStrRev("," & Application.UserName, ",") + 1))
UserForm1.Hello.Caption = " Good " & IIf(Hour(Now) < 12, "Morning ", "Afternoon ") & Trim(Mid(Application.UserName, InStrRev(Application.UserName, ",") + 1))
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,155
Members
453,021
Latest member
Justyna P

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