Insert range reference (date) into formula - excel

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)"

Related

Excel VBA conditional formatting based on another column and cell value [duplicate]

I've got an Excel spreadsheet, with a Macro, that inserts a conditional formatting, like this:
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=UND($A3=""" & lastName & """; $B3=""" & firstName & """)"
As you can see, I've used the German formula for "AND" (i.e. "UND"), and obviously, this code doesn't work as soon as I use it on a French or English version of Excel.
Usually formulas are localized automatically, but how can I insert a formula during run-time that will work on ALL versions?
Ok, thanks for helping me with this, you've helped me crack this one.
It is indeed not possible to just use English. One can use English when operating on a formula, eg. by setting coding Range("A1").formula="AND(TRUE)", but this does not work with FormatConditions.
My solution is a function that writes a formula temporarily to a cell, reads it through the FormulaLocal property, and returns the localized formula, like so:
Function GetLocalizedFormula(formula As String)
' returns the English formula from the parameter in the local format
Dim temporary As String
temporary = Range("A1").formula
Range("A1").formula = formula
Dim result As String
result = Range("A1").FormulaLocal
Range("A1").formula = temporary
GetLocalizedFormula = result
End Function
The returned formula can be used on FormatConditions, which will be re-localized or un-localized when the document is later opened on a different-language version of Excel.
I just found a very elegant solution to the problem in a German Excel forum. This doesn't write to a dummy cell but rather uses a temporary named range. I used the original idea (credit to bst) to write a translating function for both directions.
Convert localized formula to English formula:
Public Function TranslateFormula_LocalToGeneric(ByVal iFormula As String) As String
Names.Add "temporaryFormula", RefersToLocal:=iFormula
TranslateFormula_LocalToGeneric = Names("temporaryFormula").RefersTo
Names("temporaryFormula").Delete
End Function
Convert English formula to localized formula:
Public Function TranslateFormula_GenericToLocal(ByVal iFormula As String) As String
Names.Add "temporaryFormula", RefersTo:=iFormula
TranslateFormula_GenericToLocal = Names("temporaryFormula").RefersToLocal
Names("temporaryFormula").Delete
End Function
This is very handy if you need to deal with formulas in conditional formatting, since these formulas are always stored as localized formulas (but you could need their generic version, e.g. to use Application.Evaluate(genericFormula)).
Store (a trivial version of) the formula in a (hidden) cell in your workbook.
Then when you open the workbook that formula will be translated automatically by excel for the user.
Now you just have to dissect this formula in your script (find the opening bracket "(" and take the past left of that:
Use something like:
strLocalizedFormula = Mid(strYourFormula, 2, InStr(1, strYourFormula, "(") - 2)
where strYourFormula will be a copy from the formula from your worksheet.
I hope this works as I only use an English environment.
Also from reading this:
http://vantedbits.blogspot.nl/2010/10/excel-vba-tip-translate-formulas.html
I am thinking you should (only) be able to use the english version of a cell formula from VBA.
Maybe try this (untested as I only have English version insatlled)
Write your international version of the formula to an out of the way cell using Range.Formula . Then read it back from Range.FormulaLocal, and write that string to the FormatConditions
I know this thread is ages old, and someone may have found an elegant solution, but I just had the same problem where I needed to apply conditional formatting without modifying the sheet, creating temporary cell contents or named ranges. All users use English language versions of Excel, so the functions used in the formulas are the same, but the regional settings vary by location, and therefore also the parameter separater; In Norwegian, it's ";" instead of ",", much like the rest of Europe, I guess.
For example, I needed to automatically create conditional formatting, using Excel formula for the following criterion:
.FormatConditions.Add xlExpression, Formula1:="=AND(ISNUMBER(B" & I & "),B" & I & ">=" & Ul1 & ")"
Where "Ul1" is a value defined in a previous step, and it's not important for the solution.
However, I needed to be able to run this on computers with both Norwegian and English settings
I and found a very short and simple solution from Andrew Pulsom here: https://www.mrexcel.com/board/threads/french-vba-vs-english-vba.729570/. He just made the parameter separator into a variable:
If Application.International(xlDecimalSeparator) = "," Then
Sep = ";"
Else
Sep = ","
End If
Cl1 = "=AND(ISNUMBER(B" & I & ")" & Sep & "B" & I & "<" & Ul1 & ")"
Worked like a charm for me :)
I know that this only solves part of the problem, but I assume that this could apply to many international companies which use English Office installations with local regional settings.
Thanks everyone! I found the post very useful.
My solution is a combination of others, I add it in case somebody finds it useful.
Dim tempform As String
Dim strlocalform1 As String
Dim strlocalform2 As String
' Get formula stored in WorksheetA Cell O1 =IFERROR(a,b)
tempform = Worksheets("Sheet").Range("O1").Formula
' Extract from the formula IFERROR statement in local language.
strlocalform1 = Mid(tempform, 2, InStr(1, tempform, "(") - 1)
' Extract from the formula separator , (comma) in local settings.
strlocalform2 = Mid(tempform, InStr(1, tempform, "a") + 1, 1)
' Add formula in local language to desired field.
pvt.CalculatedFields.Add Name:="NewField", Formula:="=" & strlocalform1 & "FORMULA" & strlocalform2 & ")"
Hope this helps!
Please refer to the link for more explanation: https://bettersolutions.com/csharp/excel-interop/locale-culture.htm
CultureInfo baseCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo(xlapp.LanguageSettings.LanguageID(Office.MsoAppLanguageID.msoLanguageIDUI));
// do something
System.Threading.Thread.CurrentThread.CurrentCulture = baseCulture;

