I am making a macro where I search through a spreadsheet of dates where each component of a date is already broken into separate components ie, year, month, day, hour, minute, second.
I want to compare these dates and find the earliest and latest date.
I was trying to concatenate the date components from strings into a Date variable, store the Dates in a Collection, and then search the Collection for the earliest and latest dates.
Dim dt As Date
Dim dtstring As String
Dim dates As New Collection
... [code where I just set unrelated variable values from cells and started a loop]
dtstring = "#" & day & "/" & month & "/" & year & " " & hour & ":" & minute & ":" & second & "#"
dt = dtstring
dates.Add dt 'add a Date into the dates collection
It doesn't like setting my Date variable value, dt, from a string but every example I find of this hard codes the Date variable with something similar to
Dim dteAppointment As Date = #8/2/2007 14:02:00#
whereas I need it set by the six date components.
Any help on this is much appreciated.
Under presumption you still have the original dates available, simply use:
Application.WorksheetFunction.Max(<Range>)
and
Application.WorksheetFunction.Min(<Range>)
respectively.
This will already return the result for you, without needing to do any crazy string formatting and what not.
EDIT: Probably worth noting, the returned value is Double type. It's not that big of a deal, given the function automatically serializes the input dates, but if you're returning the result to a variable, make sure to return it to a Dim resAs Double and not As Date
Afterwards, if you wish to return it back to a specific cell or somewhere as a Date, simply use the inbuilt DateSerial() in-built method! ;)
Related
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'm using Excel VBA and want to verify 2 dates are the same. On my macro, the user enters a date in the mm/dd/yy format (01/31/15). When the macro runs, it opens a file submitted by a user in Europe. On that sheet, there is a date field where the date is entered in the dd/mm/yy format (31/01/15). I'm wondering if there is an easy way to compare these 2 dates to verify that they are the same.
I could convert one of the dates into the other format and then convert both to a date serical to see if they are the same. I wanted to check and see if there was an easier way or a function that could do that.
Thanks for the help........
So apparently the MSDN Library has a converter Sub:
Function MakeUSDate(DateIn As Variant) As String
' Do nothing if the value is not a date.
If Not IsDate(DateIn) Then Exit Function
' Convert the date to a U.S. Date format.
MakeUSDate = "#" & Month(DateIn) & "/" & Day(DateIn) & "/" & Year(DateIn) & "#"
End Function
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")
I have a userform with textbox. When textbox is initialized it's getting filled with actual date. What I want to do is to fill it with custom date format = DD-MM-YYYY
I wrote code below and something is wrong about it but I have no idea what is wrong. Code has msgbox before inserting value, MsgBox shows date in a custom format but when it is passed to textbox.value it's like M/DD/YYY.
Dim year As Long, year_control As Date
year = Format(Date, "yyyy")
year_control = Format(Date, "dd-mm-yyyy")
MsgBox (year_control)
textbox.Value = year_control
(...)
If year_control < "01-04-" & year Then
Me.Controls("rok1").Value = True
Else
Me.Controls("rok2").Value = True
End If
You cannot "Format" a date variable:
year_control As Date
year_control = Format(Date, "dd-mm-yyyy")
The above code does nothing because a Date variable is simply holing a date more specifically VBA stores Date variables as IEEE 64-bit (8-byte) floating-point numbers that represent dates ranging from 1 January 100 to 31 December 9999 and times from 0:00:00 to 23:59:59.
No matter what you do to this variable it will always display dates according to the short date format recognized by your computer. Times display according to the time format (either 12-hour or 24-hour) recognized by your computer.
So while you can change the internal value that is held by the Date Variable you cannot store its format inside of the same vairable.
You can however display it however you would like inside of a string variable. So, if you used:
Dim year As Long, year_control As Date
Dim strYear_control As string
year = Format(Date, "yyyy")
year_control = Format(Date, "dd-mm-yyyy")
strYear_control = Format(year_control , "dd-mm-yyyy")
MsgBox (strYear_control)
textbox.Value = strYear_control
It should work as you are expecting. As the Format() function will return a Variant (String) containing an expression formatted according to instructions contained in a format expression.
As a side note you may also wish to use
Format$(year_control , "dd-mm-yyyy")
as it will be much faster, You also can use FormatDateTime to format your date in other various ways.
The format function in vba is changing the date. e.g for format("3/12/2009","DD/MM/YYYY"), the function returns 12/03/2009 where "3/12/2009" is what excel vba reads from a cell that has the value 12-Mar-2009 and the format as "dd-mmm-yyyy"
No it's not.
If a date-as-string is passed to the Format function, it will parse it using current regional settings. Your settings are obviously MM/DD/YYYY which is default for USA. Nothing prevents Excel from displaying a date as DD/MM/YYYY if set manually, but by default it would display MM/DD/YYYY.
To do: Stop reading dates as strings. Read them as dates.
dim d as date
d = activecell.value
Had few times problem myself where VBA in Access reades most dates as europian but some as USA:
This DOES NOT work properly:
myRs.FindFirst ("Date =#" & myDate & "#")
This works:
myRs.FindFirst ("Date =#" & Format(myDate, "Long Date") & "#")
The long date (eg 01 January 2012) clearly makes the difference between month and day