I have a string like "Saturday, November 28, 2015 11:59:59 PM GMT-5" in an Excel spreadsheet and would like to remove the Day of the week and Time Zone before importing it to Access using VBA. I tried :
ExcelWorksheet.Cells(1 + counter, nIdCol) = left((ExcelWorksheet.Cells(1 + counter, nIdCol)), Find(" GMT", ExcelWorksheet.Cells(1 + counter, nIdCol) - 1)) "
but found that "Find" is not an Access function.
Any idea to do it?
If you want to do it in Excel, before importing to Access with VBA, you can use this formula to trim them. Note: I'm assuming that each of your strings will have "GMT" at the end to denote the time zone, and will have a , after the day of the week.
If Saturday, November 28, 2015 11:59:59 PM GMT-5 is in A1, use this formula in B1:
=TRIM(MID(A1,SEARCH(", ",A1)+1,SEARCH("GMT",A1)-SEARCH(", ",A1)-2))
Try using the Instr() function. Replace Find("GMT") with
Instr(string:=ExcelWorksheet.Cells(1 + counter, nIdCol).Value, substring:="GMT") - 1
More on the Instr() function: http://www.techonthenet.com/excel/formulas/instr.php
Related
I have dates that are formatting using the default time formats for Bash (echo $date) for example:
Fri Dec 24 07:35:41 EST 2021
I cannot change this behavior as I don't have permissions to alter our IT solution. I need to be able to load these dates into a UK localised Excel, but any attempts to use the usual Text To Columns DMY approach doesnt work due to the Day, time zone, time etc.
Is the only way to extract the data and re-assemble it or is there a simpler solution?
Try this function . write it to module and call from any cell . Like =makeDateFromStr(A1)
Function makeDateFromStr(ByVal someDate As String) As Date
On Error Resume Next
Dim var As Variant, Mu As Long
var = Split(someDate, " ") ' array to hold all parts of the string-date
Mu = (InStr("JanFebMarAprMayJunJulAugSepOctNovDec", var(1)) + 2) / 3 ' a way to make short string month (Dec) to number
makeDateFromStr = DateSerial(var(5), Mu, var(2)) & " " & TimeValue(var(3)) ' putting all together
On Error GoTo 0
End Function
As far as I can see, if we get rid of the EST and the week day FRI, we can get a valid date value. Note: If you want the weekday, you can use the function WEEKDAY() to get the value 6, which is Friday. (1 is Sunday).
So, to get rid of the weekday, you can use the RIGHT() function.
All weekdays are 3 letters and a space, so we need to get rid of the 4 first characters, like so:
=RIGHT(A1;LEN(A1)-4)
You can put this just underneath your example time, so place this in cell A2.
To remove the EST is a bit more tricky, we need to know where in the text EST is. For this, we use FIND():
=FIND("EST ";A2)
Now we know at which position the text "EST " appears in A2.
Then we can use a simple REPLACE() function, to replace "EST " with nothing. Like so:
=REPLACE(A2;FIND("EST ";A2);4;"")
Note: The 4 refers to the length of "EST ".
This worked for this example, so you can try it out with different data. Just comment below if it isn't working correctly, and with what data and I'll look into it.
Put a date in A1
Put =RIGHT(A1;LEN(A1)-4) in A2
Put =REPLACE(A2;FIND("EST ";A2);4;"") in A3
You could also merge it into 1 cell:
=REPLACE(RIGHT(A1;LEN(A1)-4);FIND("EST ";RIGHT(A1;LEN(A1)-4));4;"")
You could then place this next to the date in A1, so in B1, and drag this formula down for ALL your imported dates. Now it should work for all of your dates.
You could also copy their values and replace the original dates, then remove the formulas, and you will have the same layout and functionality you were expecting from the beginning!
So my problem is I have a Date that is defined as General in Excel as this "10 JUL 2021 10:30" I want to make it Excel Date.
kindly have a look at my picture for detailed understanding.
Need any kind of solution to automate this VBA or any formula,
Thanks in advance
If you need VBA to extract Date, please use the next function:
Function TextToDateX(txt As String) As Date
Dim arrD, arrM
arrM = Split("JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC", ",")
arrD = Split(txt, " ")
TextToDateX = DateSerial(CLng(arrD(2)), Application.match(arrD(1), arrM, 0), CLng(arrD(0))) + CVDate(arrD(3))
End Function
It can be tested as:
Sub testTextToDateX()
Dim txt As String, d As Date
txt = "10 JUL 2021 10:30"
d = TextToDateX(txt)
Debug.Print Format(d, "dd/mm/yyyy hh:mm")
End Sub
DateValue is localization related. Microsoft states: "DateValue recognizes the order for month, day, and year according to the Short Date format that you specified for your system". So, it may work or not.
In my case it doesn't...
I just tried using the DateValue() worksheet function, and every worked fine (normal Excel formula, no VBA needed):
=DATEVALUE("07 JUL 2021 10:30")
One thing that might be interesting for you: there exists a cell formatting (dd mmm yyyy hh:mm), which you seem to be using. (When you apply this on any cell, the value inside gets automatically formatted into that kind of date format.)
Edit, based on comment:
In case there's a comma in your date string, remove it first and then apply the DateValue() function:
=DATEVALUE(SUBSTITUTE("07 JUL 2021, 10:30",",",""))
I have a column that has a name date: Friday, December 25, 2015
and I want to convert it to a date format: 12/25/2015 , that way I can perform other date functions.
I have unsuccessfully tried both changing the format directly and using the =DATEVAL() function.
Any direction would be great. thanks
First remove the day names
Ctrl+f and just replace "Friday," or "Saturday," or other days with ""
now your columns should be formatted like
"December 25, 2015"
Now use the dateval function like
=DATEVALUE("December 25, 2015")
Now select date from formatting and you should be good.
With text in A1 use:
=DATEVALUE(MID(A1,FIND(", ",A1,1)+2,9999))
and format as a Date:
I am looking to convert a text time with a and p on the end into usable time in excel I have used the mid function and I does not seem to recognize the a/p and makes all the times am unless its 12--. Then it will return a PM time. I have used the Mid function and have searched the threads.
Times in the database look like
10/ 1/14 5:14P
These are text
I have used the mid function
=TIMEVALUE(MID(B6,9,6))
Returning
5:14:00 AM
when the time should be a Pm or 17:14
Help would be appreciated
Thanks
Try this :
=SUBSTITUTE(SUBSTITUTE(A1,"A", " AM"),"P", " PM")+0
where A1 has the value.
Format the cell to the kind of date / time / datetime format that you want ; formatting it to :
dd-mm-yyyy h:mm:ss AM/PM
will display :
10-01-2014 5:14:00 PM
I'm working with a date and time format from the Twitter API. It looks like this:
Tue Nov 26 20:44:15 +0000 2013
Is there a formula to convert this to a format that could be sorted chronologically? I don't need the +0000. Also not concerned about the day of week.
=DATEVALUE(MID(A1,9,3) & MID(A1,4,5) & RIGHT(A1,4)) + TIMEVALUE(MID(A1,11,9))
Then format as you like. As requested you would want a Custom Format of mmm dd HH:mm yyyy
Try this, assuming that your string is in A1:
=DATETIME(MID(A1, 5, 16) & MID(A1, 27, 4))
The two MID formulas cut out the parts of the string you want, namely the month, day, time, and year, but exclude the day of the week and the timezone. This produces a string that the DATETIME function can automatically recognize and covert into a native Excel date format.