I am trying to take a file with the date being mm/dd/yyyy and reformat it to yyyy-mm-dd and output that new format to a new sheet, but even after running my VBS macro it does not change although the birthDate is correct. I have seen that there is issues with Excel overwritting your format to US regardless. Is there any way around this? Thanks!
birthDate = Trim(CStr(Sheet1.Cells(currRawRow, "F")))
checkInput = InStr(1, birthDate, "/")
If (checkInput > 0) Then
birthYear = Year(birthDate)
birthMonth = Month(birthDate)
birthDay = Day(birthDate)
birthMonth = Format(CStr(birthMonth), "00")
birthDay = Format(CStr(birthDay), "00")
birthYear = Format(CStr(birthYear), "0000")
birthDate = (birthYear & "-" & birthMonth & "-" & birthDay)
Any number recognised as a date by Excel will display in the short date format of the control panel regional settings ->Date & Time for whomever opened the file.
To override this, either:
1) set the format explicitly as part of you loop as Daimian suggests or;
2) force the format to string, by preceding the date by a single quote.
e.g in your last line of code change:
birthDate = (birthYear & "-" & birthMonth & "-" & birthDay)
to
birthDate = "'" & (birthYear & "-" & birthMonth & "-" & birthDay)
Related
I currently have a log file that get generated when you click on a button.
At the beginning of the .log you have a preview with different information such as the login, url of a server etc... But you also have the date of the beginning of the process and the date for the end. Both are in the long date format and are displayed correctly in the log file.
That log file takes the information from a sheet the preview part is static and is a range ("A1:C13") and the rest of the log is beneath but still in the columns ("A:C").
The cells in excel :
I want to add a line that shows the difference between the two date, to quickly see the time the process took.
However when I'm creating the log file, I get the time difference in number.
eg : in excel the cell with the format shows 00:01:55 but in the log file I get 0,001331
The output in the .log :
So far I tried :
To force the format in vba when the export in processing
To copy/paste in value with the hour format to not show the subtraction in the cell
different kind of format but it wasn't conclusive
You'll find the code that create the log file here :
Private Sub iExportLog(Optional LogPath As String, Optional bouton As String)
Dim NF As Integer, C As Range, posi As Integer
Dim date_nom As String
Dim drct As String
NF = FreeFile
drct = ThisWorkbook.path & "\_LogELB"
Set C = iFLog.Range("A1")
' iFLog = the sheet
' Things I tried :
'iFLog.Range("C12") = Format(iFLog.Range("C11").Value - iFLog.Range("C2").Value, "hh:mm:ss")
'iFLog.Range("C12").NumberFormat = "hh:mm:ss"
'
posi = InStrRev(ThisWorkbook.Name, ".")
If Dir(drct, vbDirectory) = "" Then MkDir (ThisWorkbook.path & "\" & "_LogElb")
date_nom = Format(CStr(Now), "yyyy_mm_dd_hh_mm_ss_")
If LogPath = "" Then LogPath = drct & "\" & bouton & "_" & date_nom & ".log"
Open LogPath For Append As #NF
Do While C.Value <> ""
Print #NF, C.Value & vbTab & vbTab & vbTab & C.Offset(0, 1).Value & vbTab & C.Offset(0, 2).Value
Set C = C.Offset(1, 0)
Loop
Close #NF
End Sub
I don't have a lot of practice with manipulating dates but I know that it can be painful.
Is there a way to display a date subtraction correctly when generating a .log/.txt file ?
If you write a formatted time string to a cell then Excel will interpret that as a time value and "undo" your formatting, converting the value back to a numeric value (though the display format may hide that).
If you want to keep the "hh:mm:ss" format then first set the cell format to "Text", or prepend the string with ' before placing it in the cell. Or read the cell's Text property instead of Value
I have some issue with my VBA code. I got Date variable that stored as dd/mm/yyyy but when I write the date to a specific cell the format change to mm/dd/yyyy, I have been trying many options but none of them worked for me, I also check that the variable know what is the current day, month and year.
The code:
Sheets("report_orders").Select
Range("A1").Value = customerName
Range("A2").Value = "äæîðä " & orderID
Range("A3").Value = "äæîðä ì÷åç " & orderPo
'Range("B2").Value = orderDate
Range("B2").Value = Day(orderDate) & "/" & Month(orderDate) & "/" & Year(orderDate)
Debug.Print "year " & Year(orderDate)
Debug.Print "month " & Month(orderDate)
Debug.Print "day " & Day(orderDate)
but the result is :
When you put a date into a cell, you can format it using .NumberFormat. Ensure that you format the cell before entering the value. For example
Range("B2").NumberFormat = "mm/dd/yyyy"
Range("B2").Value = orderDate
You can read more about the .NumberFormat in Range.NumberFormat property (Excel)
I have around 30 columns and in the 27th column, I would like to use the below-mentioned formula:
"=SUMPRODUCT(--(RC[-22]:RC[-1]),--(MOD(COLUMN(RC[-22]:RC[-1]),2)=1))"
The above-mentioned formula is a recorded one, but I would like to use a dynamic formula as the columns get added and sometimes deleted. I tried different ways of editing the recorded formula. I'm unable to understand where I'm going wrong. The below-mentioned code is what I have tried:
Dim OLC As ListObject
Dim FirstName As String
Dim LastName As String
IndMetricsSht As Worksheet
Set OLC = IndMetricsSht.ListObjects(TableName)
Set IndMetricsSht = ActiveSheet
FirstName = IndMetricsSht.Cells(1, 5)
LastName = IndMetricsSht.Cells(1, IndMetricsShtHeaderCount - 3)
ActiveCell.FormulaR1C1 = "=SUMPRODUCT(--(" & IndMetricsSht.ListObjects(TableName) & [#[FirstName]:[LastName]] & "),--(MOD(COLUMN(" & IndMetricsSht.ListObjects(TableName) & [#[FirstName]:[LastName]] & "),2)=1))"
Please help me!
Thanks in advance.
To close this question:
The [#[ and ]:[ and ]] in [#[FirstName]:[LastName]] will need to be surrounded in quotes and concatenated in with &.
The final formula:
"=SUMPRODUCT(--(" & IndMetricsSht.ListObjects(TableName) & "[#[" & FirstName & "]:[" & LastName & "]]),--(MOD(COLUMN(" & IndMetricsSht.ListObjects(TableName) & "[#[" & FirstName & "]:[" & LastName & "]]" & "),2)=0))"
I can format a date in a specific date format, but the formatted string seems not possible to format anymore.
It wouldn't be a problem, because I could save the old format. But the thing is that I also have to read a string in the specific format from an excel cell and format it.
I use the german date system. (Mi = Wednesday)
UI_Main.txt_BeginnDatum.Value = Format(UI_Date_Picker.Date_Picker.Value, "ddd dd mmm yyyy")
That's how I get the date from the datepicker. It's now formatted in the specific date format I'm talking about (Mi. 09 Jan 2019).
The default format from the picker is dd.mm.yyyy.
strBeginDate_g = txt_BeginnDatum.Value
strTemp = strTemp & "Nr. " & Format(strBeginDate_g, "yyyy-mm-dd")
Here I try to write the date in another format, but the output is just the same as before.
Of course I could write my own function but I am sure format is supposed to handle this.
You could write a custom format, like so
Function FormatYYYYMMDD(strDateIn As String) As Date
Dim a() As String
Dim s As String
a = Split(strDateIn, Chr(32))
a(0) = vbNullString
FormatYYYYMMDD = CDate(Format(a(1) & "/" & a(2) & "/" & a(3), "yyyy-mm-dd"))
Erase a
End Function
The Format function can only format a numeric value. But you give it a string strBeginDate_g as parameter.
Instead give it the value as parameter:
strTemp = strTemp & "Nr. " & Format$(UI_Date_Picker.Date_Picker.Value, "yyyy-mm-dd")
Once you formatted a date with Format() it becomes a string, and you cannot calculate anymore with a string nor can you format it again.
I am using DateTime.Now in my Excel Macro to show the current timestamp.
It shows timestamp in "dd-MM-yyyy hh:mm:ss" format.
Instead, how can I get the timestamp in "yyyy-MM-dd hh:mm:ss" format?
Try with: format(now(), "yyyy-MM-dd hh:mm:ss")
DateTime.Now returns a value of data type Date. Date variables display dates according to the short date format and time format set on your computer.
They may be formatted as a string for display in any valid date format by the Format function as mentioned in aother answers
Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Format(Now(), "yyyy-MM-dd hh:mm:ss")
If some users of the code have different language settings format might not work. Thus I use the following code that gives the time stamp in format "yyymmdd hhMMss" regardless of language.
Function TimeStamp()
Dim iNow
Dim d(1 To 6)
Dim i As Integer
iNow = Now
d(1) = Year(iNow)
d(2) = Month(iNow)
d(3) = Day(iNow)
d(4) = Hour(iNow)
d(5) = Minute(iNow)
d(6) = Second(iNow)
For i = 1 To 6
If d(i) < 10 Then TimeStamp = TimeStamp & "0"
TimeStamp = TimeStamp & d(i)
If i = 3 Then TimeStamp = TimeStamp & " "
Next i
End Function
this worked best for me:
Cells(partcount + 5, "N").Value = Date + Time
Cells(partcount + 5, "N").NumberFormat = "mm/dd/yy hh:mm:ss AM/PM"
Copy and paste this format yyyy-mm-dd hh:MM:ss in format cells by clicking customs category under Type
Timestamp in saving workbook path, the ":" needs to be changed. I used ":" -> "." which implies that I need to add the extension back "xlsx".
wb(x).SaveAs ThisWorkbook.Path & "\" & unique(x) & " - " & Format(Now(), "mm-dd-yy, hh.mm.ss") & ".xlsx"
It can work as easy as this, choose the location you want, in this case I choose D3
Sheets("Put your Sheet's name here").Range("D3") = Now
Example, my sheet is called Sources
Sheets("Sources").Range("D3") = Now
Use the Format function.
Format(Date, "yyyy-mm-dd hh:MM:ss")