I have the following in excel:
Saturday, June 11 2011
I would like to convert this into the standard dd/mm/yy format.
What would be the easiest way to do this (in either excel or VBA)?
In order to use Excel's CDate function, you first have to get rid of the leading text marking the day. Assuming this is always with a trailing comma and space, you could do the following:
Sub changedtformat()
Dim rawdate As String, cleandate As Date, resultdate As Date
rawdate = "Saturday, June 11 2011"
cleandate = Mid(rawdate, InStr(1, rawdate, ",") + 2, Len(rawdate))
resultdate = CDate(cleandate)
MsgBox resultdate
End Sub
With your text in A1, in B1 enter:
=DATEVALUE(SUBSTITUTE(MID(A1,FIND(" ",A1)+1,9999)," ",", ",2))
and format anyway you like. We just make a string that DATEVALUE() can handle.
Related
I'm writing a Macro where I need two things in one of the column ranges:
a) Cell values must be the date of the next day in the dd/mm/yyyy format;
b) Cell Format must be text
I keep running into the problem of the date format being changed to mm/dd/yyyy.
Default excel format for the vba function date is dd/mm/yyyy ,since my excel language is set to Portuguese.
By running:
Sub format()
Range("A1").Value = Date + 1
End Sub
I get the value 11/02/2023, date format, which is expected. But if I run:
Sub format2()
Range("A1").NumberFormat = "#"
Range("A1").Value = Date + 1
End Sub
The output is 02/11/2023 in text, in the mm/dd/yyyy format. I'd like to understand why this would even happen
Same thing happens for me. Not sure why but you can achieve what you want by doing:
Sub format2()
Range("A1").NumberFormat = "#"
Range("A1").Value = format(Date + 1, "dd\/mm\/yyyy")
End Sub
This will result in the cell being formatted as text in format "dd/mm/yyyy".
If you wanted the same format but actually formatted as date, you should do this:
Sub format2()
Range("A1").NumberFormat = "dd\/mm\/yyyy;#"
Range("A1").Value = Date + 1
End Sub
The backslashes are there because excel (at least on my system) considers the forward slash a substitute for default separator and replaces it with hyphens, which are my actual default separators.
I'd like to credit this answer for the last point (escaping the forward slashes): https://stackoverflow.com/a/8527762/9852011
I have a userform with few text boxes.
I would like to calculate a date based on the date put on text box1.
E.g. on text box 1 you enter today's date.
I want to calculate a date 5 years from today on textbox 2.
Private Sub TextBox1_AfterUpdate()
TextBox2.Value = Format(Val(TextBox1.Value) + 1825, "DD/MM/YYYY")
End Sub
Suggestions:
Use DateValue or CDate instead of Val.
Use DateAdd instead of the hard-coded 1825.
Format(DateAdd("yyyy", 5, CDate(TextBox1.Value)),"dd/mm/yyyy")
Private Sub TextBox1_AfterUpdate()
TextBox2.Value = Format(CDate(TextBox1.Value) + 1825, "DD/MM/YYYY")
End Sub
Use DatePicker you deminish chances of incorrect input and amount of needed error handlers and issues when user has US instead of UK format, or the other way around.
For using date use Dim myDate as Date: myDate = DateSerial(year, month, day) <- all as integers, whenever handled by code, this also would protect you from format differences.
Never set hard date format into inputs.
I have two dates in cell "A1" (Start Date) and "B1" (End Date). I change the date to get the real figures between the dates using SUMIFS formula.
There is Date Column in the Sheet with Time.
So what i want is that whenever the "A1" (Start Date) changes its time should always be initial like 00:00:00 and when i change the date for "B1" (End Date) it should always be like 23:59:59 is there any way to achieve this using VBA.
Your help will be appreciated.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
With Range("A1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
With Range("B1")
.Value = Now()
.NumberFormat = "mm/dd/yyyy h:mm:ss AM/PM"
End With
End If
Numeric time 23:59:59 is not equal to 0.9999999, it is 0.999988425925926
But use native date/time handling for this and avoid smart numbers:
Range("A1").Value = DateValue(Range("A1").Value)
Range("B1").Value = DateValue(Range("B1").Value) + TimeSerial(23, 59, 59)
I think this could work, if i understood your question correct.
It will make sure A1 is integer and make B1 integer then add 0.99999.
If Not Intersect(Target, Range("A1:B1")) Is Nothing Then
Range("A1").value = cint(range("A1").value)
Range("B1").value = cint(range("B1").value) + 0.99999999
End if
Just make sure the cells is set up to display as date and time.
If I understand you correctly, you don't need to bother with the time portion of the dates or go throught the mechanics of trying to change it.
You just need to properly construct your SUMIFS formula.
Range to sum: sumRange
Dates in your table: myDates
When you enter a date in A1 or B1, it will be entered as if it were at the beginning of the day.
If your dates in myDates are just dates, you can use the formula:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<=" & endDate)
However, if your dates in myDates also include a time, then you can modify the formula slightly:
=SUMIFS(sumRange,myDates,">=" & startDate,myDates,"<" & endDate+1)
And you can use the 2nd formula in either instance
Edit: I just noticed you posting your formula as a comment
I assume you have entered JUST the date (no time) in I1 and J1
Your formula as posted will work UNLESS C:C includes times with the dates. If it does include times with the dates, then try:
=SUMIFS(D:D,E:E,"<>Pending for Payment",B:B,H2,C:C,">="&$I$1,C:C,"<"&$J$1+1)
By adding 1 to the date, we move it to the start of the day after; so we change the <= to just < and encompass the full range of dates you desire.
I have some dates in a string in a column.
Because the format seems to be M/DD/YYYY I sometimes get a VALUE error when using DATEVALUE.
How can I convert a column to make sure all dates are correct.
For example, the following 2 cells
9/29/2006 12:49:58.956 AM DATEVALUE gives an error
9/12/2008 5:36:59.356 PM DATEVALUE converts to 39791
You need to select the range (just one colunm) with the dates... Go to:
(in Excel 2010)
Data >>> Text to Columns
Inside the dialog box select:
Delimited >>> Next
Just select Tab checkbox.
And heres is the magic!
You need to define the arrangement for your dates...
In the Date field:
Choose the what you need (as you say in your question is M/D/Y)
Destination field:
Make sure that is the same of the data you want to format.
And finish.
The problem you are having is probably that the data is getting interpreted as a general field, and shows up as, say 49:59.0 - which isn't text, which is what Datevalue() expects as input; for both inputs, Datevalue(CellID) words if you prepend the text with a ' - which ensures it is treated as text.
it seems that your system settings use dd/mm/yyyy for short date format. and datevalue function uses this settings, so its try to read the day from the first part of the string and the month from the second part. as in your example 9/29/2006 12:49:58.956 AM there is no month with 29 then it gives error, and in the second example it gives 39791 = 9 december 2008 and NOT 12 september 2008.
If you change the short date format in your system settings to mm/dd/yyyythen datevalue function will work correctly, but this not acceptable, so we need to replace month with day and day with month to get correct date.
I don't know if we can do this with Excel formula but we can do that simply with VBA.
So try this code:
Sub Test()
Dim d1 As Variant, d2 As Variant, td As Date
Dim Rng As Range, CL As Range
Set Rng = Range("A1:A2") ' change this to your range
For Each CL In Rng
d1 = Split(CL.Value, " ")
d2 = Split(d1(0), "/")
td = DateSerial(d2(2), d2(0), d2(1))
CL.Offset(0, 1) = td
CL.Offset(0, 1).NumberFormat = "mm/dd/yyyy" ' change the Date format as you need
'CL.Offset(0, 1).NumberFormat = "dd/mm/yyyy"
Next
End Sub
Using Excel 2003 VBA code, I copied a date from a form into a cell, which I then formatted as vbLongDate. This puts in a date like "Wednesday, August 13, 2014". I am struggling with getting the date components back out, though. I am trying this
Dim myDate As Date
myDate = Date(Range("A1").Value)
From there, I want to pull out the year, month and day values from myDate, but this isn't working. I tried a few variations, but they are all bombing out.
Thanks!
I assume you use the FormatDateTime() function with vbLongDate. That returns text. Consider formatting the cell with a date format instead and retain its date properties.
Sub test()
Dim myDate As Date
' this will return text
Range("A1") = FormatDateTime(Now, vbLongDate)
' A2 will remain a date
Range("A2") = Date
Range("A2").NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
myDate = Range("A2").Value
Debug.Print Month(myDate) ' no error
End Sub
Edit: Depending on the date format used, the appearance of the date will adjust to the regional settings.
' this date format will adjust to the regional settings
' my regional settings are DMY and I see "Thursday, 14 August 2014"
' even though the format shows the mmmm before the dd
myRange1.NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
' this date format will always be like displayed below
myRange2.NumberFormat = "dddd, d mmmm yyyy;#"
You could set different formats to different cells, depending on whether they need to adjust or not.