Convert the value to specific date format in Excel - excel

I have the following date in B cell. Actually this is not in the date format.
2015-11-01-01.13.34.737000
that has to be changed into the following format:
11/01/2015 01:13:34 AM
I'm not so familiar in excel formulas. Can anyone help me to bring the expected output? Actually, I tried changing the date format using format cells but no luck and also tried some excel formulas.
-Thanks in advance

If your original value is in cell A1, then as written, this works:
= MID(A1, 6, 2 ) & "/" & MID(A1, 9, 2) & "/" & LEFT(A1, 4) & " " & SUBSTITUTE(MID(A1, 12, 8), ".", ":") & " AM"
If you need the formula to 12-hour time and append AM or PM, then obviously you need to extract that 2 digits of the input value and check whether it's >= 12, append the right string, and change the hour before you output it. By the time you've understood what the above is doing, you may begin to see how you could do that. However, since it's a bit more complex, here:
= MID(A1, 6, 2 ) & "/" & MID(A1, 9, 2) & "/" & LEFT(A1, 4) & " " & RIGHT("0" & MOD(MID(A1, 12, 2) - 1, 12) + 1, 2) & ":" & SUBSTITUTE(MID(A1, 15, 5), ".", ":") & IF(NUMBERVALUE( MID(A1, 12, 2) ) < 12, " AM", " PM")
The MOD is needed to wrap around after 12 hours, and the add/subtract 1 acrobatics are because we write hours from 1 to 12, not 0 to 11. The right("0" & foo, 2) bit simply ensures that 1-digit numbers are 0-padded to 2 digits. This seems to work, but I didn't test it totally exhaustively.

=--SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"-"," ",3),"-","/"),".",":"),":",".",3)
The above converts your original string into a string with this format:
2015/11/01 01:13:34.737000
The double unary at the beginning then converts that string to a real Excel date/time which can be formatted however you wish.

Related

Problem with using VBA to insert formula in Excel sheet

I recently ran into an issue when using VBA to insert a formula into an Excel sheet. Below the relevant piece of code;
Calculatie_cell.Offset(n, 5) = "=SOM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8) = "=SOM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"
For the first iteration where it triggers, the expected output is;
=SOM(H4:H7)
=SOM(K4:K7)
The output I recieve is;
=#SOM(H4:H7)
=#SOM(K4:K7)
This output yields a #NAME? error. Pressing F2, enter on the cell (like inputting it by hand), gives an error stating that this formula is not supported by older versions of Excel with the option to correct it to the expected output. If I accept this it works.
I can't find why Excel thinks it needs the # and the code below does not filter the # from the formule (but it does filter it out of random text with an #)
Calculatie_cell.Offset(n, 5) = Replace(Calculatie_cell.Offset(n, 5), "#", "")
Calculatie_cell.Offset(n, 8) = Replace(Calculatie_cell.Offset(n, 8), "#", "")
Does anyone have a suggestion how I can get rid of the #, or another way to make the formula work? Thanks in advance!
Relevant note; I use Excel 2016 in Dutch, meaning "SOM" is the correct way to spell "SUM"
First of all, regardless of your Office installation language, VBA asks for engligh input.
Therefore, replace SOM by SUM in your code.
It should appear as SOM in the workbook, but not in the code.
Second of all, using oCell = "=SOME_FORMULA" usually does not yield the intended result.
Instead, one should use oCell.Formula2 = "=SOME_FORMULA".
This is why an # shows up in the formula.
See this article for information.
Finally, your code should be
Calculatie_cell.Offset(n, 5).Formula2 = "=SUM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8).Formula2 = "=SUM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"
User #Solar Mike was right.
Even though Excel wants the code to be "SOM" (Dutch spelling), in VBA it needs to ben SUM (English spelling). It auto-translates and works right of the bat. Thank you for your help!
If you post it as an answer, I will accept it as such.

VBA: AddDate + time changes the format

