+ Reply to Thread
Results 1 to 12 of 12

Splitting and Manipulating a String

  1. #1
    Registered User
    Join Date
    05-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    52

    Splitting and Manipulating a String

    Hello,

    I need to split a username string into a specific format. For example if the name is "john.doe", I then have to display "J. Doe" in TextBox136. Note, the period and the space following the first initial as well as the capitalized initials.

    I have a working function which finds the username of the person logged in called UserName. All account names that I'm dealing with are in the format "firstname.lastname" and the variable "UserName" is assigned the value of the account name. In the example above, my UserName variable will automatically be assigned the value of "john.doe".



    Please help, and Thanks!

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Splitting and Manipulating a String

    This works for me:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    05-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Splitting and Manipulating a String

    Thanks, but that doesn't work for me. I used your function and then in the initialize event I put: TextBox136.Value = (GetUser) and I get a "Run-time erroe 9, Subscript out of range" warning.

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Splitting and Manipulating a String

    Are you sure there's a full stop in that users windows logon. Or are you using your own username system?

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Splitting and Manipulating a String

    if I understand well
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Splitting and Manipulating a String

    Sorry Kyle, are you asking if there's a period in each username? I'm not sure what a fullstop is. And, no, I'm not using my own system. All login account names at work are in the format "firstname.lastname".

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Splitting and Manipulating a String

    @patel45, some variable declaration would be nice, also it's generally not a good idea to use variable names that are key/reserved words in VBA

  8. #8
    Registered User
    Join Date
    05-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Splitting and Manipulating a String

    @patel, thanks for the code, but that doesn't seem to work either.

    I tried "TextBox136.Value = UserFinal" in both the initialize event and then after the code you gave me and the textbox remained empty.

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Splitting and Manipulating a String

    Yes, over this side of the pond a period is called a full stop

    You'd only get an error like that if there wasn't a full stop in the username - how are you getting the username?

    The fact that Environ("username") returns something that doesn't have a full stop suggests that you're getting the username from somewhere else.

    What is the result of just calling the function Environ("username") alone?

  10. #10
    Registered User
    Join Date
    05-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Splitting and Manipulating a String

    I put
    "usr = Environ("UserName")
    MsgBox (usr)"
    in my initialize event for my UserForm.

    I then called the function "usr" via msgbox. It returned me a blank value.

  11. #11
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Splitting and Manipulating a String

    That's quite strange. Can you post your code for getting the username?

    I'm confused by your terminology also, usr isn't a function?

  12. #12
    Registered User
    Join Date
    05-29-2012
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    52

    Re: Splitting and Manipulating a String

    Please Login or Register  to view this content.


    ---------- Post added at 10:38 AM ---------- Previous post was at 10:31 AM ----------

    Got it to work. Turns out since UserName lready used the "Environ" function, I removed "Environ" from the "getUser" function you made and it works perfectly. Thanks, Kyle!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1