I have a column with data:
New York is really nice, 456983 Good food
Beijing is awesome 678932, Nice culture.
... and so on
I want to extract the six digit numbers and place it in a column beside the original column.
There are n rows.
Is there any way to do this with a formula, without VBA?
Without VBA, you can use the following Excel formula assuming A1 is the cell containing the text.
=MID(A1,FIND("------",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"0","-"),"1","-"),"2","-"),"3","-"),"4","-"),"5","-"),"6","-"),"7","-"),"8","-"),"9","-")),6)
This formula works for both of your examples:
=MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&1234567890)),6)
You can use this, enter as an array formula:
=MID(A2,MIN(IFERROR(FIND({0,1,2,3,4,5,6,7,8,9},A2),"")),SUMPRODUCT(--ISNUMBER(MID(A2,ROW($A$1:$A$200),1)+0)))
This works for me. This will look for all digits in the string. - You can of course add limitations.
Sub numberExtract()
x = ActiveCell
Dim valIs As String
Dim a As String
For i = 1 To Len(x)
a = Mid(x, i, 1)
If IsNumeric(a) Then
valIs = valIs & a
End If
Next i
MsgBox valIs
End Sub
Related
I am new to Excel VBA and what I want to do is the following.
Currently the cell "F3" is active, and I would like to know if there is a function
that returns the value "F3".
I imagine that there is a way to extract the 'coordinate' of F3 as (6,3) since F is the 6th letter and 3 is simply 3 (maybe (3,6) if it counts row first and columns later).
If it can return something like (F,3) it would be even better, and best case scenario, F (the column LETTER) by itself.
It is imperative that I am able to extract the column letters because I intend to use this as an input to "replace all letter A to letter B".
If you have an exact answer that would be great, but if you could guide me towards a useful link that would be appreciated, too.
Thank you!!
Try below sub-
Sub GetCellAddress()
Dim x As Variant
x = Split(ActiveCell.Address, "$")
MsgBox x(1) & "," & x(2)
End Sub
Result in message box: F,3
Looking for some suggestions to tweak/enhance the formula I have created, to extract the number from a string.
Have the below sample text in a Cell A1:
Based on the invnum:-1234567 The calculation is based on 123.33*3.00
Wrote the below formula in B1
=VALUE(LEFT(MID(A1,FIND("invnum:-",A1)+7,LEN((A1))),7)
the Result given is -1234567
However, the length of the reference number on my source file is variable, Looking to extract only the number following the word invnum:-
Looking to include this formula in a macro, so trying to keep it simple any ideas/suggestions please?
Try this Function:
Function findNumber(inPtStr As String) As Double
Dim strArr() As String
Dim i As Long
inPtStr = Replace(inPtStr, ":", " ")
strArr = Split(inPtStr)
For i = LBound(strArr) To UBound(strArr)
If IsNumeric(strArr(i)) Then
findNumber = --strArr(i)
Exit Function
End If
Next i
End Function
Then you can call it from a regular sub.
If you want a formula then:
=--TRIM(MID(SUBSTITUTE(A1," ",REPT(" ",99)),FIND("invnum:",SUBSTITUTE(A1," ",REPT(" ",99)))+7,99))
Let's get crazy and step through this...
Start with the intermediate indexes you need to find, such as the start, end, and length of the numeric string. Once you have those, just carefully reassemble your formula:
I am trying to perform the following in one steps (one formula):
Strip a letter from a column of elements and add them up.
Example:
Data:
1x
2y
3x
I want to strip letters and add up numbers all in one formula.
I understand that I could have a helper column in which I strip letters x,y,z and then have a formula to add up the numbers, but I don't want to do this.
Thanks for any suggestions.
Assuming one entry per cell:
Is there only one letter at the end? If so, you can use:
=SUMPRODUCT(--LEFT(A1:A100,LEN(A1:A100)-1))
If there might be multiple letters at the end, a simple UDF would be simpler:
Option Explicit
Function AddStrings(rg As Range)
Dim L As Long
Dim I As Long
For I = 1 To rg.Count
L = L + Val(rg(I))
Next I
AddStrings = L
End Function
EDIT: If some of the cells might be blank, you can use either the UDF, or, if you prefer the formula, this array-entered formula:
=SUM(IFERROR(--LEFT(A1:A100,LEN(A1:A100)-1),0))
To array-enter a formula, after entering
the formula into the cell or formula bar, hold down
ctrl-shift while hitting enter. If you did this
correctly, Excel will place braces {...} around the formula.
Assuming that the format is consistent, you can do something like
=VALUE(LEFT(A1,1))+VALUE(MID(A1,4,1))+VALUE(MID(A1,7,1))
If the format is not consistent, things get more difficult. Let me know and I will expand the answer.
EDIT:
This function works with a variable length text, assuming that the fields are separated by the spaces and have one letter after the number:
Function AddValues(Text As String)
Dim Tokens() As String, I As Integer
Tokens = Split(Text)
For I = 0 To UBound(Tokens)
AddValues = AddValues + Val(Left(Tokens(I), Len(Tokens(I)) - 1))
Next I
End Function
In a VB macro for Excel, how do I remove all occurances of a word that starts with a certain string?
Eg:
The string reads: xxxx $AUD543.43 yyyy
I want to search for anything in a string that starts with $AUD and remove the whole word before the next space
So the example above should result in:
xxxx yyyy
use Regex. Add a reference to Microsoft VBScript Regular Expressions in VBA >> Tools >> Options.
Dim txt As String
txt = "$Audthisfew is$Aud $Auda test $Aud"
Set regEx = New RegExp
With regEx
.Global = True
.Pattern = "((^, )\$Aud)"
Debug.Print .Replace(txt, "")
End With
It would be remiss of me not to remind you of the expectation that you show "what you (have) tried". (Which I do to avoid me being yelled at for answering the question.) My duty thus having been done, I now move on to it.
You actually don't necessarily need VBA code to do this; you could do it with the Find() function albeit more clumsily and I wouldn't recommend it for a really large sheet. Still, VBA code you have specified, and that you shall have. Change the range to match the one that you'll be searching. ALSO, you should note that you have only one space between the x's and y's in your example but that varies from your request that it be the word starting with $AUD and ending BEFORE the next space. If you want only one space, please adjust the formulas accordingly.
Sub ReplaceText()
Dim rng As Excel.Range
Dim s_Contents As String
Dim l_FindAUD As Long, l_FindSpace As Long
For Each rng In ActiveSheet.UsedRange
s_Contents = rng.Value
'Does the $AUD expression exist in this cell?
l_FindAUD = InStr(1, s_Contents, "$AUD", vbTextCompare)
If l_FindAUD > 0 Then
'If so, is it followed by a space?
l_FindSpace = InStr(l_FindAUD, s_Contents, " ")
If l_FindSpace > 0 Then
'If so, take all of the content up to but not including the $
'and all of the contents from the space onwards, merge them
'together and write to the cell.
s_Contents = Left$(s_Contents, l_FindAUD - 1) & Mid$(s_Contents, l_FindSpace)
rng.Value = s_Contents
End If
End If
Next
End Sub
Although not exactly what you ask for, but you could also use an Excel formula to achieve this. Assuming your text is in A1, the formula would be:
=TRIM(LEFT(A1,FIND("$AUD",A1)-1))&RIGHT(A1,LEN(A1)-FIND(" ",A1,FIND("$AUD",A1))+1)
My example is used for delete the last few words of a cell
For example:
in cell A1: ABCDE[Acct:12345]
in cell A2: FGHIJ[Acct:67890]
in cell A3: KLMNO
Wanna delete all the words begin with "[Acct:", note not every cell includes "[Acct:"
Now in column B, insert the below function, this will return with the words on the left on "[Acct:"
=LEFT(A1,FIND("[Acct:",A1)-1)
Result: A B
ABCDE[Acct:12345] ABCDE
FGHIJ[Acct:67890] FGHIJ
KLMNO #VALUE!
Now in column C, insert below function, this will check whether column B is #VALUE! and then return with what we want
=IF(ISERROR(C1)=TRUE,A1,B1))
Result:
A B C
ABCDE[Acct:12345] ABCDE ABCDE
FGHIJ[Acct:67890] FGHIJ FGHIJ
KLMNO #VALUE! KLMNO
Then you can copy the three columns and paste with value, and delete column A and B , column C will be the final result you want.
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]," "))