I am searching for a way in excel to check a cell for other characters than the alphabet a-z, numbers 0-9 and the character "-"
In column "A" I have a list of product names like
A1: samsung-s7-black
A2: apple-phone-6-silver
A3: huawei-p9-limited-edition!
In column "B" I would like to get the following info
B1:
B2:
B3: !
Basically I am looking for a "negative" search in which i don't define which characters are not allowed but more which characters are allowed in my cell and output the characters that do not match. If this could be done without VBA even better.
If you have Office 365 / Excel 2016, you can use the TEXTJOIN function in an array formula:
B1: =TEXTJOIN("",TRUE,IF((CODE(MID(A3,seq,1))>=97)*(CODE(MID(A3,seq,1))<=122)+(CODE(MID(A3,seq,1))=45)+ISNUMBER(--MID(A3,seq,1))=1,"",MID(A3,seq,1)))
Since this is an array formula, you need to "confirm" it by holding down ctrl + shift while hitting enter. If you do this correctly, Excel will place braces {...} around the formula as observed in the formula bar
seq is a Name'd formula that refers to:
=ROW(INDEX(Sheet1!$1:$65535,1,1):INDEX(Sheet1!$1:$65535,LEN(INDIRECT("RC[-1]",FALSE)),1))
Note that we use the RC version of INDIRECT so the formula needs to be placed in the adjacent column of the string being tested.
Oh, and if you have mixed case in your actual data, replace A1 in the formula with =LOWER(A1)
=TEXTJOIN("",TRUE,IF((CODE(MID(LOWER(A1),seq,1))>=97)*(CODE(MID(LOWER(A1),seq,1))<=122)+(CODE(MID(LOWER(A1),seq,1))=45)+ISNUMBER(--MID(LOWER(A1),seq,1))=1,"",MID(LOWER(A1),seq,1)))
If you do not have the TEXTJOIN function, you could do a nested SUBSTITUTE or use a VBA solution.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(LOWER(A1),"a",""),"b",""),"c",""),"d",""),"e",""),"f",""),"g",""),"h",""),"i",""),"j",""),"k",""),"l",""),"m",""),"n",""),"o",""),"p",""),"q",""),"r",""),"s",""),"t",""),"u",""),"v",""),"w",""),"x",""),"y",""),"z",""),"0",""),"1",""),"2",""),"3",""),"4",""),"5",""),"6",""),"7",""),"8",""),"9",""),"-","")
Here's a VBA solution. Put this in a workbook module, and you can call with =remove_alphanumeric(A1)
Function remove_alphanumeric(InputString As String) As String
Dim i As Integer, strLen As Integer
Dim tmp_str As String, final As String
final = ""
i = 1
strLen = Len(InputString)
For i = 1 To strLen
tmp_str = Mid(InputString, i, 1)
If InStr(1, "abcdefghijklmnopqrstuvwxyz0123456789-", tmp_str) = 0 Then final = final + tmp_str
Next
remove_alphanumeric = final
End Function
Related
I have 0,4*A1 in a cell (as a string). How can convert this "string formula" into a real formula and calculate its value, in another cell?
Evaluate might suit:
http://www.mrexcel.com/forum/showthread.php?t=62067
Function Eval(Ref As String)
Application.Volatile
Eval = Evaluate(Ref)
End Function
I concatenated my formula as normal, but at the start I had '= instead of =.
Then I copy and paste as text to where I need it. Then I highlight the section saved as text and press ctrl + H to find and replace.
I replace '= with = and all of my functions are active.
It's a few steps, but it avoids VBA.
UPDATE This used to work (in 2007, I believe), but does not in Excel 2013.
EXCEL 2013:
This isn't quite the same, but if it's possible to put 0.4 in one cell (B1, say), and the text value A1 in another cell (C1, say), in cell D1, you can use =B1*INDIRECT(C1), which results in the calculation of 0.4 * A1's value.
So, if A1 = 10, you'd get 0.4*10 = 4 in cell D1. I'll update again if I can find a better 2013 solution, and sorry the Microsoft destroyed the original functionality of INDIRECT!
EXCEL 2007 version:
For a non-VBA solution, use the INDIRECT formula. It takes a string as an argument and converts it to a cell reference.
For example, =0.4*INDIRECT("A1") will return the value of 0.4 * the value that's in cell A1 of that worksheet.
If cell A1 was, say, 10, then =0.4*INDIRECT("A1") would return 4.
Just for fun, I found an interesting article here, to use a somehow hidden evaluate function that does exist in Excel. The trick is to assign it to a name, and use the name in your cells, because EVALUATE() would give you an error msg if used directly in a cell. I tried and it works! You can use it with a relative name, if you want to copy accross rows if a sheet.
I prefer the VBA-solution for professional solutions.
With the replace-procedure part in the question
search and replace WHOLE WORDS ONLY, I use the following VBA-procedure:
''
' Evaluate Formula-Text in Excel
'
Function wm_Eval(myFormula As String, ParamArray variablesAndValues() As Variant) As Variant
Dim i As Long
'
' replace strings by values
'
For i = LBound(variablesAndValues) To UBound(variablesAndValues) Step 2
myFormula = RegExpReplaceWord(myFormula, variablesAndValues(i), variablesAndValues(i + 1))
Next
'
' internationalisation
'
myFormula = Replace(myFormula, Application.ThousandsSeparator, "")
myFormula = Replace(myFormula, Application.DecimalSeparator, ".")
myFormula = Replace(myFormula, Application.International(xlListSeparator), ",")
'
' return value
'
wm_Eval = Application.Evaluate(myFormula)
End Function
''
' Replace Whole Word
'
' Purpose : replace [strFind] with [strReplace] in [strSource]
' Comment : [strFind] can be plain text or a regexp pattern;
' all occurences of [strFind] are replaced
Public Function RegExpReplaceWord(ByVal strSource As String, _
ByVal strFind As String, _
ByVal strReplace As String) As String
' early binding requires reference to Microsoft VBScript
' Regular Expressions:
' with late binding, no reference needed:
Dim re As Object
Set re = CreateObject("VBScript.RegExp")
re.Global = True
're.IgnoreCase = True ' <-- case insensitve
re.Pattern = "\b" & strFind & "\b"
RegExpReplaceWord = re.Replace(strSource, strReplace)
Set re = Nothing
End Function
Usage of the procedure in an excel sheet looks like:
In my opinion the best solutions is in this link:
http://www.myonlinetraininghub.com/excel-factor-12-secret-evaluate-function
Here is a summary:
In cell A1 enter 1,
In cell A2 enter 2,
In cell A3 enter +,
Create a named range, with =Evaluate(A1 & A3 & A2) in the refers to field while creating the named range. Let's call this named range "testEval",
In cell A4 enter =testEval,
Cell A4 should have the value 3 in it.
Notes:
a) Requires no programming/VBA.
b) I did this in Excel 2013 and it works.
Say, let we have column E filled by formulas that returns string, like:
= " = " & D7
where D7 cell consist more complicated formula, that composes final desired result, say:
= 3.02 * 1024 * 1024 * 1024
And so in all huge qty of rows that are.
When rows are a little - it just enough to copy desired cells as values (by RMB)
to nearest column, say G, and press F2 with following Enter in each of rows.
However, in case of huge qty of rows it's impossible ...
So, No VBA. No extra formulas. No F&R
No mistakes, no typo, but stupid mechanical actions instead only,
Like on a Ford conveyor. And in just a few seconds only:
[Assume, all of involved columns are in "General" format.]
Open Notepad++
Select entire column D
Ctrl+C
Ctrl+V in NPP
Ctrl+A in NPP
Select cell in the first row of desired column G1
Ctrl+V
Enjoy :)
.
Excel 2019 here. EVALUATE isn't valid.
It would work if we created a Named Range out of it:
But in this case we provide an absolute reference, which is not nice:
We would have to modify the formula every time we want to reuse it.
When the value in A1 changes, the evaluated result would not change.
Solution:
=EVALUATE(GET.CELL(5,OFFSET(INDIRECT("RC",FALSE),0,-1)))
The best, non-VBA, way to do this is using the TEXT formula. It takes a string as an argument and converts it to a value.
For example, =TEXT ("0.4*A1",'##') will return the value of 0.4 * the value that's in cell A1 of that worksheet.
have any idea for create unique character from cell in excel?
example :
i have string in cell
A1 = "lorem lipsum bla bla"
and A2 with formula generate char from A1 like "LLBB"; just get first char
with Office 365 Excel you can use CONCAT() in an array formula:
=UPPER(CONCAT(LEFT(TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),(ROW(1:20)-1)*99+1,99)))))
Being an array formula it must be confirmed with Ctrl+Shift+Enter instead of Enter when exiting edit mode. If done correctly Excel will put {} around the formula.
Replace the 20 with the max number of words it can be.
Try this small User Defined Function:
Public Function fLetter(rng As Range) As String
Dim a
For Each a In Split(rng.Value, " ")
fLetter = fLetter & UCase(Left(a, 1))
Next a
End Function
How would I extract all the numbers from a cell, so for example A1 cell contains: "AA59" I would like the formula to extract 59 and ignore AA. I can use =if(right(A1),2) formula but if A1 contains value AA5 then it will select last two characters (which is A5), so this is wrong?
Here is a native worksheet formula solution. It's a little inelegant compared to a VBA user-defined-function, but is a standard (non-array) formula and looks more complex than it actually is.
The formula in B21 is,
=MID(A1, MIN(INDEX(ROW(INDIRECT("1:"&LEN(A1)))+((CODE(MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1))<48)+(CODE(MID(UPPER(A1),ROW(INDIRECT("1:"&LEN(A1))),1))>57))*1E+99,,)), 99)
Fill down as necessary. Wrap the whole thing in a VALUE(...) function or precede MID with a double unary (aka double minus or --) to convert the text to a true number.
As an alternative, consider the Array Formula:
=--MID(SUMPRODUCT(--MID("01"&A1,SMALL((ROW($1:$300)-1)*ISNUMBER(-MID("01"&A1,ROW($1:$300),1)),ROW($1:$300))+1,1),10^(300-ROW($1:$300))),2,300)
to extract digits from a mixed cell:
Array formulas must be entered with Ctrl + Shift + Enter rather than just the Enter key.
If you are always trying to get a single or double digit number from the end of the string try this version:
=LOOKUP(100,RIGHT(A2,{1,2})+0)
try this macro
Function RakamCikart(Txt As String) As String
With CreateObject("VBScript.RegExp")
.Global = True
.Pattern = "[0-9]"
RakamCikart = .Replace(Txt, "")
End With
End Function
I have names in a column. I need to split just the last names from that column into another column.
The last name is delimited by a space from the right side.
The contents in cell A2 = Alistair Stevens and I entered the formula in cell B2 (I need 'Stevens' in cell B2)
I tried using the following formulas:
=RIGHT(A2,FIND(" ",A2,1)-1)
=RIGHT(A2,FIND(" ",A2))
Both these formulas work for this cell but when I fill it down / copy and paste it for the cells below it doesn't work. I get the wrong values!!
A3 -> David Mckenzie
B3 -> Mckenzie
This works, even when there are middle names:
=MID(A2,FIND(CHAR(1),SUBSTITUTE(A2," ",CHAR(1),LEN(A2)-LEN(SUBSTITUTE(A2," ",""))))+1,LEN(A2))
If you want everything BUT the last name, check out this answer.
If there are trailing spaces in your names, then you may want to remove them by replacing all instances of A2 by TRIM(A2) in the above formula.
Note that it is only by pure chance that your first formula =RIGHT(A2,FIND(" ",A2,1)-1) kind of works for Alistair Stevens. This is because "Alistair" and " Stevens" happen to contain the same number of characters (if you count the leading space in " Stevens").
The answer provided by #Jean provides a working but obscure solution (although it doesn't handle trailing spaces)
As an alternative consider a vba user defined function (UDF)
Function RightWord(r As Range) As Variant
Dim s As String
s = Trim(r.Value)
RightWord = Mid(s, InStrRev(s, " ") + 1)
End Function
Use in sheet as
=RightWord(A2)
Try this function in Excel:
Public Shared Function SPLITTEXT(Text As String, SplitAt As String, ReturnZeroBasedIndex As Integer) As String
Dim s() As String = Split(Text, SplitAt)
If ReturnZeroBasedIndex <= s.Count - 1 Then
Return s(ReturnZeroBasedIndex)
Else
Return ""
End If
End Function
You use it like this:
First Name (A1) | Last Name (A2)
Value in cell A1 = Michael Zomparelli
I want the last name in column A2.
=SPLITTEXT(A1, " ", 1)
The last param is the zero-based index you want to return. So if you split on the space char then index 0 = Michael and index 1 = Zomparelli
The above function is a .Net function, but can easily be converted to VBA.
If you want to get the second to last word in a text, you can use this macro as a function in your spreadsheet:
Public Function Get2ndText(S As String) As String
Dim sArr() As String
Dim i As Integer
sArr = Split(S, " ")
'get the next to the last string
i = UBound(sArr) - 1
Get2ndText = sArr(i)
End Function
Then in your spreadsheet B1 as the text:
CURRENT OWNER 915 BROADWAY ST HOUSTON TX 77012-2126
in B2 your formula would be:
=Get2ndText(B1)
The result would be
TX
Simpler would be:
=TRIM(RIGHT(SUBSTITUTE(TRIM(A2)," ",REPT(" ",99)),99))
You can use A2 in place of TRIM(A2) if you are sure that your data doesn't contain any unwanted spaces.
Based on concept explained by Rick Rothstein:
http://www.excelfox.com/forum/showthread.php/333-Get-Field-from-Delimited-Text-String
Sorry for being necroposter!
Right(A1, Len(A1)-Find("(asterisk)",Substitute(A1, "(space)","(asterisk)",Len(A1)-Len(Substitute(A1,"(space)", "(no space)")))))
Try this. Hope it works.
Try this:
=RIGHT(TRIM(A2),LEN(TRIM(A2))-FIND(" ",TRIM(A2)))
I was able to copy/paste the formula and it worked fine.
Here is a list of Excel text functions (which worked in May 2011, and but is subject to being broken the next time Microsoft changes their website). :-(
You can use a multiple-stage-nested IF() functions to handle middle names or initials, titles, etc. if you expect them. Excel formulas do not support looping, so there are some limits to what you can do.
RIGHT return whatever number of characters in the second parameter from the right of the first parameter. So, you want the total length of your column A - subtract the index. which is therefore:
=RIGHT(A2, LEN(A2)-FIND(" ", A2, 1))
And you should consider using TRIM(A2) everywhere it appears...
Try this:
Right(RC[-1],Len(RC[-1])-InStrRev(RC[-1]," "))
There is a code function that returns ASCII for every letter.
I would like to use it in a way that it will break up a cell a1 = "some string"
Into it's ASCII codes:
Something like this: "23423423434634"
Sorry I don't know the exact ASCII of that but you get my point.
please note that i would like to do this specifically with a formula and NOT with VBA
One way is to use a Byte Array to give Unicode number pairs for each character:
Sub ByteArray()
Dim aByte() As Byte
Dim str1 As String<
Dim j As Long
str1 = "ABC"
aByte = str1
For j = LBound(aByte) To UBound(aByte)
MsgBox aByte(j)
Next j
End Sub
Write an Excel User Defined Function.
Pseudocode for the function is below:
string returnValue;
for each ( char c in string)
returnValue = returnValue + Chr(char)
return returnValue
You can call the UDF as part of an excell formula eg
=StringToASCIICodeValues(A1)
Formula Version
You could do it manually with excel builtin formula's by (excel doesn't have a for-loop function for formulae)
A1="some string"
A2="=MID($A$1,COLUMN(A2),1)"
Drag the formula in cell A2 to the right. Drag to K2 for some string example.
A3="=CODE(A2)"
Drag the formula in cell A3 to the right. Drag to K3 for some string example.
A4="=A3"
B4="=CONCATENATE(A4,B3)"
Drag the formula in cell B4 to the right. Drag to Cell K4 for some string example.
The right most column with a value on row 4 contains the final value. For some string it will return: 11511110910132115116114105110103 in cell K4
string someText = "some string";
CharEnumerator ce = someText.GetEnumerator();
int counter = 0;
while (ce.MoveNext())
{
char letter = someText[counter];
//Call the function to get the ascii
GetAsciiValue(letter);
//Do something
counter++;
}
May be this might help you.
Regards, J'Sinh
Although it's ugly, if your strings are not too long you can do it with a long formula which converts each character and then joins them back together.
For example this formula can handle strings which are up to 20 characters in length (creating hex ascii codes):
=if(len(A1)>=1,DEC2HEX(CODE(MID(A1,1,1))),"")&
if(len(A1)>=2,DEC2HEX(CODE(MID(A1,2,1))),"")&
if(len(A1)>=3,DEC2HEX(CODE(MID(A1,3,1))),"")&
if(len(A1)>=4,DEC2HEX(CODE(MID(A1,4,1))),"")&
if(len(A1)>=5,DEC2HEX(CODE(MID(A1,5,1))),"")&
if(len(A1)>=6,DEC2HEX(CODE(MID(A1,6,1))),"")&
if(len(A1)>=7,DEC2HEX(CODE(MID(A1,7,1))),"")&
if(len(A1)>=8,DEC2HEX(CODE(MID(A1,8,1))),"")&
if(len(A1)>=9,DEC2HEX(CODE(MID(A1,9,1))),"")&
if(len(A1)>=10,DEC2HEX(CODE(MID(A1,10,1))),"")&
if(len(A1)>=11,DEC2HEX(CODE(MID(A1,11,1))),"")&
if(len(A1)>=12,DEC2HEX(CODE(MID(A1,12,1))),"")&
if(len(A1)>=13,DEC2HEX(CODE(MID(A1,13,1))),"")&
if(len(A1)>=14,DEC2HEX(CODE(MID(A1,14,1))),"")&
if(len(A1)>=15,DEC2HEX(CODE(MID(A1,15,1))),"")&
if(len(A1)>=16,DEC2HEX(CODE(MID(A1,16,1))),"")&
if(len(A1)>=17,DEC2HEX(CODE(MID(A1,17,1))),"")&
if(len(A1)>=18,DEC2HEX(CODE(MID(A1,18,1))),"")&
if(len(A1)>=19,DEC2HEX(CODE(MID(A1,19,1))),"")&
if(len(A1)>=20,DEC2HEX(CODE(MID(A1,20,1))),"")
For longer strings you could repeat this pattern, although you'd be limited by the maximum formula length allowed by Excel, which seems to be 8,192 characters.