+ Reply to Thread
Results 1 to 20 of 20

Insert user's name into cell based on windows user login when a button is clicked.

  1. #1
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Insert user's name into cell based on windows user login when a button is clicked.

    I need to modify my plain text thoughts into VBA so that it does the following:

    sub requestPTO01()

    If D20 is blank, then insert the user's name based on their windows login into D20 and date/time stamp G20.

    If D20 is not blank, but J20 is blank, then insert the user's name based on their windows login into J20 and date/time stamp M20.

    If D20 and J20 are both not blank, but P20 is blank, then insert the user's name based on their windows login into P20 and date/time stamp S20.

    If D20, J20, AND P20 are all not blank, then give the user the following msgbox notification: "Notification Text Here."

    End sub

    All users' windows login is based on their initials, so I would imagine i would also have to tell the code what initials belong to what name, such as "jad" = "John Allen Doe".

    Any help in generating this VBA is greatly appreciated!
    Last edited by jonvanwyk; 08-17-2011 at 09:42 AM.

  2. #2
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Hi jonvanwyn,

    Below is the code to enter a date stamp into a cell and a username into a different cell. Let me know if you can adapt this to meet your needs. If you need more specific help with your VBA, can you possibly post a copy of your Excel Workbook in .xls format so that I can take a look.

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Here is what I have so far, but it does not work. All this does is put the date and time into G20 when the button is pressed, and only if the worksheet is unlocked. If the worksheet is locked, it still puts the date and time into G20, but it also gives me the following debugging error:

    'Run-time error 1004'
    Unable to set the NumberFormat property of the Range class

    Please Login or Register  to view this content.

    I basically want it to so when I, the user, click on the button, it populates my name into D20 and time stamps G20; even if those cells are locked. This means the only way to change those cells is to click on the macro button. The user should not be able to delete their name once it is in there.
    Last edited by jonvanwyk; 08-05-2011 at 02:57 PM.

  4. #4
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Understood. I wasn't sure if you simply needed the syntax for these procedures or if you needed the full procedure. Hence, I included this statment: "Below is the code to enter a date stamp into a cell and a username into a different cell. Let me know if you can adapt this to meet your needs. If you need more specific help with your VBA, can you possibly post a copy of your Excel Workbook in .xls format so that I can take a look." A copy of your workbook can help with testing the code we are developing without having to recreate our vision of what we think the workbook should look like. It can also help us determine if there are tenable workarounds in situations where the code may be a bit tedious.

  5. #5
    Registered User
    Join Date
    08-05-2011
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    2

    Re: Insert user's name into cell based on windows user login when a button is clicked

    hey jonvanwyk

    I found your macro very useful indeed thanks!!

  6. #6
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    @BigBas

    Here is a copy of it saved in .xls

    Please Login or Register  to view this content.

    So, when the button in V20 is clicked, my name should show up in D20 (since my windows login is jmv), and G20 should get time stamped. What am I doing wrong?
    Attached Files Attached Files
    Last edited by jonvanwyk; 08-16-2011 at 09:03 AM.

  7. #7
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Hi Jon,

    Glad I caught this thread. First, I will include code that will hopefully meet your needs with regards to row 20:
    Please Login or Register  to view this content.
    Next, I will make a few comments about the spreadsheet. First, it looks great and it's very well put together. Unfortunately, due to certain limitations in Excel (unless changes have been made in newer versions), the command buttons do not fully interface with the spreadsheet. To be more specific, simply because the "Request" button sits in row 20, excel doesn't recognize it as a button on row 20. With that said, there's not true method of creating one macro that would work with each button. You'd have to create one macro per button to account for each row. Of course, that is both time consuming and will result in a bloated spreadsheet.

    You can use a worksheet change event to trigger the macro, then you can capture the row that is being queried. For example, if a user double clicks on the actual date (ie, cell A20), a macro can be fired that acounts for the PTO

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Insert user's name into cell based on windows user login when a button is clicked

    I have a slightly different opinion:

    - do not use merged cells in combination with VBA
    - not every macro needs a separate module (put the into 1)
    - avoid using 'select' and 'activate' in VBA
    - macro's that only apply to a certain worksheet can be put into the sheet's macromodule
    - you better not use VBA to format cells, if that can be done beforehand.

    Please Login or Register  to view this content.
    if no merged cells:

    Please Login or Register  to view this content.



  9. #9
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Thanks guys...I will see if I can get it to work for me! :-)

    snb: If you see my original post, there are also conditions that need to be met based on cells that are blank or not. Also:

    - do not use merged cells in combination with VBA
    For aesthetic reasons I need to used merged cells with VBA.

    - not every macro needs a separate module (put the into 1)
    This would be great if I knew how...your help is appreciated

    - avoid using 'select' and 'activate' in VBA
    I will do my best, but it seems many suggestions I get (that work) do this.

    - macro's that only apply to a certain worksheet can be put into the sheet's macromodule
    I realize this, but most of my macros serve duty on multiple worksheets.

    - you better not use VBA to format cells, if that can be done beforehand.
    I usually don't format cells with VBA. I think I did in this case because the format I wanted was not available as an option otherwise?

  10. #10
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    @bigbas

    My plan was to create a macro that just worked for row 20, and then duplicate it into a unique macro for each line after that. Right now I am just trying to get a "template macro" that works and can be modified.

    @everyone:

    I will have multiple users that will be clicking on these "request" and "volunteer" buttons. The form needs to recognize who is using it and just insert their name in the first available box and then time stamp it.

  11. #11
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Jon,

    If that's the case, try the code that I provided above for line 20. Also, I noticed that you wanted to convert the "username" into a full name. Right now, this will only work for your name. If you have a list of everyone's username and full name, you can include them on a separate worksheet (hide it if necessary), and have the macro search for the username (ie, jwb) and return the full name (ie, Jon Van Wyk).

    Post back with any questions or problems you have adapting the code.

  12. #12
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    BigBas

    You recognized my next question before I asked it. Can't I do something like this? ...

    Please Login or Register  to view this content.

  13. #13
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Jon,

    That would also work, especially if there are only a couple of people who use the spreadsheet and they don't change very often. If you have many users, or there is a high turnaround in users, it can become annoying and inefficient to have to code an if...than statement for each possible user name. In that case, a lookup into a table may be easier. In the case of three users, perhaps another SELECT CASE statement can tidy the code:

    Please Login or Register  to view this content.

  14. #14
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    There are 10 users and they change rarely.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Keep it simple

    Please Login or Register  to view this content.

  16. #16
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    The following is what I came up with combining both suggestions. Based on my windows user login of "jmv", it enters Melanie Waters into D20, and then time stamps M20 instead of G20.



    Please Login or Register  to view this content.



    I think my brain is incapable of keeping it simple. I should note that BigBas's suggestion functions exactly as I want it to, except I could not get it to work when incorporating multiple users. BigBas, can you show how you would use your Case Select code in the overall scheme?

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Please Login or Register  to view this content.

  18. #18
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    snb...

    That puts my name in D20, then timestamps M20.

    Update: I see now that this code was if I do not used merged cells, which I do, so I have to modify the other.

    Please Login or Register  to view this content.
    Last edited by jonvanwyk; 08-16-2011 at 12:52 PM.

  19. #19
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    snb...your kung fu is too strong for me. I find that my modified version of BigBas's code below meets my needs.

    Please Login or Register  to view this content.

  20. #20
    Forum Contributor jonvanwyk's Avatar
    Join Date
    06-28-2010
    Location
    Iowa, USA
    MS-Off Ver
    Excel 2010
    Posts
    452

    Re: Insert user's name into cell based on windows user login when a button is clicked

    Now...can I turn said macro "on" and "off" at will? For instance, if users can only request time off during a certain time period, I need to be able to enable and disable just the REQUEST macro. Thoughts?

    Update: I will start a new thread for this question.

+ 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