Excel VBA to web form date format - excel

Doing my first VBA to fill in a web form in IE. Having some issue with date from excel changing format when it is put in the web field.
Set doc = ie.document
doc.getElementById("txtTrackNotes").Value = ThisWorkbook.Sheets("Sheet1").Range("B2").Value
doc.getElementById("txtEntryTrackConnote").Value = ThisWorkbook.Sheets("Sheet1").Range("A2").Value
doc.getElementById("txtTrackDate").Value = ThisWorkbook.Sheets("Sheet1").Range("C2").Value
The last line there is the one causing problem. The Value in C2 is a date in the format dd/mm/yyyy
When it goes into the webform it comes up in the format dd MONTH (in text) yyyy 00:00 (for example 1 January 2018 00:00)
Source HTML is
<input name="txtTrackDate" class="Size9" id="txtTrackDate" style="width: 100px;" onclick="javascript:displayDatePicker('txtTrackDate');" onblur="javascript:CheckDate(this.value);" type="text" value="25/10/2018">
Any suggestions would be greatly appreciated

Try
=Format$(ThisWorkbook.Sheets("Sheet1").Range("C2").Value,"dd/mm/yyyy")
Is the field expecting this input format? If the above still changes then verify what the expected format is for the field.

Related

VBA Date + TimeValue returns no time

I have a date and time which I assemble into a date + time from strings in the form
date_string = "2020-12-30" 'yyyy-mm-dd
date_code = CDate(date_string)
time_string = "00:00:00" 'hh:mm:ss
time_code = TimeValue(time_string)
date_time = date_code + time_code
Commonly the return looks like 05.01.2019 11:00:00, which is what I expect.
The returned values also all check out as TRUE if I test with IsDate(date_time)
Whenever the time is 00:00:00 however, I only get the date returned with no time appended. I dont quite understand this, since TimeValue(time_string)returns 00:00:00.
So it must be an issue when combining date and time to a date + time string.
Can someone please enlighten me why midnight somehow does no exist in Excel VBA or where my error in creating the time code is?
EDIT:
I try to explain my situation a bit better:
I do this date date/time stuff in code and then but the result in an array in a loop. Only later on it is written to a cell in a table.
By the time is is written into a cell, even custom formatting the cell to "DD.MM.YYYY hh:mm" does not show the time as it is completely missing from the cell value.
Do I neet to apply a format at the point of date_code + time_code?
Sometimes the answer can be so simple. Thanks to Variatus and Paul I checked formatting out.
I applied a date_time = Format(date_code + time_code, "dd.mm.yyyy hh:mm") in my code. Using this, my code runs as expected and 00:00:00 appears as expected, even in the cell values of the Excel table.
When you enter an integer, like 43930, in a cell Excel will record the number as an integer, just as you entered it. You can then proceed to format the cell as #,##0.000 and thereby make the number display as 43930.000. Or you can format that very same number as custom dd mmm yyy hh:mm:ss and display it as 09 Apr 2020 00:00:00. The point is that Excel chose to record the number in its most efficient way, as an integer.
So, if you enter a DateValue + TimeValue which, together, amount to an integer Excel will record the integer correctly. The format in which that integer is displayed in your worksheet is a matter for cell formatting.

Excel::Writer::XLSX writing unformatted date

I have a script which is using the Spreadsheet::XLSX module to read in data which includes unformatted dates. When I open the spreadsheet on the desktop it does show the dates in the mm/dd/yyyy format. After I am finished reading them I write them out to a spreadsheet using the Excel::Writer::XLSX module. I am basically adding the next date in sequence. Everything works fine until I then read in from the spreadsheet that I created. It ONLY reads the date as formatted, no matter if I use either of these to read them:
$cell->{Val}
$cell->value()
This is the write format I'm using to write out the date. Just so I'm clear, I am writing out the date in the unformatted value using this format. If I don't use the num_format then the dates are in the ddddd format when I open the spreadsheet.
$workbook->add_format(bold => 1, align => 'center', num_format => 'mm/dd/yyyy');
How do I get it to be consistent when reading the spreadsheets and also viewing the dates as mm/dd/yyyy when I open it?
Quoting from CELL FORMATTING section:
Cell formatting is defined through a Format object. Format objects are created by calling the workbook add_format() method as follows:
my $format2 = $workbook->add_format( %props ); # Set at creation
The format object holds all the formatting properties that can be applied to a cell, a row or a column. The process of setting these properties is discussed in the next section.
Another quote from write_date_time() section:
A date should always have a $format, otherwise it will appear as a number, see "DATES AND TIME IN EXCEL" and "CELL FORMATTING".
According to this your code should be:
my $date_format = $workbook->add_format(
bold => 1,
align => 'center',
num_format => 'mm/dd/yyyy'
);
# apply format to a specific cell
# $string should be in ISO-8601 format as UTC, e.g. yyyy-mm-ddThh:mm:ss.sssZ
$worksheet->write_date_time($row, $column, $string, $date_format);
My weapon of choice for wrangling time stamps would be the Date::Manip distribution.

