I have a cell in an Excel file that I want to populate with the current date&time and the user name. The cell is populated by clicking a button.
Until now I was not using the function Application.UserName and the date & time output was formatted correctly (e.g: 07.01.2014 16:57)
After I have added the & Space(1) & Application.UserName piece of coding the formatting of date & time is not formatted the way I want (depending on the user that populates the cell I get results like: 09-Jan-14 11:30:13 + USERNAME or 1/7/2014 5:59:43 PM + USERNAME)
I guess that it takes the formatting from the user's regional settings, correct ?
My question is how can I override those settings as it looks that the ActiveCell.NumberFormat = "dd.mm.yyyy hh:mm" piece of code is no longer working good.
Below is my code:
Private Sub CommandButton1_Click()
ActiveCell = Now() & Space(1) & Application.UserName
ActiveCell.NumberFormat = "dd.mm.yyyy hh:mm"
End Sub
The Date/Time NumberFormat will only work if the cell has a valid Date/Time. So the trick is to change the format first and then add the username.
Is this what you are trying?
With ActiveCell
.Value = Now()
.NumberFormat = "dd.mm.yyyy hh:mm"
.Value = .Text & Space(1) & Application.UserName
End With
Related
I have a database that I need to store a bunch of times and do calculations based on the times, however, the times can be over different days so using just a time format wont calculate the correct difference between the two. For ease of use I wanted to limit how much of the date they have to input because there is a bunch of times and I dont want to have to type the full date each time. I created the following custom format: 16-Jan 15:00 (d-mmm h:nn)
I also created a custom input mask to go with this: 90-LLL\ 90:00
My form fields have the same format and input masks to match. The initial input of the field works right, the issue comes when I try to edit a field and change one digit. It pops up with the error that it doesnt match the input mask, even though it does. In order to change the field I have to delete everything, remove focus from the textbox and then click back in. Is there anyway to fix that? Or is there another option to calculate the difference between the times without having to use the date when it could be over 24hrs long (not longer than 48 though)
Yes, you can have a checkbox, NextDay, to mark if the end time is of the following day.
Then your timespan will be:
Dim Timespan As Date
Timespan = CDate([EndTime] - [StartTime] + Abs([NextDay]))
To input the time, you can use the method here: Entering 24-hour time
To format and display your timespan, also for values above 24 hours, use a function like this:
Public Function FormatHourMinute( _
ByVal datTime As Date, _
Optional ByVal strSeparator As String = ":") _
As String
' Returns count of days, hours and minutes of datTime
' converted to hours and minutes as a formatted string
' with an optional choice of time separator.
'
' Example:
' datTime: #10:03# + #20:01#
' returns: 30:04
'
' 2005-02-05. Cactus Data ApS, CPH.
Dim strHour As String
Dim strMinute As String
Dim strHourMinute As String
strHour = CStr(Fix(datTime) * 24 + Hour(datTime))
' Add leading zero to minute count when needed.
strMinute = Right("0" & CStr(Minute(datTime)), 2)
strHourMinute = strHour & strSeparator & strMinute
FormatHourMinute = strHourMinute
End Function
I need to get todays date + 14 days string formatted in standard dd.mm.yyyy format into Libre Office calc cell.
I already tried the code below, but I lack the knowledge to cope with "Object variable not set" error.
REM ***** BASIC *****
sub Datumplus14
rem ----------------------------------------------------------------------
rem define variables
dim document as object
dim dispatcher as object
Dim cell as object
dim term as date
rem ----------------------------------------------------------------------
rem get access to the document
document = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
term = today()
cell.String = DateAdd("d", 14, datum)
dispatcher.executeDispatch(document, ".uno:EnterString", "", 0, cell)
end sub
Different ideas on how to get this done instead of macro is welcome as well.
Hmm, not sure if this will answer the specifics of what you're actually trying to do, but you can easily get the current date and just +14 to it directly in a cell formula.
Like so:
=NOW()+14
The rest is just applying the required date format to that cell. You can also grab the date from another cell too.
Function myFunction() As String
myFunction = Format(Now()+14, "dd.mm.yyyy")
End Function
I Do have a form with a sub-form (continuous form) on which I would like to apply a date range filter. The thing is that it works partially.
I am using a simple piece of VBA that seem to be THE method. I am using two controls (start & End) and a button to apply the filter.
He is my code sample
With Me.Sub_Desi_Schedule.Form
.Filter = "[Task_End] " & " BETWEEN " & "#" & DateStart & "#" & " AND " & "#" & DateEnd & "#"
.FilterOn = True
And it works fine except when I pick a date range with a value in the last days of the month (as starting period) AND any values within the first week of the next month (as ending period).
Results are blanks even if there is values AND no error message of any kind to help me trouble shooting.
I thought that it could have been something with the date format. I tried to force it to DDMMYYY. No effect and It had the same strange behavior.
And for any other ranges picked later on the month, it works fine...
Does anyone here had this problem before? Is there something obvious I am missing?
I suspect you're right that the problem is due to date format.
Examine the completed filter string the code creates. Do that by using a variable to hold it. Then you can use Debug.Print to see it, and later assign the variable to the form's .Filter property.
Dim strFilter As String
With Me.Sub_Desi_Schedule.Form
strFilter = "[Task_End] BETWEEN #" & DateStart & "# AND #" & DateEnd & "#"
Debug.Print strFilter ' <- view this in Immediate window; Ctrl+g will take you there
.Filter = strFilter
.FilterOn = True
End With
You can avoid problems due to date format by using the unambiguous yyyy-m-d format for those Date/Time values.
strFilter = "[Task_End] BETWEEN " & Format(DateStart, "\#yyyy-m-d\#") & " AND " & Format(DateEnd, "\#yyyy-m-d\#")
I added the # delimiters within the Format expressions. But you don't need to do it that way; I think this should work as well ...
strFilter = "[Task_End] BETWEEN #" & Format(DateStart, "yyyy-m-d") & "# AND #" & Format(DateEnd, "yyyy-m-d") & "#"
Hi there I've created a form on access which looks like this:
I can make it so, that all the filters work, except for the Year and Length filters.
The Year boxes are unbound, and the left is called Year1 and the right one is Year2. I've tried to use Me.Filter code, but it doesn't work. It doesn't come up with an error, it just won't filter my data. So what I need is to make it so that the user can enter a year in Year1 and in Year2, and it filters the data between those two years. So for example they put 2000 in Year1 and 2010 in Year2, and then when they run the query it only shows data from 2000 to 2010.
This is the code I'm currently using:
Private Sub Year2_AfterUpdate()
Me.Filter = "[Year] BETWEEN #" & Me.Year1 & "# AND #" & Me.Year2 & "#"
Me.Filteron = true
Any help would be much appreciated! :)
(If you could explain what code does what that would be much appreciated too, so that I can learn to write it myself, and so that I can understand better! Thanks!)
You only use the "#" characters when you are using a date literal in your filter. If it is just a numeric you would use:
Me.Filter = "[Year] BETWEEN " & Me.Year1 & " AND " & Me.Year2
If [Year] actually IS a date then you should use:
Me.Filter = "[Year] BETWEEN #01/01/" & Me.Year1 & "# AND #12/31/" & Me.Year2 & "#"
Which will evaluate to [Year] BETWEEN #01/01/2010# AND #12/31/2011# if you enter 2010 and 2011 in the year textboxes.
You must use the name of the query (frmAufträgeQuery) and NOT the Form (frmAufträge):
On Error Resume Next
FromDate = "01.01.2021"
ToDate = "01.04.2021"
Me.Filter = "([frmAufträgeQuery].[Administrativ abgeschlossen] Between #" & Format(FromDate, "yyyy-mm-dd") & "# AND #" & Format(ToDate, "yyyy-mm-dd") & "#)"
Me.FilterOn = True
I need to print the date in the format of mm/dd/yyyy.
if the date is 4/24/2009 it should print the date as 04/24/2009.
that is zero padding is also needed..
I used date function to get the current date...but the date is getting in the format of m/dd/yyyy...
Tested in the immediate window and is working for me (output as a comment)
Format(Now, "MM/dd/yyyy") '04/29/2009
Format(Date, "MM/dd/yyyy") '04/29/2009
Format(CStr(Now), "MM/dd/yyyy") '04/29/2009
Format(Date$, "MM/dd/yyyy") '04/29/2009
Format(CDate(Date), "MM/dd/yyyy")'04/29/2009
So whether it is string or datetime should not matter.
Edit: Saw your comment to Fredrik. It doesn't matter how it looks like when you save it to the db table (column date format would be a property of the db and not your program's (or vb's) responsibility). Just format the value as and when you retrieve it from the db.
Note that the "/" character in date formatting functions has a special meaning, as "date separator". This means that i may be replaced with the date separator for the current locale that the program is executed in (here in Sweden it would be replaced with "-" for instance). In order to ensure that you do indeed get the "/" character in the output, I think this would work (I don't have a VB installation to verify with):
Format(date, "MM'/'dd'/'yyyy")
just for the record, escaping the slash will work
Format(dt,"MM\/dd\/yyyy")
Try the next code:
Format(dt,"MM/dd/yyyy")
When you enter date in whatever format it will convert default value so do one thing that in access change your data type date/time to text then it can't affect to your work sure.
I also use VB6 and need to format date in my txt report
this works for me
Format$(Now, "yyyy-mm-dd-00.00.00")
but only if I declare date as string
You can try like this also depending upon your requirement
Dim strDate As String
Dim strDate1() As String
strDate = FormatDateTime(Now, vbGeneralDate)
If InStr(strDate, " ") > 0 Then
strDate1 = Split(strDate, " ")
Dim datDate1 As Date
If Month(strDate1(0)) < 10 Then
txtDate.Text = "0" + strDate1(0)
Else
txtDate.Text = strDate1(0)
End If
Else
End If
Formatting DateTime as a string is straightforward. Often we use format patterns like "HH." But methods like ToShortDateString are also useful.
Example. First we get the current time through DateTime.Now. When you execute these code examples, the current DateTime will be different on your computer.
Here: A format string beginning with MMM (for the month) is used. Look at how the pattern matches up to the output of the program.
VB.NET program that uses format string with DateTime
Module Module1
Sub Main()
' Use current time.
' ... Use a format.
' ... Write to console.
Dim time As DateTime = DateTime.Now
Dim format As String = "MMM ddd d HH:mm yyyy"
Console.WriteLine(time.ToString(format))
End Sub
End Module
Output
Feb Tue 21 13:26 2017
strDate = Format(strDate, "yyyy-mm-dd")
BillTime = Format(BillTime, "hh:mm:ss")