Can someone advise, please?
I have used DateSerial, DateValue with mixed results, also Format(... doesn't work in this case.
When I use DateAdd("d", 1, DateValue(x.Value))
06/05/2021 20:20:20
is changed to
05/07/2021 20:20:20
All I need is this 06/05/2021 20:20:20 to be changed to this 07/05/2021 20:20:20
FYI if it helps the format of my cells is as follows:
Try this and swap dd/mm to mm/dd (the format will remain dd/mm/yyyy):
Format(DateAdd("d", 1, cel.Value), "mm/dd/yyyy") & " 18:59:59"
If you wish to use DateAdd, and your value is a true date value (not text), use:
DateAdd("d", 1, x.Value)
This will not alter a time part. If x is 2021-05-06 20:20:20, result will be:
`2021-05-07 20:20:20`
If x is 2021-05-06 18:59:59 you can add one hour:
DateAdd("h", 1, x.Value)
for the result: 2021-05-06 19:59:59
just x.value + 1 it'll get that it's a date and add one day

Convert 5 cells with 1s and 0s into a single array like {1,0,0,1,1} in Excel

Alright, in excel I'm converting a 5 bit binary code into a single array in the form of a string. Cells D62, D64, D68, D70, and D72 all have either a 1 or a 0, and I'm requesting help to convert these cells with numbers in them into an array by using a formula. I need the output to cell D59.
=IF(ARRAY(D62:D70)={1,1,1,0,0},1,0)
something like that
To change the 5 cells values to a 5 bit binary letter just concatenate:
=CHAR(BIN2DEC(A1&A2&A3&A4&A5)+64)
If one has CONCAT:
=CHAR(BIN2DEC(CONCAT(A1:A5))+64)
In D59 enter:
="{" & D62 & "," & D64 & "," & D68 & "," & D70 & "," & D72 & "}"

VBA 2010 - CDate Type Mismatch Issue

I have the following code that runs without issue on some laptops and then others it will error out with a Type Mismatch. In Column AA are Dates with format (mm/dd/yyyy) and Column AB has the respective Times with format (hh:mm:ss).
I am subtracting the date and time from Now(). I am unable to really troubleshoot the issue because it works fine on my laptop. For others, it errors out. CLng(CDate(Now())) has a value but the other two induce type mismatch. I've tried the two lines below and also another which concatenated the date and time, then performed CLng(CDate(.
'If CLng(CDate(Now())) - CLng(CDate(Range("AA" & i).Value)) + CLng(CDate(Range("AB" & i).Value)) >= 7 Then
If CLng(CDate(Now())) - CLng(CDate(FMT(Range("AA" & i), "mm/dd/yyyy"))) + CLng(CDate(FMT(Range("AB" & i), "hh:mm:ss"))) >= 7 Then
where FMT is a public function:
Public Function FMT$(ByVal Value, ByVal strFormat)
FMT = VBA.Format$(Value, strFormat)
End Function
I am requesting my colleague to send me a screenshot of his region date/time settings.. I was thinking this could be the case. I've read other questions similar to this but not able to figure it out. Thanks in advance.
If columns AA and AB contain text rather than dates and times, try using the following statement:
If Now() - (DateValue(Range("AA" & i)) + TimeValue(Range("AB" & i))) >= 7 Then
This will probably still be problematic if the users have a local date setting of, for instance, dd/mm/yyyy but your columns are storing a string representing a date in some other locale's date setting. If that is the case, you may need to parse the fields and do it as follows:
Dim myDateStr As String
Dim myTimeStr As String
Dim myDateTime As Date
myDateStr = Range("AA" & i)
myTimeStr = Range("AB" & i)
myDateTime = DateSerial(CInt(Mid(myDateStr, 7, 4)), CInt(Mid(myDateStr, 1, 2)), CInt(Mid(myDateStr, 4, 2))) + _
TimeSerial(CInt(Mid(myTimeStr, 1, 2)), CInt(Mid(myTimeStr, 4, 2)), CInt(Mid(myTimeStr, 7, 2)))
If Now() - myDateTime >= 7 Then
You can use the Value2 property of the Range object to get the numerical representation of a DateTime. Try:
If CDbl(Now()) - (Range("AA" & i).Value2 + Range("AB" & i).Value2) >= 7 Then
Just be sure to convert Now() to Double, otherwise it will use just the date part of the DateTime.
I find it easier to work with the numerical representation of the DateTime, as it allows you to bypass the regional settings.

How to convert date to week number

How can I convert 20110114 (YYYYMMDD) to week, eg WK02-11, in excel ?
Thanks.
First convert the number in a date. Supposing your number is in A1 cell
=DATE(LEFT(A1;4); MID(A1;5;2); RIGHT(A1;2)
Then use WEEKNUM
=WEEKNUM(DATE(LEFT(A1;4); MID(A1;5;2); RIGHT(A1;2), 2)
(gives 3)
Then, if you want, you could embellish the result:
="WEEK-" & WEEKNUM(DATE(LEFT(A1;2); MID(A1;5;2); RIGHT(A1;2), 2) & "-" & LEFT(A1;4)

Resources