how to multi conditionally sort values using vba - 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.

Related

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

Have Excel Check Cell format at every Change

I have a VBA code that helps users schedule projects by entering either a date or days until completion for particular tasks and then creates a graph that shows the percent completion of the project.
The VBA code hinges on the formatting of the cell to perform its calculations. Excel does a great job of choosing a number or date correctly the first time a user inputs a value. However if a user was to input a date and then decide to input a number (ex: 7) they would end up with 1/7/1900 as the date, which then makes excel perform the VBA incorrectly.
Is there a way to get Excel to "reconsider" the formatting of the cell each time it is changed? If not is there a way to force it to do so in VBA and choose either number or date formatting only?
Thanks,
-MK
Yes,
as a user prefixing an "=" is probably your best bet.
Adding "e0" on the end will format as an ugly scientific number, but at least it's accurately represented in memory.
Another option is multiplying by 100 followed by a "%".
You've accurately noticed that dates to numbers don't change back as easily. On the other hand any entry with a front-slash '/' seems to format as a date pretty reliably. The single apostrophe ' formats as Text, which is not very good for automated solutions. A number by itself is too general to change the NumberFormat, so any "automatic" solution would be non-standard.

Same date format over several localizations

Currently im facing issue that troubles me a lot. I hope that somebody could help me out. I work for big company where are both Office 2007 (32bit) and Office 2010 (64 bit) used. Writing macros to be compatibile through whole company was hard task for me (I've never programmed in VBA before - actually this forum helped me a lot). My task is to maintain one big table in shared Excel sheet. There are several macros and several userforms. Now i will decsribe the problem briefly:
Sheet contains two columns with date format (start date and close date). Both values are imported to column form userform's textboxes (commandbutton lunches MsCal -exported to class- which fills those textboxes with date). What I simply need is to have date format as mm/dd/yyyy in both columns in order to perform filtering and other operations. When this values are updated by worker that uses different localization than English U.S. date is entered as dd.mm.yyyy. Thats make proper filtering based on date impossible. I tried to alter formating by:
UserForm1.TextBox10.Value = Format(Calendar1.Value, "mm/dd/yyyy")
but this piece of code misbehave somehow. On some machines it works, on some of them it is not working. And thats what is giving me headache. How should i proceed now? Is there a way to force excel to use same date format in sheet and ignore localization settings in Windows? Employees dont want to change localization to English U.S. because they are either used to their format, or need it for other applications. Is there a way to temporarily change localization only when this sheet opens?
Any advice will be apreciated.
Thanks in Advance
Peter
The best you can do is NEVER transform a date variable into Text.
Internally for excel a date is just a consecutive number (Left from the decimal separator are days and right from the decimal separator are hours). So, for example, the 10th of June 2012, for excel is 41188. This date value is independent of the date format set on your computer.
Now when it comes to represent dates (for humans to visualize) Excel will format this internal value into a String with the format set in your computer. So, for example if you have US date format in your computer, the date 41188 will be formated as 6/10/2012.
The big challenge with dates is to input the date in the correct format. When you input a Date as a string ("6/10/2012") then Excel will interpret it depending on the date format set on your computer. If you have US format, then it will thake the first cypher as month, the second as day and the last as the year. If you have a German format, it will read the first as day, the next as month and the last as year. So, the same input ("6/10/2012") for a US Format Excel will read 10th of june as for a German format Excel will read 6th of Oktober.
In your case, you should NOT format the date inside the Textbox10. For a US format Excel there is no problem, but if you have another date format, where the first cypher is the day instead of the month, you will get the wrong values:
Check this example. User inputs 10th of June in a German format Excel (dd.mm.yyyy)
Calendar1.Value retrieves a date value (41188)
Format(Calendar1.Value, "mm/dd/yyyy") transforms the date value into a string "06/10/2012"
When using the formated date (STRING), Excel will have to interpret what date it is. Because the computer date format is German, it will read Day:06, Month:10, Year:2012. You will be using day 41070 instead of 41188
If Calendar1.Value retrieves a Date variable and you give this date variable into a Date formated column, you will allways get the correct dale in your column and you will be able to filter and sort dates correctly regardless of the date format set inside the Column cells or the format set in the users computer.
Now, in your case, the best would be to assign directly the Calendar1.Value to the required cell. Something like:
ThisworkBook.WorkSheets("Sheet1").Range("C3").Value= Calendar1.Value
You can still asign Calendar1.Value into the TextBox10 for the user to see his selection, but disable the TextBox10 so that the only edit option is the calendar control. And when working with the date, istead of thaking it from the TextBox10, taking it directly from the Calendar1.Value .
If you still need to show the selected value from Calendar1 into a textBox then do NOT format the date in the Textbox. Instead, use:
UserForm1.TextBox10.Value = Cstr(Calendar1.Value)
This way, the user will see the date in the dateformat that he has set in his computer and to which he is used to.
One solution is to not use the date number format but rather only use the custom format for all your cell dates where you specify "mm/dd/yyyy" as the formatting string. However, in my experience, if your computer's regional settings are set to use "mm/dd/yyyy" then if you try make a custom cell formatting with this same string excel will keep as a dater linked to the computer setting so that doesn't help you. The way I worked around this was to change the date format on my computer, then format the cells as custom "mm/dd/yyyy" and save (and then turn your computer's settings back to how they were.) Now even though excel still claims they are date cells, you'll see that changing the settings on your computer doesn't change the value in the cell.
I guess another way is to always have a cell next to your date cell that calls the TEXT function. So if you have a date in A1 then in another cell =TEXT(A1, "mm/dd/yyyy") and only refer to this new cell. But that could make your spread sheet very messy.
I guess the best solution is to just get you IT dept to set every one in the company's date settings to use the same formats.

VBA Excel: Add one second to a time

I'm working with a spreadsheet with two columns, in which one of the columns is a time. I have loaded the data into a dictionary using the time as a key. I want to look up the data corresponding to a particular time (easy), and then get the next data point so that I can compare their values. The data is recorded such that one piece of data is generated every second over a period of around two hours.
My question is, does VBA in Excel have a function which can automatically add one second to a time? Or do I need to manually write one?
Update: In addition to the answers below, I have also found the "DateAdd" function useful - I suspect it depends on the version of Excel being used.
you should be able to use Time(hour, minute,second) function to add a second to the time.
so to add 1 second to a date with something like Date(2012,1,16) + Time(0,0,1) which should result in a value of "2012-01-16 12:00:01 AM"
you can use TimeValue("00:00:01") for adding in this example 1 second