Excel VBA Check Date, Set Date in long format - vba

I am new to VBA. I am trying to learn simple things and I have a specific problem I am trying to figure out. I get the gist of VBA, however actually trying to put the code into place is driving me crazy. Here is what I am trying to do.
I have a date value in cell C2 of 11/24/2015. I want to use VBA to do the following:
a. Check the active cell to see if the value in the cell is a date.
b. If it is not a date, display a message that reads "This is not a date".
c. If it is a date, format the date to display the complete date (day, month, and year) in the long date format.
This seems simple enough. I need to use an If Then Else statement and I have been trying different variations of VBA code to get this to work, with no luck. Can you please help me? Thank you in advance!!

To check if a value is a date, use the IsDate() function. To format a date a particular way, you can use the Format() function. You can find a reference on MSDN. In short, you want to use an expression along the lines of:
Format([date to format], "Long Date")
If you already found these functions and are running into other errors then you should post your code. We cannot debug code that we can't see.

Related

MS-Access conditional format date in tabular forms row by row

I have created a database to store genealogical records. I have created a tabular form to display records which include a date text box, however I am struggling to display the date text boxes with the formatting different on each row. The different formats I require are "dd/mm/yyyy" where a full date is known, "yyyy" where the year only is known, and the text box to be left blank if the date is unknown. The table the form is aligned to has a field to indicate whether the date is a full date, year only, or no date - field values "fd", "yo" and "nd" so easy to test the format required for each row.
I cannot see how this is possible using the Conditional Format option.
I am happy to use VBA but have struggled with this. I am new to Access and new to VBA but have a technical background (coding in C++ and Java) and VBA currently looks alien to me - but willing to get into it.
Any pointers please will be much appreciated.
Always store dates as DateTime as it can be stored as Null for unknown.
Then, have a Boolean field, YearOnly, to mark values where only the year is known; store the year with any date of the year, like 2020-01-01.
Now, use a query like this:
SELECT
ID,
[Date],
YearOnly,
Format([Date],"yyyy" & IIf(Not [YearOnly],"-mm-dd")) AS DisplayDate
FROM
YourTable;
[Solved] Thank you Kostas K and Gustav, I have implemented the query you suggest, which took me a while to figure out, but I have it now working in my tabular form. My problem was I was trying to achieve this using VBA on loading the form. VBA wise I am a complete novice.
Thanks Bob H

LibreOffice Calc (date has past coditional formatting)

I am building a to do list in LibreOffice. I have one column that describes the task and next to it is the deadline date. I would like any task that is past it's deadline to be a different color. Is it possible to do this using conditional formatting?
Just to clarify. I just managed to work something out by using "if cell value is less than TODAY()" however this only changes the look of the date cell. I also need to change the two cells in front of the date.
Thanks in advanceImage of to do list
Just figured it out. Just selected the entire list and used conditional formatting 'formula is $C2 < TODAY()'. This seems to work fine.

Macro or formula on Excel to Standardize dates

I really need help on this. I've been looking for an answer for a LONG time but couldn't find it.
In my work, I need to sort a table on Excel using the date present on every line. The problem is that the spreadsheet I use have different layouts of dates as seen below.
2/13/2017 4:43:02 AM (M/DD/YYYY)
02/11/2017 05:05 (DD/MM/YYYY)
I can't sort it this way because it always read wrongly. What I need to do is to split cells and then concatenate them but this is causing a lot of trouble.
Could you help me check if there's any way to do it automatically, using a macro, or at least using just a formula?
This is happening because the output date you got is in text format, not in date format. Here is the trick to resolve your case to get the output in mm/dd/yyyy hh:mm format. You can change the format to your desired one.
If text date is in A column, then formula is -
=DATE(MID(A3,SEARCH("/",$A3,SEARCH("/",$A3,1)+1)+1,4),LEFT(A3,SEARCH("/",A3,1)-1),MID($A3,SEARCH("/",$A3,1)+1,SEARCH("/",$A3,SEARCH("/",$A3,1)+1)-SEARCH("/",$A3,1)-1))+TIME(HOUR(RIGHT(A3,LEN(A3)-SEARCH(" ",A3,1))),MINUTE(RIGHT(A3,LEN(A3)-SEARCH(" ",A3,1))),SECOND(RIGHT(A3,LEN(A3)-SEARCH(" ",A3,1))))
Hope this helps. Rate if satisfied. :)

how to multi conditionally sort values using vba

So I'm pulling data from an external source which returns Date strings of the given format: "10/26/2013 9:46:46 AM"
When I sort the data it does not seem to be able to distinguish between AM and PM values so many noonish / 1 AM values are moved towards the bottom. Has anyone dealt with this before / have a solution to make it recognize the AM/PM aspect along with the day and time?
Thanks
It looks like it is sorting this as text. It may depend on how the data is getting pulled through.
If you select one of the cells and press F2 (to edit) then enter to go to the next cell, does this change your data?
In the code that is pulling the data through, you just need to amend it slightly.
Range("D5").value = string
It may change it slightly to maybe 24 hour time depending on your computer settings
I ended up just splitting the date and time using TimeValue() and DateValue() excel functions.I then wrote a macro to do a 3 key sort based on ticker, date, and then time.

Convert Excel Date to Sql Server Date Time format using ACE Provider VB.Net

I am attempting to import data in a batch from an Excel Worksheet to a Sql Server database. Everything works except for the one date field in the spreadsheet. The date returned is off by four years from the value in the spreadsheet.
Example: The Excel sheet has a date 10/24/2010 14:18, but when I look at the column in my query, the date is 10/23/2006 2:18. This pattern, 4 years and 1 day earlier, is repeated for every row in the worksheet.
The Excel column comes to me as a custom type, formatted m/d/yyyy h:mm. I receive this from an outside vendor and having them change the column is not going to be my simplest solution. I'm hoping that someone has worked with this and can point me in the right direction.
For what it's worth, the relevent part of the query is:
Select [Date Created] From MyWorksheet
TIA.
I would guess that the spreadsheet is generated from a Macintosh, or some other computer which uses the 1904 date system by default. See here for more info.
As to the solution, you could try switching the date system for the workbook before doing the import. I've never had to deal with this, so can't be sure it will work, but here's the VBA command to do that:
ActiveWorkbook.Date1904 = True
Or, as stated in the article linked above, you can just add 1,462 to each date.
EDIT: I realized that changing the date system, as I suggested above, will just move the problem up in your process, so that the error is apparent in Excel. I think adding 1,462 (or 4 years and 1 day) to each date is the only solution.
If it's that consistent, you can just do this:
Select DateAdd(dd,1, DateAdd(yy,4,[Date Created])) From MyWorksheet
Of course, you have to be sure this happens for every row in the column and it would be preferable to find the cause of the issue and fix that. I'm also not sure exactly at what level the dataadd function will run here. It sql server does it after pulling the data, you will be fine. But if sql server passes this to excel as an ole query then the dateadd function calls need to be tweaked a bit. HTH