Date fields and formulas that change format - excel

I have a huge issue with trying to copy values onto my database
lets say i have 2 columns, A is integer and B is date
I'm trying to build column C with the expression
= "(" &A2& ",'" & B2& "'),"
Expected output (1000, '2020-01-29'),
Obtained output (1000, '43859'), (?????)
Is there a function that allows me to do a formula but keep the date as it is?

Change your function to
= "(" &A2& ",'" &TEXT(B2,"DD/MM/YY")& "'),"
.
The =TEXT() function takes the value you want to format and the format as arguments.
=TEXT(Value you want to format, "Format code you want to apply")
Click here for the TEXT function manual

Related

How can I replace multiple string at once in Excel?

The function I expected
some_function(original_text, "search_text", "replacement_text")
The value of the second & third parameters will be multiple characters. For example. The result will replace the character based on the location of the character at the second & third parameters
some_function("9528", "1234567890", "abcdefghij")
1 -> a
2 -> b
3 -> c
...
8 -> h
9 -> i
0 -> j
The result of some_function will be iebh. The nested SUBSTITUTE function can archive the goal but I hope to compact the complexity.
The way you described your requirement is best written out via REDUCE(), a lambda-related helper function and recently announced to be in production:
=REDUCE("9528",SEQUENCE(10),LAMBDA(x,y,SUBSTITUTE(x,MID("1234567890",y,1),MID("abcdefghij",y,1))))
Needless to say, this would become more vivid when used with cell-references:
Formula in A3:
=REDUCE(A1,SEQUENCE(LEN(B1)),LAMBDA(x,y,SUBSTITUTE(x,MID(B1,y,1),MID(C1,y,1))))
Another, more convoluted way, could be:
=LET(A,9528,B,1234567890,C,"abcdefghij",D,MID(A,SEQUENCE(LEN(A)),1),CONCAT(IFERROR(MID(C,FIND(D,B),1),D)))
Or, as per the sceenshot above:
=LET(A,A1,B,B1,C,C1,D,MID(A,SEQUENCE(LEN(A)),1),CONCAT(IFERROR(MID(C,FIND(D,B),1),D)))
Function Multi_Replace(Original As String, Search_Text As String, Replace_With As String) As String
'intEnd represents the last character being replaced
Dim intEnd As Long: intEnd = WorksheetFunction.Min(Len(Search_Text), Len(Replace_With))
'necessary if Search text and replace text are different lengths;
Dim intChar As Long 'to track which character we're replacing
'Replace each character individually
For intChar = 1 To intEnd
Original = Replace(Original, Mid(Search_Text, intChar, 1), Mid(Replace_With, intChar, 1))
Next
Multi_Replace = Original
End Function
Maybe simpler if you do not have lambda yet: =TEXTJOIN(,,CHAR(96+MID(A1,SEQUENCE(LEN(A1)),1)))
*Note that this will not return 0 as the expected result.
Let's say you have a list of countries in column A and aim to replace all the abbreviations with the corresponding full names. you start with inputting the "Find" and "Replace" items in separate columns (D and E respectively), and then enter this formula in B2:
=XLOOKUP(A2, $D$2:$D$4, $E$2:$E$4, A2)
Translated from the Excel language into the human language, here's what the formula does:
Search for the A2 value (lookup_value) in D2:D4 (lookup_array) and return a match from E2:E4 (return_array). If not found, pull the original value from A2.
Double-click the fill handle to get the formula copied to the below cells, and the result won't keep you waiting:
Since the XLOOKUP function is only available in Excel 365, the above formula won't work in earlier versions. However, you can easily mimic this behavior with a combination of IFERROR or IFNA and VLOOKUP:
=IFNA(VLOOKUP(A2, $D$2:$E$4, 2, FALSE), A2)

excel formula to find maximum within same cell have multiple values of right most numbers

