date conversion SQL-VBA - sql

I am getting data from SQL and pasting it to an excel sheet through VBA. The data comes in format
hh:mm:ss and I want excel to realize this is a time variable but that is not the case. Even if I select the pasted area and change format to any time format it doesn't seem to be working. It only works if I manually select a cell modify it slightly and click enter (or F2+enter for each cell) then it moves to a time format is there a way to fix this or is someone familiar with this issue?

How exactly are you getting this data into excel?
You would not happen to have Manual Calculation on would you? Excel will parse hh:mm:ss into a datetime, but it sounds like it is not recalculating after however you import this data.

Related

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. :)

converting date to different format

Ok this one has been asked a million times but I can't find any answer to work for me. I have a column named 'Invoice Date' that is data type 'date'. I have an excel pivot chart that uses my query to update itself every 60 mins. Everytime this updates I lose my cell formatting and what's weirder, is that when I format the cells in excel to ie: Mar-14 then nothing happens, until I double click in each cell, then it displays correctly. This is vital because I need to group by months, and this is the only way I have been able to do this.
I have another date field in the query that is 'smalldatetime' and when formatting the cells in excel I just select the column, and format and all cells change without me double clicking. So I'm thinking maybe if I can convert the data in my SQL query to display the date similar to 'smalldatetime' that my pivot will work properly.
I have tried the checking of boxes in excel that CLAIM to preserve cell formatting... this obviously doesn't work for me.
Any help is greatly appreciated
You could try the following SQL in your SELECT statement:
CONVERT(VARCHAR(11),YourDateColumn,106)
the end result will be "02 Apr 2014".
You should be able to do so for smalldatetime type of column as well.

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.

Specify date format of a large amount of input data

I have an input spreadsheet that needs to get sorted by date. The current format of the date is in the UK format (dd/mm/yyyy) but I need it in yyyy-mm-dd (actually I don't, I just need to sort it and that format is the most foolproof way of sorting). This all needs to be done in VBA as it's part of a bigger project that allows a bunch of data collation at once. The other problem is that the input sheet can be quite large (150,000+ rows). So, while I could parse through each row of data and change it around to the way I need, this would be horrifically slow and is NOT an option.
Currently I'm using this bit of code to format the date to yyyy-mm-dd:
inputGADRSheet.Columns(7).NumberFormat = "yyyy-mm-dd"
But, Excel outsmarts me and assumes that the date format of the column is originally in the US format (mm/dd/yyyy) which messes everything up and half of the values in the column don't meet that requirement (days above the 12th) so they don't get formatted at all. Is there any way to tell Excel what format the current data is in? That way it won't just assume that it's in the US date format...
Is the solution to change my Excel region to the UK. I assume this could be done using VBA, but it seems risky...
If your data is already in an Excel column, you can't reinterpret the values: Excel date values are (internally) number, 1 representing 1900-01-01. After the data has been (mis-)interpreted by Excel there's no way back.
The question is: Where do you get the input data sheet from? If the dates are entered correctly, reformatting is possible without any problem and does not affect sorting (which depends only on the numeric value of the date). If your data comes from a text file (probably .csv-kind), be sure to read ii as text and use Excel worksheet functions or VBA to interpret the values.

Why do my Excel spreadsheets format my numbers as dates?

We have a lot of automated spreadsheets that extract data, calculate forecasts, publish web pages, etc. It's a really messy system, and I'd love to redo it (with, say, a real web service), but we don't have permission to do that.
We run Excel 2007, with most of the spreadsheets converted to .xlsms.
Anyway, for some reason unknown to me, certain spreadsheets will format certain numeric cells as dates at seemingly random times. As you might imagine, the problem is difficult to track down, and usually I only find out when someone has written a nasty email about our data reading 3-Feb-1901 when it should read 400 (apparently Excel thinks there was a leap year in 1900).
I've explicitly set the cells in question to numeric format several times, only to find them filled with dates a week later.
I've scanned the macros (many are in the ancient Excel 4.0 macro language), they appear to be clean of any formatting changes. All copy/pastes are done as values and do not preserve source cell formatting.
There are no conditional formatting rules in the sheets in question.
Adding a little VBA to format the cells as desired in the Auto_Open subroutine appears to work around the issue.
Is this fix good enough, or in a week will I find the date format is appearing mid-calculation? If not, what is the root cause and how I can fix it?
One thing: I just read from this source that Excel '07 may spontaneously change Normal-styled cells to the format [$-409]m/d/yy h:mm AM/PM;#. Some of my cells that have changed were definitely styled Normal, but evidently not all.
I'm assuming the cells in question are the receipients of copied/pasted data, or external/queried data?
Whenever you add pasted data to a cell in excel, it runs througha routine to determine the best format for anything that's listed as "general" format, even if the source is "general" or "number". Sometimes even setting the destination cells to "number" format does not clear up this issue. But I've found the only way to to get this to work the way you need it to is to explicitly declare the format (in the macro) to the destination range in question before the paste/update/refresh operation.
I also think there's a way to disable date recognition within your VBA, but I can't remember the exact code. Try recording a macro while doing manually importing or performing your update and you should be able to use what comes up.
Edit: just did a test run, and after your connection string and destination add:
.WebDisableDateRecognition = True
That should definitely do the trick if you're pulling data in from an external source and not copy/pasting.
Just in case this helps, i had an issue where excel was auto formating my info pulled from a webpage into DATE format, to fix, in the import window, click options and UNCHECK the auto date formating option, after that, my data imported perfect!