I want to remove "-" and "/" from 011-2729729 011/2729729 these numbers and convert them in to 0112729729 in excel. I tried with substitute function but i could not get the correct answer.
Already attempted formula: =SUBSTITUTE(A1,"/"," ",4)
If you truly desire a formula over a macro use this, else Gary's Student provided a nice macro.
If you are specifically searching position 4 then use REPLACE not SUBSTITUTE with a simple IF check at the start to see if position 4 is a "/" or "-"
=IF(OR(MID(A1,4,1)="/",MID(A1,4,1)="-"),REPLACE(A1,4,1,""),A1)
Notes:
SUBSTITUTE is great when you want to replace certain text with other text
REPLACE is great when you want to replace a certain position with other text
Sometimes removing "/" and "-" will create a string that Excel will treat as a number and you can lose leading zeros. This small macro will fix the cells "in place"
Sub FixValues()
Dim r As Range, v As String
For Each r In ActiveSheet.UsedRange
v = r.Text
If InStr(v, "-") > 0 Or InStr(v, "/") > 0 Then
r.NumberFormat = "#"
r.Value = Replace(Replace(v, "-", ""), "/", "")
End If
Next r
End Sub
Related
I do have a cell in excel that contains Σh. Not sure how to check for it in vba. I have tried with .Value and .Text, but the check is never true.
If (tRange.Value = (ChrW(931) + "h")) Then
Exit Sub
End if
When testing (Debug) I get this result for ActiveCell.Value = Sh
(a) You have to use .Value to get the content of the cell.
(b) You should use the ampersand character (&) to concatenate strings in VBA. The plus-sign works also, but only if all operands are strings.
(c) ChrW(931) & "h" (or ChrW(931) + "h") should work. VBA is able to handle characters even if the VBA-environment cannot show them.
Seems to me that either the Sigma-character is composed with a different character, or your cell contains invisible characters like space, newline, tabs...
You can dump the content of the cell with the following code to get an idea why your If-statement fails:
Sub DumpString(s As String)
Dim i As Long
For i = 1 To Len(s)
Dim c As String
c = Mid(s, i, 1)
Debug.Print i, AscW(c), c
Next
End Sub
When you enter the following command into the immediate window, you will see output like that:
DumpString activecell.Value
1 931 S
2 104 h
This should check if cell value contains the sub-string 'Σh'
If tRange.Value Like "*" & ChrW(931) & "h*" Then
Exit Sub
End If
Another maybe simpler way for some folks
If InStr(1, tRange.Value, ChrW(931) & "h") <> 0 Then
Exit Sub
End If
You have to use an ampersand to join the two characters:
If (tRange.Value = ChrW(931) & "h") Then
Exit Sub
End if
I have a report that includes a bunch of text in one cell. The first part of the text is a product# but the length varies. The product number is separated from the other information by a space.
I'm looking to write a macro that will replace just the first space with a delimiting character. I usually use "~". This will then allow me to script a text-to-columns command that will isolate the product number in one column.
You can do this with a formula:
=LEFT(A1, FIND(" ", A1, 1)-1) & "~" & RIGHT(A1,LEN(A1) - FIND(" ", A1, 1))
Copy that down. Copy/PasteSpecial Values. Then text-to-column that result
With VBA, the following approach is possible:
Locate the first empty string position and write it to a variable
Take the left part of the string to the position and append the replacement string
Take the right part of the string from the position to the end and append the rest
This is the function:
Public Function ReplaceFirstSpace(myInput As String, _
Optional replacement As String = "~") As String
Dim position As Long
position = InStr(1, myInput, " ")
If position = 0 Then
ReplaceFirstSpace = myInput
Else
ReplaceFirstSpace = Left(myInput, position - 1) & _
replacement & Right(myInput, Len(myInput) - position)
End If
End Function
And some tests:
Sub TestMe()
Debug.Print ReplaceFirstSpace("my name is")
Debug.Print ReplaceFirstSpace("slim shaddy")
Debug.Print ReplaceFirstSpace("tikitiki")
Debug.Print ReplaceFirstSpace(" taram")
Debug.Print ReplaceFirstSpace("tam ")
Debug.Print ReplaceFirstSpace("")
End Sub
Use REPLACE:
=REPLACE(A1,FIND(" ",A1),1,"~")
I have two columns with data. The first one has some terms and the other one contains single words.
what I have
I'm looking for a way to identify which words from each cell from the first column appear in the second, so the result should look something like this (I don't need the commas):
what I need
My question is somehow similar to Excel find cells from range where search value is within the cell but not exactly, because I need to identify which words are appearing in the second column and there can be more than one word.
I also tried =INDEX($D$2:$D$7;MATCH(1=1;INDEX(ISNUMBER(SEARCH($D$2:$D$7;A2));0);))
but it also returns only one word.
If you are willing to use VBA, then you can define a user defined function:
Public Function SearchForWords(strTerm As String, rngWords As Range) As String
Dim cstrDelimiter As String: cstrDelimiter = Chr(1) ' A rarely used character
strTerm = cstrDelimiter & Replace(strTerm, " ", cstrDelimiter) & cstrDelimiter ' replace any other possible delimiter here
SearchForWords = vbNullString
Dim varWords As Variant: varWords = rngWords.Value
Dim i As Long: For i = LBound(varWords, 1) To UBound(varWords, 1)
Dim j As Long: For j = LBound(varWords, 2) To UBound(varWords, 2)
If InStr(1, strTerm, cstrDelimiter & varWords(i, j) & cstrDelimiter) <> 0 Then
SearchForWords = SearchForWords & varWords(i, j) & ", "
End If
Next j
Next i
Dim iLeft As Long: iLeft = Len(SearchForWords) - 2
If 0 < iLeft Then
SearchForWords = Left(SearchForWords, Len(SearchForWords) - 2)
End If
End Function
And you can use it from the Excel table like this:
=SearchForWords(A2;$D$2:$D$7)
I have a partial solution:
=IF(1-ISERROR(SEARCH(" "&D2:D7&" "," "&A2&" ")),D2:D7&", ","")
This formula returns an array of the words contained in the cell (ranges are according to your picture). This array is sparse: it contains empty strings for each missing word. And it assumes that words are always separated by one space (this may be improved if necessary).
However, native Excel functions are not capable of concatenating an array, so I think the rest is not possible with native formulas only.
You would need VBA but if you use VBA you should not bother with the first part at all, since you can do anything.
You can create a table with the words you want to find across the top and use a formula populate the cells below each word if it's found. See screenshot.
[edit] I've noticed that it's incorrectly picking up "board" in "blackboard" but that should be easily fixed.
=IFERROR(IF(FIND(C$1,$A2,1)>0,C$1 & ", "),"")
Simply concatinate the results
=CONCATENATE(C2,D2,E2,F2,G2,H2)
or
=LEFT(CONCATENATE(C2,D2,E2,F2,G2,H2),LEN(CONCATENATE(C2,D2,E2,F2,G2,H2))-2)
to take off the last comma and space
I've edited this to fix the problem with "blackboard"
new formula for C2
=IF(OR(C$1=$A2,ISNUMBER(SEARCH(" "&C$1&" ",$A2,1)),C$1 & " "=LEFT($A2,LEN(C$1)+1)," " & C$1=RIGHT($A2,LEN(C$1)+1)),C$1 & ", ","")
New formula for B2 to catch the error if there are no words
=IFERROR(LEFT(CONCATENATE(C2,D2,E2,F2,G2,H2,I2),LEN(CONCATENATE(C2,D2,E2,F2,G2,H2,I2))-2),"")
I have a one column filled with values like 3 days, 6 days, etc.
How can I strip out the text and force convert these to integers?
If you want to convert them "in-place", then select the cells and run this little macro:
Sub fixData()
Dim r As Range, v As String, i As Long
For Each r In Intersect(Selection, ActiveSheet.UsedRange)
v = r.Text
i = InStr(1, v, " ")
If i <> 0 Then
r.Value = Mid(v, 1, i - 1)
End If
Next r
End Sub
You could try substitute on a column next to you data:
=substitute(A1," days","") +0
Substitute replaces the string part " data" with nothing (zls "") and adding zero returns a number. This assumes your data is in col A. Drag down the formula and voila
If you don't mind changing the data you could also just select it all hit ctrl+F choose find and replace find field is " days", leave replace with blank and hit "Replace all"
If its VBA, you could actually use the Val function to do this.
If A1 has the data and you want this integer value in A2
So something like:
Range("A2").value = Val(Range("A1").value)
The returned value is actually a double. If a double isn't good enough,
Range("A2").value = CInt(Val(Range("A1").value))
Hope that helps.
I am trying to extract numbers from a text.
If I have an entry like 12&6&2014, how can I extract the 12 (the number that is before the first &) and 2014 (the number that occurs after the second &)?
To get first number:
=LEFT(A1, FIND("&", A1)-1)
To get last number after the second &:
=RIGHT(A1, 4)
Otherwise, if that's not always a year:
=MID(A1, FIND(CHAR(1), SUBSTITUTE(A1, "&", CHAR(1), 2))+1, LEN(A1))
you can loop through each character int he string and check to see if it is numeric
Sub getNumberValues()
Dim s As String
Dim c As New Collection
Dim sNewString As String
s = "12&6&2014"
For v = 1 To Len(s)
If IsNumeric(Mid(s, v, 1)) Then
sNewString = sNewString & Mid(s, v, 1)
Else
c.Add sNewString
sNewString = ""
End If
Next v
'add the last entry
c.Add sNewString
sNewString = ""
For Each x In c
sNewString = sNewString & x & Chr(13)
Next
MsgBox sNewString
End Sub
If what I understand is correct which is that the characters that separate vary as well as how many digits are used you might look into something like this:
Function CleanUp(Txt)
For x = 1 To 255
Select Case x
Case 45, 47, 65 To 90, 95, 97 To 122
Txt = WorksheetFunction.Substitute(Txt, Chr(x), "") <- "" can be replaced
End Select with "&" to do a
Next x MID() using & as
the delimiter
CleanUp = Txt
End Function
If you can use VBA, this will replace your characters with a blank, but you could put in your own character and then use your formulas to separate from a specific delimiter.
The original code can be found here:
http://www.mrexcel.com/forum/excel-questions/380531-extract-only-numbers.html
Simplest might be Text to Columns with & as the delimiter, then delete the middle column. This overwrites the original data, so a copy might be appropriate.
Another simple way would be to create two copies, and for one Find what: &* and Replace with: nothing, for the other Find what: *& and Replace with: nothing.
An alternative formula solution might be:
=DAY(SUBSTITUTE($A1,"&","/"))
and
=YEAR(SUBSTITUTE($A1,"&","/"))