How do I convert "3 days" to "3" in Excel? - excel

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.

Related

How to check the value of Σ character in an Excel Cell? - VBA

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

Check if a string (word) is a subset of another string (word) independent of sequence

I want to check in a string if it is a subset of another string. But not in sequence.
Ie = "ABC" in "BAC" = true.
What I have done so far is sort both strings alphabetically (once off exercise done in vba), then check with "find(a,b)" but this does not quite work:
"hour" is then not a subset of "dffhioru" because i find for "hour" but the letter sequence is hioru whilst hour is horu when sorted.
I ideally want to not use vba here for speed purposes, but if there is not such a function then so be it.
You can do this without VBA by finding each letter in separate columns and then joining them if the string length matches the original string.
Find formula in cell E1 then autofill to O?
=IFERROR(IF(FIND(MID($B2,E$1,1),$A2,1) > 0,MID($B2,E$1,1),""),"")
Concatinate formula in C2 and autofill down to the end of your word list.
=IF(COUNTA(E2:O2)-COUNTBLANK(E2:O2)=LEN(A2),CONCATENATE(E2,F2,G2,H2,I2,J2,K2,L2,M2,N2,O2),"")
Have a look at this:
Sub CheckString()
Dim myChar As String
Dim i As Long
Range("C1").Value = ""
For i = 1 To Len(Range("A1"))
myChar = Mid(Range("A1").Text, i, 1)
If InStr(1, Range("B1").Text, myChar, vbTextCompare) = 0 Then
Exit Sub
End If
Next i
Range("C1").Value = "OK"
End Sub
Basically what it does is loop trough every char in first string (here in cell A1) and checks if it's in the second string (cell B1). If a char is not found the code stops, if all of the chars are found it puts "OK" into cell C1. This should get you going.
EDIT: To address Storax's point I have changed the code a little.
First it checks the length of both strings - if the first one is longer automatically it's not a subset. If it's not the case it stores the second string in memory as myText and takes out the chars one by one. And now if all the chars can be taken out it meant the first string is a subset and the code puts "OK" into cell C1.
Sub CheckString()
Dim myChar, myText As String
Dim i, charPos As Long
Range("C1").Value = ""
myText = Range("B1").Text
If Len(Range("A1").Text) > Len(Range("B1").Text) Then Exit Sub
For i = 1 To Len(Range("A1"))
myChar = Mid(Range("A1").Text, i, 1)
charPos = InStr(1, myText, myChar, vbTextCompare)
If charPos = 0 Then
Exit Sub
Else
myText = Left(myText, charPos - 1) & Mid(myText, charPos + 1, Len(myText))
End If
Next i
Range("C1").Value = "OK"
End Sub

Excel cell custom format and text

I want to change the following
123456789A1
to
123-456-789 A1
Background:
In Format Cells, I used this:
000-0000-00 00
And that works if everything in the cell is a number,
12345678911
will become
123-4567-89 11
But as soon there is a letter, it breaks it.
How can I change the type to ignore letters?
this is an example :
Sub test()
a = [A1]
b = Left(a, 3) & "-" & Mid(a, 4, 3) & "-" & Mid(a, 7, 3) & " " & Right(a, 2)
[A2] = b
End Sub
But want you 3 or 4 numbers in the seconde position?
if A1=123456789A1 then in A2 : 123-456-789 A1
Cell number formatting only works on numbers.
If you don't mind changing the values into text strings, you could format them in VBA using the Format function. For example:
Option Explicit
Sub FMT()
Dim R As Range, C As Range
Const sFMT As String = "###-####-## ##"
Set R = Intersect(Range("A:A"), ActiveSheet.UsedRange)
For Each C In R
C.Value = Format(C.Text, sFMT)
Next C
End Sub
You will need to change the range arguments to match what you need. And you could also use this in an event-triggered macro to do it automatically.

copy last value of a cell into a cell of another column

