excel format generated date (not hardcoded date) - excel

I'm using closedxml to generate a excel file.
I'm mapping the cells directly in excel by creating a named range and inside of it reaching the items in the range by {{item.property}}, the range collection is added like a variable in the code.
The item I'm using has a Year property and a month property, the mapping in that excel cell is: {{item.Yr}}/{{item.Mon}}.
I would like Excel to format the value in the cell to mmm-yyyy (Jan-2020). Which it does if I directly write a date in the cell in Excel and presses Enter, but that's not what I want, I want it to format my generated value.
I've tried using the functions like =TEXT(date,format) or =DATEVALUE("1/1/2015").
But the problem is that I can't write directly "1/1/2015", I need to write something like DATEVALUE(1/{{item.Mon}}/{{item.Yr}} but Excel syntax doesn't allow this. Ofcourse I've also tried just the "format cell" to date on the cell, but doesn't work (only if I directly type a date and presses Enter)
I'm sorry for my crappy explanation. I hope someone understands what I mean and what I want to accomplish.

SOLVED
After 2 days banging my head against the wall, I found that If a variable is used in a formula, it needs to be escaped with "&" at the beginning:
&=Date({{item.Yr}},{{item.Mon}},1).

Related

change cell format to general or text?

we have sort of data in excel like this ---> 34:5:20
excel likes to consider it as time or date.
here is the problem because I know it's not a date and what ever action I want to do with it again
excel thinks it's a date/time value .
how can I stop this behavior ?
(each of the ':' delimited values has a special meaning to me but not date/time)
thanks a lot.
There is one option though, you can use the formula below, to convert the dates back to your format
=CONCAT(" ",TEXT(A2,"[h]:mm:ss"))
I am adding a space using concat function just so that if you ever decide to convert all to value, it will not return back to dates
I found the solution finally .
copy column to notepad.
select another column
change it's format cell to text.
now paste from notepad.
now my data is considered as text and I can separate the values using "column to text" from DATA tab.
thank you all for your valuable time.

Need Faster way to remove leading Zero from Dates in Excel Output

We have an internal software that exports data reports to Excel, and several of the columns contain dates in the MM/DD/YYYY format. I have a spreadsheet setup where I will copy these exported reports over to, and then I have formulas already setup to look for these dates. The problem currently is that all dates in months 1-9 come in like this: 01/22/2017.
The formulas do not recognize them until I activate each and every cell, and hit enter, and then it re-formats to: 1/22/2017 and then everything works. Currently I am having to go cell by cell and activate, and hit enter. Simply selecting all of the cells, and changing the formatting to a Date hasn't worked. Is there a faster way around this? I'm open to VBA if it works, however I'm wondering if there is a simpler method I'm missing, or simply a way to get my formulas to recognize the original date formatting.
EDIT:
The data is exported from a SQL database. The formulas that need to reference are using <= in reference to week start dates on another sheet. I initially tried having those match this formatting, but because the dates come as General, math operators don't work at that point.
Select the date column and then click Data > Text to Columns
Next, Next, then select Date 'MDY'
then you should be able to do this using a number format of m/dd/yyyy or m/d/yyyy if you don't want a leading zero in the days as well
select the range and run:
selection.value = selection.value
(for some cases this does not work)... in such moments this should do:
selection.value = evaluate("INDEX(" & selection.address & "+0,)")
just keep in mind that there may be a need of changing the formatting of the cell...
This also can be done in a formula, without having to go through menu options, thus making it more automatic. Use TEXT and command and format the date exactly how you want. Leaving off leading zeros on the month or day is as simple as including just one character for them instead of two:
=TEXT(date-field,"M/D/YYYY")
instead of
=TEXT(date-field,"MM/DD/YYYY")

Convert text date/time to a real date time in excel

My data is extracted from an application and it has a text that looks like a date/time in excel. How do I actually convert "3/24/2016 11:22:07 PM" (in text) to a real date/time conversion? I've tried formatting the cells but it doesn't work.
For a date conversion:
=DATEVALUE(TEXT(A1,"MM/DD/YYYY"))
For a time conversion:
=TIMEVALUE(TEXT(A1,"HH:MM:SS"))
For datetime conversion:
=DATEVALUE(TEXT(A1,"MM/DD/YYYY"))+TIMEVALUE(TEXT(A1,"HH:MM:SS"))
Where A1 has the data you wish to convert.
By the way, then you may wish to format the cell to a date/time or whatever.
Hope that helps.
1) try using the DATEVALUE function and see if that works for you.
2) A more reliable way, since datevalue does not always work is to strip the text out manually and insert it into and excel date value. You are going to want to use a combination of the following functions:
DATE
TIME
IF
FIND
MID
LEFT
RIGHT
LEN
Now in my opinion the easiest way to do this is to work with multiple helper columns to build out all the steps. One column per step. When you get your final answer, you can substitute or copy paste your formulas from the helper columns into the final formula until you are left with one variable. The reason I say this is that the final formula referring to only 1 variable gets rather lengthy/ugly and very hard to trouble shoot if you make a typo, forget a bracket or something goes wrong. When I did this approach I used a totally of 14 columns (includes final formula). When I packed it all up into 1 formula it resulted in this:
DATE(RIGHT(LEFT(A3,FIND(" ",A3)-1),4),LEFT(LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))-1),MID(LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))+1,FIND("/",LEFT(A3,FIND(" ",A3)-1),FIND("/",LEFT(A3,FIND(" ",A3)-1))+1)-FIND("/",LEFT(A3,FIND(" ",A3)-1))-1))+TIME(LEFT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1)+IF(AND(LEFT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1)<12,RIGHT(RIGHT(A3,LEN(A3)-FIND(" ",A3)),2)="AM"),0,12),MID(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1,FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1)-FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))-1),MID(RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)),FIND(":",RIGHT(A3,LEN(A3)-FIND(" ",A3)))+1)+1,2))
Note it is set up using cell A3 as the one with the time as text that needs formatting.
3) You should also be able to use excel's text to column function located on the DATA ribbon about half way across.
4) And of course there will be a way to code it through VBA as an option as well.
=DATEVALUE(A1)+TIMEVALUE(A1) seems to work as well, since each function only returns the value corresponding to what it recognizes in the string. That is, DATEVALUE() ignores the time component, while TIMEVALUE() ignores the date component.

