change date format excel - excel

I have the following date in excel "5/2/2020", I would like to obtain "5/Feb/2020", but unfortunately I am getting "May/2/2020".
Also tried to change the format of the cells , but excel still interprets that the first number is the month.
How do I tell Excel that the month is in the second position ?

Try:
If 5/2/2020 is in cell E5 then:
=VALUE(TEXT(E5,"d/mm/yyyy"))
and then custom number format this cell to d/mmm/yyyy
will yield 5/Feb/2020

OK, here is a new method tested with the following dates 5/2/2020, 22/2/2020, 22/12/2020, 12/2/2020.
This function splits the date numbers into 3 parts and places them into an array using the "/" as the split delimiter. It then reassembles the 3 parts in the order you wanted.
Step 1: Add the following Custom Function to a VBA Module:
Function Dates(Cell As Variant)
'Forces recalculation when call values are changed
Application.Volatile
Dim DateString As String
Dim DateArray() As String
DateString = Cell
DateArray = Split(DateString, "/")
Dates = DateArray(1) & "/" & DateArray(0) & "/" & DateArray(2)
End Function
Step 2: For example, enter the date you want to convert in Cell A1 (i.e. 5/2/2020).
Step 3: Put the function formula =VALUE(Dates(A1)) in Cell B1.
Step 4: Apply the custom number format to Cell B1: d/mmm/yyyy and the result will be 5/Feb/2020.
If you want to change the number format to mmm/dd/yyyy, then the result will be Feb/05/2020.
Note: the =VALUE(Dates(A1)) formula will work anywhere on the spreadsheet. All you have to do is change the cell reference A1 to another cell reference. You can also use the formula many times on the same spreadsheet.
Sammy

Related

How do I replace formula text with required IDs depending upon sheet name and having IDs is in fixed column name