change date format 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

excel function to combine cells into single line of text?

I'm new to stack overflow so I apologize if this is a horrendously stupid question. I am wondering if there is a function or way to code a function in excel that will combine a column of cells with plain text and convert them into one cell with the text on a single line? Specifically I want to convert a column of random numbers into a single line of text and insert SPACE+AND+SPACE between them.
Ex.
15133484
12345188
12345888
to
15133484 AND 12345188 AND 12345888
Currently I am copying and pasting all this information into google and then into Word and using find/replace and it is taking forever everytime. If it is possible to just get Excel to do this for me that would be amazing.
Thanks!
If you have Office 365 Excel use TEXTJOIN():
=TEXTJOIN(" AND ",TRUE,A:A)
otherwise one would have to use:
=A1 & " AND " & A2 & " AND " & A3
Or one can use a helper column, B1 put:
=A1
put this in B2 and copy down:
=IF(A2<>"",B1 & " AND " & A2,B1)
And grab the last cell in column B.
A little late, but still:
Reference here
Step 1:
=concatenate(transpose(rngBeg:rngEnd & " AND "))
Step 2:
highlight the transpose statement and then press F9, which substitutes the actual values for the formula.
Step 3:
Remove the curly braces, { }, from the formula. The cell will display the range of reference cells combined with whatever separator chosen after the ampersand sign.
Not a "live" formula, but still far easier than manually concatenating a range of values.
Press ALT+F11 to open Microsoft Visual Basic for Applications,
Insert-> Module
Paste this:
Function Combine(WorkRng As Range, Optional Sign As String = " AND ") As String
Dim Rng As Range
Dim OutStr As String
For Each Rng In WorkRng
If Rng.Text <> "," Then
OutStr = OutStr & Rng.Text & Sign
End If
Next
Combine = Left(OutStr, Len(OutStr) - 5)
End Function
In any cell type =Combine(Range)
i.e.
=Combine(A1:A500)
use concat function if you can add an additional column in the excel like this:
=CONCAT(D3:E5)
Attached sample image with input, additional column, output and formula
I assume you want to merge the data in the 3 cells into a single cell with a space between the 3 data set.
If that is the case then you can do it simply by using the Concatenate function in excel.
In the above example, you have data in Cells A1, A2 & A3.
Cell C1 has the merged data. As you can see, we have used CONCATENATE Function.
The space has been defined in Double quotes. So if you need a Hyphen (-), you can put that in Double Quotes with space “ - ” and it will display the result with Sanjay - Singh - Question
Hope this helps.

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.

Concatenation of string in excel having today() function

In a cell in excel i want to concatenate a string that contain format as below ---
Brett201505LeeMay15
I am trying to do that by today function like -
= "Brett" & Today() & "Lee" & Today()
issue is how to specify date format as "yyyymm" and "mmmyy" in a single cell.
Write now when the cell format is general i am getting some number. I guess it is datevalue. Please help.
Use this expression :
="Brett"&TEXT(TODAY(),"yyyymm")&"Lee"&TEXT(TODAY(),"mmmyy")

Resources