This question was asked before and i tried to implement that solution but I can't get it to work
THis is basically what I am trying to achive
To subtract datetime values to get the number of total hours EVEN if it is beyond a day then the value I would like to display is 36:03, this is in the format [h]:mm, tks, below is the VBA code I am trying to implement
Sub dateissue()
Dim startdate As Date, enddate As Date
Dim doublevar As Double
enddate = "13/2/2014 08:10:00"
startdate = "12/2/2014 18:10:00"
doublevar = enddate - startdate
Range("D3").Value = Format(doublevar, "[h]:mm")
End Sub
My result in D3 is always :12
any reason why
This did the trick for me:
Range("D3").Value = doublevar
Range("D3").NumberFormat = "[h]:mm"
Also make sure that you input dates in correct format (i.e. month/day order, system regional settings may affect this on different machines).
Or if you want ONLY text:
Range("D3").Value = "'" & Application.WorksheetFunction.Text(doublevar, "[h]:mm")
Related
So I know this question has been asked a couple of times over, but I believe my situation is a bit different (happy to be proven wrong of course!)
Here is the data flow: a user types a date in a date in a form. They then click a button. My macro then takes that date, runs it through the following function:
Function AddWeekDays(StartDate As Long, Days As Long) As Date
Dim i As Long
Dim d As Date
d = StartDate
i = 0
While i < Days
d = DateSerial(Year(d), Month(d), Day(d) + 1)
If Weekday(d, vbMonday) < 6 Then
i = i + 1
End If
Wend
AddWeekDays = d
End Function
Then it formats the date to change it from mm/dd/yyyy to dd/mm/yyyy in the following way:
Dim deadline As Date
Dim deadline_formatted As Date
Dim DateReceived As String
Dim DateConverted As Date
DateReceived = txt_DateReceived.Text
DateConverted = Format(DateReceived, "dd/mm/yyyy")
deadline = AddWeekDays(DateValue((CStr(DateConverted))), 9)
deadline_formatted = Format(deadline, "dd/mm/yyyy")
However, the deadline_formatted value is still coming out in the mm/dd/yyyy format.
As an example, when a user enters 01/05/2017 the program should return deadline_formatted = 12/05/2017, but it returns deadline_formatted = 05/12/2017
I have tried changing the variable type to string to see if that made a difference (it didn't), and have tried directly converting the deadline variable to the required format by using the following code:
deadline = Format(AddWeekDays(DateValue((CStr(DateConverted))), 9),"dd/mm/yyyy")
which still returns the incorrect format.
Can anybody out there suggest either:
How to fix the formatting issue to get the deadline_formatted into the format dd/mm/yyyy
OR
suggest a "workaround" to flip the "dd" with the "mm" (not ideal obviously, but if it works, it works!)
Thanks for any advice!
The best way to solve this issue is to actually change your computer's default date/time format to match the method used by the users. (In comments it is stated that the users are Australians but your company is US-based, so the default is probably currently set to be the USA's "mm/dd/yyyy" format.)
By ensuring that the computers date/time format is correct, it will allow you to process a date as a Date, and it can be stored in Excel cells as a Date, which then allows any of the Australian users to see it displayed as "dd/mm/yyyy" format while a USA-based colleague would see it displayed as "mm/dd/yyyy".
There is a financial risk to your company caused by forcing users to interact with software using an unfamiliar date system, as accidental entering of dates in the wrong formats can lead to major errors downstream, so it is in the company's best interest to allow you to change the settings to be relevant to the users.
It is not directly related to your problem, however I believe it might fix your issues. The manual calculation of adding week days might be the problem here.
There is a built in function to add workdays. You can include/exclude weekends/holidays. The following code replaces your above mentioned code.
Sub AddWeekDays()
Dim deadline As Date, deadline_formatted As Date
deadline = txt_DateReceived.Value
deadline_formatted = Format(Application.WorksheetFunction.WorkDay(deadline, 9), "dd/mm/yyyy")
'debug.print deadline_formatted
End Sub
the result to be String.
Dim deadline As Date
Dim deadline_formatted As String '<~~ change string
Dim DateReceived As String
Dim DateConverted As Date
txt_DateReceived = "01/05/2017"
DateReceived = txt_DateReceived
DateConverted = Format(DateReceived, "dd/mm/yyyy")
'deadline = AddWeekDays(DateValue((CStr(DateConverted))), 9)
deadline = AddWeekDays(CLng(DateConverted), 9) '<~~ change Long
deadline_formatted = Format(deadline, "dd/mm/yyyy")
I wouldn't bother about the regional settings. Instead, make sure that all dates are captured as Date() or Now() values (42123 or 42123.5555). On the output side such values can be presented in any format you wish.
To ensure that dates are entered correctly my preferred way is to use a date picker. If that can't be done make no rules for entering the date at all, working on the presumption that each user will know how to enter a date on his/her machine. Add a date check, like ISDATE(), which will catch some input errors but not all. You don't need to catch all. You only need to teach users how to input dates on their respective PCs.
With this line you don't need anything else.
Range("E:E").TextToColumns FieldInfo:=Array(0, xlDMYFormat)
'1.2.2019 -> 01/02/2019
'2,3,2019 -> 02/03/2019
'3-4-2019 -> 03/04/2019
'4*5*2019 -> 04/05/2019
'5_-6-*2019 -> 05/06/2019
'and so on
Of course you can change the format with
xlMDYFormat
The code below is the code I'm using to set the selection formula for my Crystal Report.
Dim dateTo As Date
dateTo = dtpCRTo.Value.ToShortDateString
dateTo = Format(dateTo, "dd/MM/yyyy")
If cmbCRSupplier.Value = "" Then
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= #" & dateTo & "#"
The issue I'm getting, is the data that I'm testing with has a Sales_Headers.Stage value of 6, which is fine, and a Sales_Lines.PaymentDate value of 30/11/2016 (In UK, dd/MM/yyyy) format, however, even though the DateTimePicker I'm using is showing 11th December 2016, the value of dateTo is returning as 12/11/2016, which is MM/dd/yyyy format, so the report shows no data, as that date is before the payment date.
Why is the date returning in this format, even though I'm formatting it?
EDIT
Also, before I put the formatting section in, and had dtpCRTo.Value.ToShortDateString in the selection formula, it still did the same thing.
For any dates where the day part is less than 13, it treats that part as a month, but works fine once the day part is 13 or more...
EDIT 2:
The query I have now is as follows:
Dim dateTo As Date = dtpCRTo.Value.AddDays(1).Date
dateTo = Format(dateTo, "dd/MM/yyyy")
If cmbCRSupplier.Value = "" Then
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} < #" & dateTo & "#"
This works for all dates, except when choosing the last day of each month. If I'm selecting 31st January, the dateTo value will become 02/01/2017 (2nd February), which is treated in the formula as 2nd January. Is there a way around this?
Format Properties:
1. DatePicker > Properties > CustomFormat > (Enter format you want here)
2. Properties > Format > Custom
Code Behind:
Dim dateFormat As String
dt.Format = DateTimePickerFormat.Custom
dt.CustomFormat = "dd/MM/yyyy"
dateFormat = dt.Text
Its either you want to format in Properties or in Code Behind.
This question is related to this one, however I thought I'd create a new post since it's not the exact same issue, and the ideas on it were just being repeated.
I have a selection formula for my Crystal Report. It's supposed to select data where the Stage field in one table is 6, and the PaymentDate field in another is less than, or equal to, the value of the DateTimePicker control.
The code that I have below is working fine for most of the dates. However, say for example I have the following data in the database:
Sales_Headers.Stage = 6
Sales_Lines.PaymentDate = 28/01/2017 (January 28th, 2017)
When choosing a date of January 26th, up to January 31st, the data is only retrieved when the date is 28th or higher. However, if I then select a date of 1st February (Or the 1st of any month to be precise), it is returning the date as 02/01/2017, or, 2nd January 2017, so the data isn't shown.
Why is it changing for only the 1st of each month? All other dates are being read correctly, as dd/MM/yyyy, but on the first, it's using the MM portion as the dd portion.
I've tried:
Dim dateTo As Date = dtpCRTo.Value.AddDays(1).Date
dateTo = Format(dateTo, "dd/MM/yyyy")
If cmbCRSupplier.Value = "" Then
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} < #" & dateTo & "#"
Then I tried this in the form_Load event, as well as in the Value_Changed event of the DateTimePicker:
Dim dateFormat As String
dt.Format = DateTimePickerFormat.Custom
dt.CustomFormat = "dd/MM/yyyy"
dateFormat = dt.Text
The final thing I tried was just to have no formatting code, and just using:
If cmbCRSupplier.Value = "" Then
selectionformula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= #" & dtpCRTo.Value.Date & "#"
But it was the same result for all of them.
As well as the method #Siva was talking about (Always a good way, that way you can create the formula there and use the syntax that it's looking for), there is the way you're trying, to do it in VB.NET.
As I just mentioned, the syntax is important, and this is what is causing your issue.
I've not seen anyone try to use DateTimes in this method before, using `#value#.
The correct way to format dates into a RecordSelectionFormula (again, this is something you'll have seen if you'd have created it in Crystal itself), is to DATE(yyyy, MM, dd).
So, the correct way to syntax this is to use:
selectionFormula = "{Sales_Headers.Stage} = '6' AND {Sales_Lines.PaymentDate} <= DATE(" & _
dtpCRTo.Value.Date.Year & "," & dtpCRTo.Value.Date.Month & "," & dtpCRTo.Value.Date.Day & ")"
Use this method to insert your dates into selection formulas in the future, that way you can't get it wrong.
If this doesn't work, then you need to check your Region and Local Date/Time settings in Control Panel, to ensure they're set correctly.
I want to filter SQL-table between start date and end date, I used before string variable then I use string.format to make the format mm/dd/yyyy, I tried now in VB.net 2015 the following code:
Dim S as String
s=inputbox("Enter Start date")
S=string.format(S,"mm/dd/yyyy")
But it doesn't work, can somebody give me a solution?
You could try this for handling the input value, assuming you only need the date value as a formatted string, since your question is about formatting a date:
Dim S As String
S = InputBox("Enter Start date")
If IsDate(S) = True Then
Dim d As Date = Date.Parse(S)
S = d.ToString("mm/dd/yyyy")
Else
'Handle the non date input here
End If
But I think you should consider #Plutonix comment, since we don't know exactly how you are sending the date to perform the filtering, or how your table fields are defined.
Regards!
How can I compare the last characters using of month, day, and year or the completedate rather than using datetime, Example case is Textbox3 is greater than textbox9 because Textbox3 day = 26 and textbox9 day = 25.
Mycode:
'in my case I have 2 Textbox.
'Date format: hh.mm MM/DD/YYYY
'Textbox3= 02.02 03/26/2014
'TextBox9= 21.01 03/25/2014
If Val(Strings.Left(TextBox9.Text.Trim, 5)) < Val(Strings.Left(Textbox3 .Text.Trim, 5))Then
TimeError.ShowDialog()
End If
Really, the fastest, most reliable, and most effective way to do this is to parse the values into a DateTime. And taking a step back from there, the fastest, most effective way to get a date time from a textbox is to use a DateTimePicker control.
But if that's not an option, we can build on the code I gave you last time:
Dim temp1() As String = Textbox3.Text.Trim().Split(" .".ToCharArray())
Dim temp2() As String = Textbox9.Text.Trim().Split(" .".ToCharArray())
If DateTime.Parse(temp2(2)) < DateTime.Parse(temp1(2)) Then
TimeError.ShowDialog()
End If
I'll add that you probably want to also have code to compare the time values in the case where the date portions are equal. Given this as a starting point, you should be able to write that code on your own.
in vb.net you can comopare dates like this:
dim date1 as date = cdate(Textbox1.text)
dim date2 as date = Date.now()
if date1.date=date2.date then ....
and months like this
if date1.month=date2.month then ...