In an Excel column, either there is one value (alphanumerical) that I should copy to the cell of the same row but in another column or there are more than one value separated by space and I should copy the last one in to the other column. The values have the same format, digits 5 or 6 followed by a character and three digits (8 or 9 chars). How can I copy correctly if there is one value in the other column and if there are more than one value the last one on 8 or 9 characters?
Thanks
Carol
(updated after additional input and chat)
In one shot!
=IF( AND(ISERR(FIND(CHAR(10), A1)), ISERR(FIND(CHAR(32), A1))), A1,
MID(A1, MAX(IFERROR(FIND("#",SUBSTITUTE(A1,CHAR(10),"#",LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),"")))),0),IFERROR(FIND("#",SUBSTITUTE(A1,CHAR(32),"#",LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(32),"")))),0) )+1, LEN(A1)- MAX(IFERROR(FIND("#",SUBSTITUTE(A1,CHAR(10),"#",LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(10),"")))),0),IFERROR(FIND("#",SUBSTITUTE(A1,CHAR(32),"#",LEN(A1)-LEN(SUBSTITUTE(A1,CHAR(32),"")))),0) ) )
)
In words...
if no line break or space is found then take the entire cell text,
otherwise
find the last occurrence of a linebreak (char 10) or space (char 32) and take the text from that index
You can also use this formula.
=IF(ISNUMBER(SEARCH(" ",A1)),TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",LEN(A1))),LEN(A1))),A1)
Use following VBA sub to copy to another column.
Public Sub CopyAnotherColumn()
Dim LastUsedCell As Long
Dim stCopyItIt As String
LastUsedCell = Range("A1").End(xlDown).Row
For Each rCell In Range("A1:A" & LastUsedCell)
If InStr(1, rCell, " ", vbTextCompare) > 0 Then
stCopyItIt = StrReverse(rCell.Value)
stCopyItIt = Left(stCopyItIt, InStr(1, stCopyItIt, " ", vbTextCompare))
ReturnLastWord = StrReverse(Trim(stCopyItIt))
rCell.Offset(0, 2).Value = ReturnLastWord
Else
rCell.Offset(0, 2).Value = rCell.Value
End If
Next
End Sub

Append string between each cell value in a series

Let's say I have a series of cells like so:
A
1 Foo
2 Bar
3 Hello
4 World
5 Random Text
What I'd like to do is have the result of my formula populate another cell with:
Foo, Bar, Hello, World, Random Text
Now, I know how to concatenate two cells with:
=A1&", "&A2
but how can I do the same thing with the entire series?
Here's a function you might be able to use. Simply put this in your workbook code module, then you can enter it in cells like:
=JoinRange(A1:A6) or =JoinRange(A2:D15), etc.
Public Function JoinRange(ByVal rng As Range) As String
Dim dlmt As String: dlmt = ","
Dim multiRow As Boolean: multiRow = rng.Rows.Count > 1
Dim r As Long, c As Long
Select Case rng.Columns.Count
Case 1
If multiRow Then
JoinRange = Join(Application.WorksheetFunction.Transpose(rng), dlmt)
Else:
'a single cell
JoinRange = rng
End If
Case Is > 1
If multiRow Then
'a 2d range of cells:
For r = 1 To rng.Rows.Count
For c = 1 To rng.Columns.Count
JoinRange = JoinRange & rng(r, c) & dlmt
Next
Next
JoinRange = Left(JoinRange, Len(JoinRange) - 1)
Else:
JoinRange = Join(Application.WorksheetFunction.Transpose( _
Application.WorksheetFunction.Transpose(rng)), dlmt)
End If
Case Else
End Select
End Function
Put a comma and a space in cell B1, then use this formula:
=CONCATENATE(A1,B1,A2,B1,A3,B1,A4, B1, A5)
There are several answers to the following question that you can try as well, including VBA options and a formula:
Need to concatenate varying number of cells...
With =A1 in B1 then =B1&", "&A2 in B2 and copied down would seem to work.

Resources