I have to replace formula text by required IDs(always in column B) depending upon sheet names referred, as example given below also Screenshot of excel attached herewith (required output is highlighted with yellow color)
eg. formula: =Node!A2 * Comp!A2
required output in new cell: [n1] * [c1]
attached screenshots,
EDIT -
I have tried following things,
1] Added 1 button on Result sheet
2] On click of it called macro having code as below
Sub Button1_Click()
Dim formulaVal As String
Dim str1 As String
Dim str12 As String
formulaVal = Range("A2").Formula
Debug.Print formulaVal
'MsgBox Range("J2").Value2
str1 = Replace(formulaVal, "!A", "!B")
Debug.Print str1
'ActiveSheet.Range("E2").Formula = strl
ActiveSheet.Range("E2").Formula = "=Node!B2*Comp!B2"
'MsgBox Range("u2").Value
strl2 = Range("E2").Formula
Debug.Print strl2
End Sub
3] After button clicking getting required formula in cell but it is showing error data type mismatch as referred data is as string which is not compatible with formula
Please suggest alternate solution if any
You can use this formula in B2 and pull it down:
="n"&VALUE(MID(FORMULATEXT(A2),8,FIND("*",FORMULATEXT(A2))-8))-1 & "*c" & VALUE(MID(FORMULATEXT(A2),FIND("Comp!A",FORMULATEXT(A2))+6,10))-1
If you have a Windows environment & Office 365 you could use the following:
=LET(split,FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(FORMULATEXT(A1),"=",""),"*","</b><b>")&"</b></a>","//b"),
char,LOWER(LEFT(split,1)),
row,MID(split,FIND("!A",split)+2,LEN(split))-1,
"="&TEXTJOIN("*",1,char&row))
FILTERXML() is used to split the text at each *.
Thee result of the split is used to get the first Character (and change it to lower case) and to find the string behind !A, which is the row number (and substract 1 from it).
Then TEXTJOIN() joins the calculated character and row number and places a * between each.
PS if A1 is an actual formula than replace A1 with FORMULATEXT(A1)
(More info on FILTERXML can be found in this post by JvdV: https://stackoverflow.com/a/61837697)
EDIT: I see the question was updated and this only provides an answer to part of the question now. Answer was based on :

After copy and paste values from one workbook to another, a formula doesn't work with a specific range until I doubleClick cells

Using VBA I open one workbook, copy data and paste it to another workbook. In the second workbook I set a formula for specific range of custom format of values(dd/mm/yyyy hh:mm) but the formula doesn't work. Values are kept on the left side of cells as a text with custom format until I double click. Format in both workbooks(worksheets) is the same and I want to keep it.
I've been trying fix it using:
range.TextToColumns Destination:=range DataType:=xlFixedWidth FieldInfo:=Array(Array(0,9),Array(1,1), Array(16,9))
It works within halfway. Date is fit to the right side of cells but my day from custom date is 17 days less???:D When I omit Array(0,9) it returns additional date 02/01/1990 and my original date is moved to the next column.
Has anyone got any solution?
How exactly does .TextToColumns work? How Can I correctly declare arg for FieldInfo to receive original date and fit it to the right side of cells?
below is an example of how to convert a date that is in text format (cell B1) to an actual date. Notice how a text format is left aligned and a date format is right aligned.
Keep in mind that a date is a number-> the integer part is the date and the decimal part is the time.
Public Sub sTestDate()
Dim v As Variant
v = Sheet1.Range("B1").Value
Sheet1.Range("B2").Value = CDbl(CDate(v))
Sheet1.Range("B3").Value = CDbl(CDate(v))
Sheet1.Range("B3").NumberFormat = "dd/mm/yyyy hh:mm"
End Sub

VBA macros use variable and cell reference in yearfrac function

I am trying to calculate the years between dates using a variable and a cell reference but so far everything I have tried returns an invalid argument (or equivalent) error. Basic premise is cells("G2").Formula = "=YEARFRAC(F2, dt)". The variable dt is equal to the last day of the previous month (I know this works because I have tested using MsgBox, so assume the issue is the syntax). The cell F2 contains a date in the form 13-Jan-2018. Can someone please advise of the correct syntax? Thanks.
You're inserting this formula into the cell as a string using VBA. The string "dt" will therefore be inserted verbatim (not the variable).
If you want to insert the date value of dt as stored in VBA at the time, you'll have to use:
Dim dtstr as string
dtstr = Format(dt, "yyyy-mm-dd")
Range("G2").Formula = "=YEARFRAC(F2, DATEVALUE(""" & dtstr & """))"
Note that I have converted dt to a string value for insertion into the cell formula in a standard format.

Subtraction between to excel column using VBScript

Suppose I have below excel sheet,And I need to find the difference between them and result need to put back to another column:
ColA ColB ColC
9/3/2012 8:31:59 AM 09/17/2012 6:45:56 PM Result
9/4/2012 8:31:59 AM 10/17/2012 6:45:56 PM Result
I did it using Loop and Row-By-Row technique. Looking for a way if it can be done directly by column level subtraction. Say ColB-ColA - > ColC. The whole operation should be performed at a time.Result should come "hh:mm:ss".
CODE
IntRow4=2
Do While objSheet4.Cells(IntRow4,1).Value <> ""
If objSheet4.Cells(IntRow4,9).Value <> "Open" Then
Date1=objSheet4.Cells(IntRow4,7).Value
Date2=objSheet4.Cells(IntRow4,8).Value
objSheet4.Cells(IntRow4,11)=TimeSpan(Date1,Date2)
End If
IntRow4=IntRow4+1
Loop
Update
ColA1 ColB1 ColC1 ColA2 ColB2 ColC2 ..... ColAN ColBN ColCN TotaltimeDurtion
Date Date 11:25:20 Date Date 10:25:00 Date Date 11:25:20 ?
here i have shown only one row,But there can be multiple or N number of rows.What I need to do is,I want to add the time durations and put them to the last colum "TotaltimeDurtion".But the last column can not be fixed.And all the columns for each row shouldn't required values,but all never will be empty.Can we also do this also in column level.here the duration is hh:mm:ss format or as per your instruction [h]:mm:ss. TotaltimeDurtion <- ColC1 + ColC2 + ...+ ColCN.
using the range object, I can set the formula on all the cells within a range at once
range("C1:C10").Formula="=B1-A1"
It will also adjust the formula based on the normal copying riles for absolute addressing.
e.g. with the above example, C10 will be =B10-A10. If I had put the formula as "=B1-$A$1" then C10 would have been =B10-$A$1
You can subtract one date from the other, and then set the formatting of the cell:
'Within the Do..While loop
Dim cell
Set cell = objSheet4.Cells(intRow4,11)
cell.Value = Date2 - Date1
cell.NumberFormat = "hh:mm:ss"

Concatenating date with a string in Excel

I have two cells in Excel. one has a string and the other one has a date. in the third cell I want to put the date and the string together. For example:
A1 = "This "
A2 = "03/03/1982"
I want A3 to be:
This 03/03/1982
when I try to put this in the A3 formula: = A1 & A2 it returns some funny numerical value for the date and does not give me the literal date.
Don't know if it's the best way but I'd do this:
=A1 & TEXT(A2,"mm/dd/yyyy")
That should format your date into your desired string.
Edit: That funny number you saw is the number of days between December 31st 1899 and your date. That's how Excel stores dates.
This is the numerical representation of the date. The thing you get when referring to dates from formulas like that.
You'll have to do:
= A1 & TEXT(A2, "mm/dd/yyyy")
The biggest problem here is that the format specifier is locale-dependent. It will not work/produce not what expected if the file is opened with a differently localized Excel.
Now, you could have a user-defined function:
public function AsDisplayed(byval c as range) as string
AsDisplayed = c.Text
end function
and then
= A1 & AsDisplayed(A2)
But then there's a bug (feature?) in Excel because of which the .Text property is suddenly not available during certain stages of the computation cycle, and your formulas display #VALUE instead of what they should.
That is, it's bad either way.
Another approach
=CONCATENATE("Age as of ", TEXT(TODAY(),"dd-mmm-yyyy"))
This will return
Age as of 06-Aug-2013
Thanks for the solution !
It works, but in a french Excel environment, you should apply something like
TEXTE(F2;"jj/mm/aaaa")
to get the date preserved as it is displayed in F2 cell, after concatenation.
Best Regards
You can do it this simple way :
A1 = Mahi
A2 = NULL(blank)
Select A2 Right click on cell --> Format cells --> change to TEXT
Then put the date in A2 (A2 =31/07/1990)
Then concatenate it will work. No need of any formulae.
=CONCATENATE(A1,A2)
mahi31/07/1990
(This works on the empty cells ie.,Before entering the DATE value to cell you need to make it as TEXT).
I found that for this situation, the simplest solution is to define a Custom number format for the cell containing the date. The format in this case should be:
"This:" mm/dd/yyyy
To set this format:
Right click on the cell
Select Format Cell
Select Number tab (should be displayed by default)
Pick Custom from the Category list
Specify the format in the "Type" field
Press OK
Note: If you really want the preceding text to be picked from a cell, this solution will not work as described.

Resources