In my data, the date-time format is 15-02-2019 19:56 in a single column.
I want to separate the date and time separate column.
Note that when using dates/date-times then what Excel shows in the cell is not what Excel actually saves as value in the cell. The value that Excel saves for that date is actually 43511,83056 which is the number of days since 1900-01-01. So time is a part of one day and is therefore represented by the part after the comma of that value.
So you can either use …
=INT(B1) to get only the date (and format the cell as date)
=B1-INT(B1) to get only the time (and format the cell as time)
Or just use the same value as in column B (=B1 in C and D) and just format one as date and one as time.
In VBA you can use the same technique:
Option Explicit
Public Sub SplitDateAndTime()
Dim MyDateTime As Date
MyDateTime = Range("B1").Value
'get date
Range("C1").Value = Int(MyDateTime)
Range("C1").NumberFormat = "YYYY-MM-DD"
'get time
Range("D1").Value = MyDateTime - Int(MyDateTime)
Range("D1").NumberFormat = "hh:mm:ss"
End Sub
Assuming your date and time in cell A1,
use the below formula in B1
=--TEXT(A1,"dd/MMM/yyyy")
and use the below formula in C1
=--TEXT(A1,"hh:mm:ss")
and select B1 and C1 and customize your date and time format.
Related
The VBA built-in Date variable can give me today's date (8/25/21 as the time this post is written). However, I really want the date in the mm/dd/yyyy format for future projects and store it in a variable - myDate. I just couldn't think of an easy string manipulation solution to get the desired result.
I've tried put the Date variable in excel sheets and then change the number format using the below code:
[A1]=Date
[A1].NumberFormat="mm/dd/yyyy"
myDate=[A1].value
debug.print myDate
Even though the number format code will change the appearance of cell [A1], making it look like it's in the desired format mm/dd/yyyy, the debug.print line still gives 8/25/21 instead of 08/25/2021
A date value holds no format. Apply the format for display - that includes when calling Debug.Print:
Dim myValue As Date
[A1] = Date
[A1].NumberFormat = "mm/dd/yyyy"
myDate = [A1].Value
Debug.Print Format(myDate, "mm/dd/yyyy")
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 was trying to import a CSV file to my phpmyadmin. Then my date field had values as 0000-00-00 since both date formats were different. I changed the CSV date-format to the format in the database. I have 5000+ data in my CSV. but only a few dates change to the format I chose. Remaining still in the old format
I went to Format cells, selected date, changed to Uk and selected the desired date format. But only a few dates change to the format I chose. Remaining still in the old format
Starting with data like:
Select the cells you wish to convert and run this:
Sub FixDate()
Dim s As String, d As String, rng As Range, cell As Range
Dim dt As Date
d = "-"
For Each cell In Selection
s = cell.Text
If InStr(s, d) > 0 Then
arr = Split(s, d)
dt = DateSerial(arr(2), arr(1), arr(0))
cell.Clear
cell.Value = dt
cell.NumberFormat = "dd-mm-yyy"
End If
Next cell
End Sub
to produce:
You can only change the date format in Excel once Excel recognizes the cell contents as dates. If this doesn't happen automatically, you can force it with the Text to columns function. After the dates have been recognized, you can use any date format you like.
Select the column with the unrecognised dates, and go to Data tab, click Text to columns.
Select Delimited, Next, Untick everything, Next, Select Date and from the dropdown select the order in which the dates currently are in your column. Hit Finish.
In my example dates are in yyyy-dd-mm format.
This is the fastest way I know of in Excel to make it recognize dates in any order.
I have a file with a few columns including one with dates and another with some integers.
I want to obtain the row number of a cell based on its date. So in column B, I want the row number of the cell containing the date 31st of December 2018. The display format in the workbook is "31-Dec-2018" and when clicking on the cell it shows 12/31/2018. I tried using
ActiveSheet.Range("B3:B500").Find(What:=CDate("Dec. 31, 2018")).Activate
and
ActiveSheet.Range("B3:B500").Find(What:=12/31/2016).Activate
But neither works as I get an error "Object Variable not set"). I have been googling a lot and couldn't find a suitable solution which activates the relevant cell. What's wrong with my approach?
This appears to work:
Sub dural2()
Dim lDate As Date, r As Range
lDate = DateSerial(2018, 12, 31)
Set r = ActiveSheet.Range("B3:B500").Find(What:=lDate)
r.Select
End Sub
Simply verify that the data in column B are real Excel dates rather than text values that resemble dates.Creating the range using FIND() will permit you to test it before you attempt to Select it and trap any errors yourself.
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