Excel wont recognize date using openpyxl

I have a piece of code that converts a unix date to standard U.S. date format. It displays properly in excel, however excel doesn't recognize it as a date.
if str(startdate).isdigit():
startdate = int(startdate)
date = datetime.datetime.fromtimestamp(int(startdate)).strftime('%m/%d/%Y')
ws4.cell(row=cell.row,column=2).value = date
ws4.cell(row=cell.row,column=2).number_format = 'MM DD YYYY'
Any idea how to get excel to see this as a date rather than text?
My mistake was assuming the line below created a date.
date = datetime.datetime.fromtimestamp(int(startdate)).strftime('%m/%d/%Y')
After venturing into the docs (Scary as hell for a noob. Does it get easier?) I realized .strftime('%m/%d/%Y') created a string not a date.
I converted that string to a date using:
date = datetime.datetime.strptime(date, '%m/%d/%Y').date()
Now excel recognizes it as a date.
Hopefully this helps someone in the future.

Date format in vba

I have VBA code which pull the details from .msg file (outlook files) and update in excel sheet. While reflecting the date column, it is showing as "3/9/2016 11:03:27 AM" but I want to show only date and not time.
I used the format option i.e.
Sheet2.Cells(Row + 1, 23) = VBA.Format(sentDate, "dd/MM/yyyy")
but it is showing the date a "8/3/2016 00:00". I want to reflecting only date and nothing else. Please guide me as to what all changes required to reflect only date.
Give this a try:
Sheet2.Cells(Row + 1, 23).Value = sentDate
Sheet2.Cells(Row + 1, 23).NumberFormat = "mm/dd/yyyy;#"
Yet, I have a feeling that the cell does not really contain a date but merely text. So, please also try to change the .NumberFormat to the following first:
Sheet2.Cells(Row + 1, 23).NumberFormat = "General"
If the dates are now showing all as numbers then these are actual dates and the first proposal should work. If not, then these are not dates yet and you'll have to convert the text (which looks like dates) to dates first.
For more information you might want to read this: Difference between date and time w/out work week Excel

VBA dates collected correctly but output wrongly

I grab dates from one spreadsheet and output them onto another spreadsheet. After grabbing the date, when I debug.print it is in the correct format. When I output the date, debug.print also displays the correct format. However, the format on the spreadsheet the value has just been sent to, doesnt show the correct format.
I am using:
Sheets(SheetName).Cells(RowNum, ColNum).Value = Data
Sheets(SheetName).Cells(RowNum, ColNum).NumberFormat = "dd/mm/yyyy"
after I have pasted the value, but the months and days are still switched the wrong way.
Is there something I am doing wrong?? If I right click the cell it thinks it's date is dd/mm/yyyy but instead of 4th Sept it is showing 9th April.
This might be some trouble with localization:
Try using NumberFormatLocal, if DanielCooks tip didn't help ;)
edit: erlier it was statet by mister Cook, to check if the given data is correct.
edit:
With my german version I have quite some trouble to use / as the seperator, that is why i tryied with this code .NumberFormat ="dd-mm-yyyy;#" - works fine; I can switch days and month as I like.
edit:
With .NumberFormatLocal = "TT/MM/JJJJ" I have to use the german shorts for day, month and year, but now I can use / as the seperator.
You should experiment a litte bit with some formats strings ;)
Sorry to resurrect an old post, however I had a problem with VBA outputting a valid date as US style with the actual date changed for example 1st May changed to 5th Jan. I came upon this post but I didn't find the answer I needed here and now that I have worked it out I thought I would share it:
The key is not to define the variable storing the date as a "date" but as a "double", e.g.
Dim ReportDate As Double
ReportDate = Date
Range("E6").Value = ReportDate
This works as it outputs the numeric "date value" which excel then formats locally e.g. 41644 gets formatted as "05/01/14" using UK format or "01/05/14" using US format.
Hope this proves useful to other people (probably me when I forget how I solved it in six months time).
In the end I had to format the cell as "TEXT" to keep the correct format
(1) You need to define the variable to "Date" type to read the input, then set the date format before assigning it to the date variable.
(2) You also need to format the date output to make it work !
'**Read the date input****
Dim date1 as Date
Range("B2").NumberFormatLocal = "dd/mm/yyyy"
date1 = Range("B2").Value
'**Output the date****
Range("E2").Value = date1
Range("E2").NumberFormatLocal = "dd/mm/yyyy"

Resources