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.
Related
Im just going to include the code I already have. I would like to be able to take the date I have in cell A1 and reference it in the vlookup formula I have. To put the formula with the referenced date in Range(“H3”). If some one could help out I would really appreciate it.
=IFERROR(VLOOKUP($I5,’J:\Optimization\Cut RiteV11\EXPORT\[Dovetail Drw Btms For (Reference to A1).xlsx] Part summary’!$A$6:$G$600,7,0),0)
Use Format$ to format the date, and & to concatenate it into the formula.
Also, use straight single and double quotes: ' and ", not ’ and “”.
Dim dt As String
dt = Format$(Range("A1").Value, "mm-dd-yyyy") ' change as necessary
Range("H3").Formula = "=IFERROR(VLOOKUP($I5,'J:\Optimization\Cut RiteV11\EXPORT\[Dovetail Drw Btms For " & dt & ".xlsx] Part summary'!$A$6:$G$600,7,0),0)"
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
I am reading some cells of type accounting into a Variant variable and then writing the variable in a new workbook. When I do this, the number formatting gets all mixed up.
I first read the cell from the first workbook with value 69,77 € and cell format Accounting:
Dim tiendas As Variant
x=.Cells(1,1)
Then I create a new workbook and write the content of the variable into a cell:
wb.Worksheets(1).Cells(1, 1) = x
If I don´t do anything else, the copied value looks like this 697.715 and has a cell format of Number.
Then I try changing the cell format before writing the value, i.e.:
wb.Worksheets(1).Range("A1:A1").Style = "Currency"
wb.Worksheets(1).Cells(1, 1) = x
As a result I get € 697.715,00 and cell type Custom.
I have tried inverting the order, i.e. first writing the value and then changing the style to "Currency". This results in exactly the same result.
I have also tried changing the format in the different way, specifically by using what appears in the custom formatting in Excel (and doubling the quotes inside the string):
wb.Worksheets(1).Range("A1:A1").NumberForma="_-* #.##0,00 €_-;-* #.##0,00 €_-;_-* "" - ""?? €_-;_-#_-"
wb.Worksheets(1).Cells(1, 1) = x
The result in this case is 697715,000 € and cell format Custom.
It seems that the original number format is not being read properly but I don´t know how to fix this.
Any ideas are very mucho welcome.
Thanks in advance!
NOTE: My number setting in Windows are "," as a decimal symbol and "." as thousands separator.
I have been trying to solve an issue I have with copying dates from Word document to Excel using VBA.
My issue is the format. If I put dd/MM/yyyy it will change during the process to MM/dd/yyyy and I cannot solve the issue.
Basically for copying I am using this line:
Sheets("wImp").Range("AA" & i) = wdDoc.ContentControls(1).Range.Text
However during the process the data is modified.
I originally used LegacyForms but it did work and I hoped Date Picker would help but it does not. And in a case the date is impossible to switch like 21/12/2017 it will stay as it should. But when it can be switched like 1/5/2017 it will be switched to 5/1/2017.
!!! Nevertheless the cell format stays dd/MM/yyyy for all the dates, but the months and days position is switched.
See below for what I have as input in Word and the output in Excel.
Word:
Excel:
If you want to use the continental date format "dd/mm/yyyy" in your sheet you must use Format() function. So, try this:
Sheets("wImp").Range("AA" & i) = Format(wdDoc.ContentControls(1).Range.Text, "dd/mm/yyyy")
Sheets("wImp").Range("AA" & i).NumberFormat = "dd/mm/yyyy"
Oficial documentation of Format() function
Sub test()
ActiveSheet.Range("A1").NumberFormat = "#"
ActiveSheet.Range("A1").Value = "01/05/2017"
ActiveSheet.Range("A2").NumberFormat = "m/d/yyyy"
ActiveSheet.Range("A2").Value = "01/05/2017"
ActiveSheet.Range("A3").NumberFormat = "yyyy/m/d;#"
ActiveSheet.Range("A3").Value = "01/05/2017"
End Sub
Try something like this.
Excel will convert the data during copy.
So, change "NumberFormat" before copy.
Assigning a string to the cell makes Excel think that the string should be interpreted in a "mm/dd/yyyy" format irrespective of your locale (and irrespective of the cell's date format).
So convert the string to a Date using the CDate function (which will be done using your system date/time settings) before assigning the value to the cell.
Sheets("wImp").Range("AA" & i) = CDate(wdDoc.ContentControls(1).Range.Text)
That date can then be displayed in whatever date format you want to use for the cell.
I'm using CDate to convert a particular date formatted as string to the Excel Date type. I wrote a small UDF for this conversion; however when I run the function as a in-cell function, it returns a number and the calling cells default format is set to Number type. When I manually convert it to Date type, Excel will display the correct date entry.
So, I need to know whether there is a way to set the default format of the calling cell to Date type through my Date conversion macro. Otherwise, the user has to manually change the format of each of cell.
e.g
Function Test()
Test = CDate("2010/12/23")
End Function
The calling cell is exposed via Application.ThisCell however your going to have to manually format before/after the call as an Excel UDF cannot modify the physical characteristic of a cell.
Perhaps you can run something after the cells have been entered?
Dim c As range
For Each c In ActiveSheet.UsedRange.Cells
''Case sensitive
If c.Formula = "=test()" Then
c.NumberFormat = "d/mm/yyy"
End If
Next
It sounds like what you want to do is actually a two-part process: convert a value in a cell (or range of cells) to a date and then display it as a date, as opposed to simply converting a text value that looks like a date to a date value.
If that is the case, then I would recommend modifying Remou's suggestion like so (using UsedRange can be problematic on worksheets containing large amounts of data):
Dim c As Range
For Each c In Selection.Cells
c.Value = CDate(c.Value)
c.NumberFormat = "m/d/yyyy"
Next c
The user would then need to select the cells to which the formatting should be applied and run the macro; it sounds as though you do not wish this to happen, but I'm not sure that is possible.
Depending on how you want to use the macro, you can add additional checks: for example, you could apply the formatting only to non-blank cells currently formatted as text (c.Value <> "" and c.NumberFormat = "#").
If you return the data as a string and in a format that Excel automatically converts to a date, you can get the cell to format properly.
Function Test() As String
Test = Format(DateValue("2010/12/23"), "mm/dd/yyyy")
End Function
This works in US Excel, but if you need it to be language agnostic, I think you'll have problems.