How to make username and today static.

J_W

New Member
Joined
Sep 14, 2021
Messages
20
Office Version
  1. 365
Platform
  1. Windows
Good day,
I have created a task tracking sheet used by multiple users. I have two columns that are causing issues. The first one is a username column that when a user checks a checkbox to mark the task as complete, it automatically puts the username in using the following VB code in a module:
VBA Code:
Public Function UserName()
UserName = Environ$("UserName")
End Function
The problem is that the username is overwritten based on whoever opens it. I need the username to be recorded based on who checked the checkbox and then be static and not get overwritten.

My second issue is the column that records the current date using the today() formula. For each task, the date is recorded when the task was completed, but obviously this result is overwritten each time the sheet is opened on a new date. I also need this value to be recorded based on when the checkbox is checked but then be static.

Is this wishful thinking or actually possible to accomplish?

Thanks in advance!
 
This will apply the same .OnAction to each check box on the sheet
VBA Code:
Sub ApplyOnAction()
    Dim chk As CheckBox
   
For Each chk In ActiveSheet.CheckBoxes
    With chk
        .OnAction = "Write_User_and_Date"
    End With
Next chk
   
End Sub
Thank you NoSparks! I will see if I can implement this. I am excited to see how this works.
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I would use the .OnAction property of the check boxes so they all call a macro like this
VBA Code:
Sub Write_User_and_Date()
   
    Dim whoCalled As Range
   
Set whoCalled = Range(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address(0, 0))

Select Case whoCalled.Column
    Case 6, 10
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Environ$("UserName")
        Else
            whoCalled.Offset(, 1) = ""
        End If
    Case 8, 12
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Date
        Else
            whoCalled.Offset(, 1) = ""
        End If
End Select

End Sub
NoSparks - this is genius!!! And extremely easy to implement. I have never seen such efficient code. It applied it to all check boxes at once! Once the macro is complete, it makes cell D4 the active cell so it's hard to know where I just clicked. Do you have any suggestions to make the active cell the same as the checkbox that was just clicked (or at least the same row)?
 
Upvote 0
NoSparks - this is genius!!! And extremely easy to implement. I have never seen such efficient code.
I think it is only right to mark NoSparks's suggestion as solution.
Once the macro is complete, it makes cell D4 the active cell so it's hard to know where I just clicked.
The code does not make any changes to the selection. Did you make any modifications?
Do you have any suggestions to make the active cell the same as the checkbox that was just clicked (or at least the same row)?
Try to add this to the end of Write_User_and_Date:
VBA Code:
whoCalled.Select
 
  • Like
Reactions: J_W
Upvote 0
Glad you liked my solution.
Like bobsan42 said... There are many ways to skin a cat

add 2 lines to the macro and see if that's satisfactory

Rich (BB code):
Select Case whoCalled.Column
    Case 6, 10
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Environ$("UserName")
        Else
            whoCalled.Offset(, 1) = ""
        End If
        whoCalled.Activate
    Case 8, 12
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Date
        Else
            whoCalled.Offset(, 1) = ""
        End If
        whoCalled.Activate
End Select
 
  • Like
Reactions: J_W
Upvote 0
Actually, I'll go with bob's suggestion, as it will look after the column O check boxes too, thanks bob
VBA Code:
Sub Write_User_and_Date()
    
    Dim whoCalled As Range
    
Set whoCalled = Range(ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address(0, 0))

Select Case whoCalled.Column
    Case 6, 10
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Environ$("UserName")
        Else
            whoCalled.Offset(, 1) = ""
        End If
    Case 8, 12
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Date
        Else
            whoCalled.Offset(, 1) = ""
        End If
End Select

whoCalled.Activate

End Sub
 
  • Like
Reactions: J_W
Upvote 0
Glad you liked my solution.
Like bobsan42 said... There are many ways to skin a cat

add 2 lines to the macro and see if that's satisfactory

Rich (BB code):
Select Case whoCalled.Column
    Case 6, 10
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Environ$("UserName")
        Else
            whoCalled.Offset(, 1) = ""
        End If
        whoCalled.Activate
    Case 8, 12
        If whoCalled = True Then
            whoCalled.Offset(, 1) = Date
        Else
            whoCalled.Offset(, 1) = ""
        End If
        whoCalled.Activate
End Select
That did it! Seriously brilliant! Thank you so much. :)
 
Upvote 0
Just one more thing. I am not 100% certain, but i think you'll have to run the .onaction assignment code every time the workbook is opened or the vb project state is lost.
NoSparks should confirm it though, I can't test it at present.
 
Upvote 0
Thank you Bobsan42 and NoSparks. This is my first time posting in an Excel forum and this has been a really great experience. You were both so helpful and really nice. I am use to posting in SAS forums and getting my head chewed off if I don't format something right. This has been a smooth enjoyable time. Thank you so much for taking the time to help me today. I hope you both have an amazing week because you just saved me a week of pulling my hair out. :)
 
Upvote 0
Just one more thing. I am not 100% certain, but i think you'll have to run the .onaction assignment code every time the workbook is opened or the vb project state is lost.
NoSparks should confirm it though, I can't test it at present.
I just tested that and everything worked the same when I reopened it.
 
Upvote 0
? Great. Glad it worked. Have a nice rest of your day or night
And please change the accepted solution if possible.
;)
 
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,289
Members
449,077
Latest member
Rkmenon

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