I want to find maximum and minimum for right most number from same cell having multiple values, below is the how data looks.
This all values are in same cell, ex: in cell "A1"
I want to extract rightmost number, like 1.00,1.5,1.5,1.0,2.00,1.50,1.0,1.00 and find max and min from it.
I am currently using Excel 2016 version.
BAEK1928_TXL_1.00
44444922_FLR2X-A_1.5
44447922_FL2X-B_1.5
44444922_FL2X-B_1.0
BAEK1928_TXL_2.00
44444922_FLR2X-B_1.50
44444922_FLR2X-A_1.0
44444922_FLRY-B_1.00
Is there any excel formula or vba code for it?
I have tried some excel formula as below but it doesn't work as required.
=MAX(RIGHT(A1,LEN(A1)-FIND("_",A1,SEARCH("_",A1)+1))
Data samples below where the code not work:
KMO6722-1_THICK_0.50
MAEJ9120_GXL_0.50
BIN76822_FLRY-B_0.50
KMO6722-1_THIN_0.50
MAEJ9120_TXL_0.50
KL STLE 2987_0.5
MAEJ9120_SXL_0.50
DIN76722_FLRY-A_0.50
MAEJ9120_TXL_1.00
KMO6722-1_FLR2X-B_1.00
MAEJ9120_GXL_1.00
BIN76822_FL2X-B_1.0
MAEJ9120_GXL_0.50
MAEJ9120_GXL_0.80
BIN76822_FLR2X-A_1.0
BIN76822_FLRY-B_1.00
MAEJ9120_TXL_0.80
KMO6722-1_FLR2X-B
MIL-STD-104_PTFE_2.0
BIN76822 _THIN_1.00
BIN76822 _THIN_2.00
MIL-STD-104_PTFE_1.0
MAEJ9120_TXL_0.80
BIN76822_FLRY-B_1.50
BIN76822-1_FLR2X-B
MAEJ9120_TXL_1.00_Sn
PLAIN_EXRD_1.00
MAEJ9120_TXL_2.00_Sn
MAEJ9120_TXL_0.80_Sn
BIN76822_TXL_1.00
BIN76822_THIN_1.50
Below Snap-shot of error:
You will need to split the string on the line break then find the second _ and grab all that comes after it.
=MAX(--MID(TRIM(MID(SUBSTITUTE(A1,CHAR(10),REPT(" ",999)),(ROW($ZZ$1:INDEX($ZZ:$ZZ,LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))+1))-1)*999+1,999)),FIND("}}}",SUBSTITUTE(TRIM(MID(SUBSTITUTE(A1,CHAR(10),REPT(" ",999)),(ROW($ZZ$1:INDEX($ZZ:$ZZ,LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),""))+1))-1)*999+1,999)),"_","}}}",2))+1,999))
This is an array formula and must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode. If done correctly then Excel will put {} around the formula.
For Minimum replace the MAX for MIN
Following is a VBA based function. You need to insert a module in VBA and paste below code.
Public Function GetMinMax(strInput As String, Optional varMode)
Dim strMode As String
If IsMissing(varMode) Then
strMode = "MAX"
Else
strMode = varMode
End If
Dim varInput
varInput = Split(strInput, Chr(10))
Dim varOut() As Double
Dim i As Long
ReDim varOut(UBound(varInput))
For i = LBound(varInput) To UBound(varInput)
varOut(i) = Split(varInput(i), "_")(2)
Next i
If strMode = "MAX" Then
GetMinMax = Application.Max(varOut)
Else
GetMinMax = Application.Min(varOut)
End If
End Function
To get MAX you can use:
=GetMinMax(A1) or =GetMinMax(A1,"MAX")
For MIN you can use:
=GetMinMax(A1,"MIN")
With Excel 2013+, you can use FILTERXML to split the rows within the cell into an array; and then apply one of the usual methods to examine the rightmost, underscore separated segment as a number:
=MAX(--(RIGHT(SUBSTITUTE(FILTERXML("<t><s>" & SUBSTITUTE(A1,CHAR(10),"</s><s>")&"</s></t>","//s"),"_",REPT(" ",99)),99)))
=MIN(--(RIGHT(SUBSTITUTE(FILTERXML("<t><s>" & SUBSTITUTE(A1,CHAR(10),"</s><s>")&"</s></t>","//s"),"_",REPT(" ",99)),99)))
Or, you can just create an XML with nodes/subnodes at the LF and _ and use the XPATH to return the last element in each substring; then apply MAX and MIN:
= MAX(FILTERXML("<t><s><u>" & SUBSTITUTE(SUBSTITUTE(A1,"_","</u><u>"),CHAR(10),"</u></s><s><u>") & "</u></s></t>","//u[last()]"))
= MIN(FILTERXML("<t><s><u>" & SUBSTITUTE(SUBSTITUTE(A1,"_","</u><u>"),CHAR(10),"</u></s><s><u>") & "</u></s></t>","//u[last()]"))
Another shorter formula option using FILTERXML function, just adopt XPATH to return MAX/MIN value and need not REPT, MAX and MIN function
To get MAX value, you can use:
=FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(A1,CHAR(10),"_A"),"_","</b><b>")&"</b></a>","//b[not(//b>.)>.*0]")
To get MIN value, you can use:
=FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(A1,CHAR(10),"_A"),"_","</b><b>")&"</b></a>","//b[not(//b<.)>.*0]")