Excel cell reference to other workbook using previous date for name

I need to reference data in my current worksheet (a daily log) to the previous day log (to show past day data).
I can do this in a fixed format using a direct reference to the other workbook. However I would really like to have it automatically reference cells in the previous day log. Thankfully the file names are formatted nicely "2016 01 January.xlsm" so referencing them using a formula shouldn't be hard.
I know I can build cell value that shows the previous day name using:
=text(today()-1, "yyyy dd Mmmm") & ".xlsm"
However, when I attempt to use this within the cell that should reference this sheet it seems my concatenation is broken in:
='[TEXT(TODAY()-1, "yyyy dd Mmmm") & ".xlsm"]Readings'!$J$14
I could easily debug the concatenation in Matlab, but sadly I'm not in that environment and I don't seem to understand how Excel works as well as I should.
Thanks in advance!
Ben
You simply need to prepend your formula with the INDIRECT command. INDIRECT tells excel that you are not explicitly referring to a particular location, but that you want to dynamically calculate a location, and then refer to that calculated spot. Assuming your file names are correct, this should be as easy as:
=INDIRECT(TEXT(TODAY()-1, "'\[yyyy dd mmmm") & ".xlsm]Readings'!J14")
To test out that it creates the file names correctly, consider putting the concatenation formula in a different cell, and then referring to that cell with INDIRECT. This will confirm for you that you aren't mispelling something etc.
*Edited as highlighted by Jeeped, to now properly calculate the file name instead of hardcoding as an explicit string of text.

Run a text formula in an adjacent cell

I'm having trouble trying to figure out a way to have two columns, one to show the entire formula that can be editable and the second to actually perform the formula.
Ideally, I would like my sheet to be set up like this:
Formula | Value
=5+2 | 7
=3-2 | 1
I would like to be able to change the Formula column and have it automatically update the Value column.
I've tried using the GetFormula() function but I don't think that's what I want to do as I would end up in a circular reference based on what I want to do. The closest I've got is using a Right() function and Text in the formula column or a space and removing the space. However, I end up with the text and not the solved formula instead.
Using =RIGHT(A2, LEN(A2)-1)
Formula | Value
=5+2 | =5+2
I have also tried using =RIGHT(A2,LEN(A2)-1) but without the "=" and can't figure out how to convert the "5+2" into text that I can use to solve. I'm hoping to do this with a formula and without a macro/VBA.
Here is how to do it.
1
Open the Name Manager. Control-F3 from the worksheet, and then click the New button.
2
For the Name field in the dialog, enter EVALA. I just picked this name; it stands for "Evaluate A". But you can pick whatever name you like.
3
For the Refers to field, enter this
=EVALUATE($A1)
4
Click OK and then Close.
5
In B1 enter this formula:
=EVALA
That's it.
You can now use this formula on any row in the worksheet and it will evaluate whatever is in the column A cell of the row where you enter the formula.
You can make a user defined function easily enough with VBA, but if you don't regularly use VBA then an alternative method is to create a Name object. Name objects can access certain functions not typically available in a cell's formula. One of these functions is "Evaluate" which will evaluate a string as a mathematical expression. Here's a demonstration how to do this.
NOTE: Pay special attention to the use of $. Chances are you don't want any $ in your name definition since that will prevent it from behaving in a relative manner. Also, Sheet1! means that this will not work on another sheet.
Update I just want to give credit for this method to the following sources. This is pretty neat stuff, so for anyone interested give it a read. The last link in particular gives a neat example of creating a chart with no data points.
MSDN Evaluating Defined Names
The power of evaluate (ozgrid)
XL4 Macro Functions in Names - JKP
Evaluate and Indirect
More unique functionality of Defined Names
This is probably the best solution for the OP, since it asks to avoid using VBA; however, this method is somewhat limited. It requires manual set-up on every sheet to be used. Much preferable I think is to create a very simple UDF like this...
Function Eval(Expr As String)
Eval = Application.Evaluate(Expr)
End Function
This can be added to any accessible add-in, making it available to any instance of Excel. A little more set-up, but less maintenance.
Just put a single ' before the formula in your A column:
'=5+2 will show in your cell as "=5+2". Then in the B column, just do the formula as normal, =5+2.

Resources