code to enter date as text

gheyman

Well-known Member
Joined
Nov 14, 2005
Messages
2,341
Office Version
  1. 365
Platform
  1. Windows
I need to add the date to a cell that I am using as a Unique Identifier. I want to use the date so I need to transform how it appears in the cell.
example for today, which would be 3/29/2021 16:58 what I am trying to populate the cell with is 103291658 (just the last digit from the year so 2021 become 1).
I also want to incorporate the users initials using CurrentUser = Environ("UserName") . Here the user the user will always be there first name and last name with a dot in-between (example Joe.Smith)

in the end the code would produce JS103291658 in a cell

Any help is appreciated. Thank you!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try:

VBA Code:
Range("A1").Value = Left(Environ("UserName"), 1) & Left(Split(Environ("UserName") & ".", ".")(1), 1) & _
                    Right(Date, 1) & Format(Month(Now), "00") & Format(Day(Now), "00") & _
                    Format(Hour(Now), "00") & Format(Minute(Now), "00")
 
Upvote 0
Rich (BB code):
Range("A1").Value = Left(Environ("UserName"), 1) & Left(Split(Environ("UserName") & ".", ".")(1), 1) & _
                    Right(Date, 1) & Format(Month(Now), "00") & Format(Day(Now), "00") & _
                    Format(Hour(Now), "00") & Format(Minute(Now), "00")
You can replace the part I highlighted in red with this much shorter code fragment...
VBA Code:
Mid(Format(Now, "yymmddhhmm"), 2)
 
Upvote 0
A tweak to your tweak:

Rich (BB code):
Mid(Format(Now, "yymmddhhnn"), 2)

You need to use n for minutes.
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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