How can I concatenate text from multiple rows in a Schematiq table into a single value

In Microsoft Excel I have a Schematiq table that contains values in a number of rows:
I would like to concatenate all of the row values into a single value and store this in a cell delimited by the text "-colour-"
So from the example above, the desired output would be: red-colour-yellow-colour-pink-colour-green-colour-orange-colour-purple-colour-blue
Is this possible using a custom aggregate function?
Yes, this is possible using a custom aggregation function. Maybe you already tried something like this:
=tbl.Group(F7, , "A")
You'll have found that you get #VALUE! because Schematiq tries to add up the values in column A, and can't because they are not numbers.
When you supply a custom function to tbl.Group, it uses it on the first two values in the column, then that result and the next value, and so on. So to replicate the default behaviour you could use something like:
(x, y) => x + y
Based on your question though, you want to combine two text values using a function like this:
(x, y) => x & '-colour-' & y
..which would look like this as a call to tbl.Group:
=tbl.Group(F7, , "A", "(x, y) => x & '-colour-' & y")
You can then wrap this in a call to tbl.GetValue to put the answer straight into a cell.
Alternatively, you could do this, which gets all the values from the table column and uses txt.Join to combine them:
=txt.Join(tbl.GetValues(F7), "-colour-")
This is probably simpler in this specific case.
(incidentally, I'm part of the Schematiq team)

Excel - How to get expression text rather than the value

I'm having problem with a big set of Excel data. One others had inputted the data like this:
A
10
10:12
11:12:15
My task is to convert it to something like this:
B
Pig
Pig:Koala
Dog:Koala:Bird
I was trying to use substitute:
= SUBSTITUTE(A1, "10", "Pig")
But the problem is, Excel recognizes those value in A column as other data types (number, time...) and the SUBSTITUTE doesn't work on those types.
How could I fix this issue?
Thank you.
This function will return a string that matches what excel is displaying.
Option Explicit
Function ToText(r As Range) As String
If r.Count <> 1 Then
ToText = "#ERR!"
Exit Function
End If
ToText = IIf(r.NumberFormat = "General", CStr(r.Value), Format(r.Value, r.NumberFormat))
End Function
for example, if 10:11:12 is in A1, which excel thinks is a time, and is formatted this way, then =ToText(A1) will return the string 10:11:12, which you can then manipulate as you would any other text
put this into a module on the spreadsheet ( ALT + F11 ) so the function is available to excel
Select column A from first to last record and right click on that,
then change the format by clicking Format cells...
and choose whatever format you want...
like
then use SUBSTITUTE(A1, "10", "Pig") method

String conversion with TEXT formula character limit?

I am attempting to format a single number stored as a text value.
For example, I would like to convert:
5145350002005000080
To:
5145-350002-00500-0080
The formula I am using is:
=text(A1,"0000-000000-00000-0000")
The output I am receiving is:
5145-350002-00500-0000
Why are the last 4 characters "0000" instead of "0080" as I would expect? Is there a character limit, or is my formula incorrect?
Quote from Large Number Arithmetic:
The limit in Excel is 15 significant digits in a number. Enter a 16
digit credit card number and 1234567890123456 will become
1234567890123450.
Actually, even 5145350002005001111 will result in 5145-350002-00500-0000.
Moreover, take a look at formula bar when your input cell is selected - for my Excel 2007 I see:
Hope that was helpful)
EDITED:
As a solution to solve the task - keep your numbers formatted as text and use the following formula:
=LEFT(A1,4)&"-"&MID(A1,5,6)&"-"&MID(A1,11,5)&"-"&RIGHT(A1,4)
Here is a custom function. Place it in a regular code module of the workbook and you can call it in the cell by =FormatLargeNumber("A1")
Public Function FormatLargeNumber(val As String)
'This function parses extremely large numbers per your example.
' Modify as needed.
FormatLargeNumber = Left(val, 4) & "-" _
& Mid(val, 5, 6) & "-" & _
Mid(val, 11, 5) & "-" & _
Right(val, 4)
End Function

Resources