Do math on cells also contaning text in EXCEL - excel

I have a range of cells which I want to do some math on. But I also want those cells to contain some text.
For instance I want the sum of A1 and B1 where A1 contains the number 10 and "z001" and B1 contains the number 20 and "Z004".
Then I want the formula to ignore the text, and just come up with 30.
Is this possible?

For a quick solution, type "=Left(A1, 2) + Left(B1, 2)" into C1. Drag this equation down the rest of your range and you should get the results you want, provided the numbers you are adding are all 2 digits.
You can also use VBA if you need to run the same equation on multiple cells.
If you can get the same results by just removing the letters, try:
For i = 58 To 127
'Change out str with the variable name you have assigned to your cell value.
str = Replace(str, Chr(i), "")
Next i
58 and 127 represent the first and last positions in a range of characters on the Ascii table that are not numerals http://www.asciitable.com/
If you just want to include the first two numbers of each cell in your equation and ignore the "Z00#", you can try:
strLeft = Left(str, 2)
This will reduce your string down to the first two characters of each cell.
You can look here for other ways to remove characters you don't want.
http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=269:excel-vba-string-functions-left-right-mid-len-replace-instr-instrrev&catid=79&Itemid=475
Here is in example of how you would implement something like this with simple addition.
Dim a as range
Dim b as range
Dim aLeft as integer
Dim bLeft as integer
Dim cleft as integer
a = Worksheets("WorksheetName").Cells(A1).Value
b = Worksheets("WorksheetName").Cells(B1).Value
aLeft = Left(a, 2)
bLeft = Left(b, 2)
cLeft = aLeft + bLeft
Worksheets("WorksheetName").Cells(C1).Value = cLeft
This would add the first two digits of cells A1 and B1 then display the result in C1.

As I see it, you have 2 options:
Search for the number within each cell and sum it up (see below).
Split the columns to have 1 column of numbers and one of codes (e.g.
"z001"). See on the "Data" tab on the ribbon and click "Text to
Columns" on the Data Tools group.
The first option would be the quickest and more straightforward. You need to make a third column where the sum will be and then use, for example, the function LEFT. This function allows you to retrieve characters from a cell. See example below:
To get "30" I have used the following formula on C2:
=LEFT(A2,2)+LEFT(B2,2)
Note this is not ideal since this formula is looking for 2 characters every time. If you have a scenario with the following code "5z005" it won't work because it will try to sum "5z" as if it was a number. In that case you're better off finding a pattern (code = "number" "z" "number") and splitting the columns as I said on option 2.

Related

Pattern match and sum values in VBA

I have a spreadsheet where users enter their three character name code corresponding to everyday with number of hours that they plan to spend in a specific task (A, B, C) within brackets. What I want is these needs to be summed up for every user and get populated in rows corresponding to their names everyday.
Currently I am using an custom vba function to perform this sum, however I wanted to know if this can be done directly with any custom formula or VLOOKUP. Any help is appreciated!
Edit: I have been using the function, one specific to each user (the three character code) to calculate this sum.
Function SumJON(Target As Range) As Double
Dim xCell As Range
Dim xSum As Double
xSum = 0
For Each xCell In Target
If xCell.Value <> "" And InStr(xCell.Value, "JON") > 0 Then
Test = Split(xCell.Value, "JON")
Test1 = Test(1)
Test2 = Split(Test1, ")")
Test3 = Test2(0)
xSum = xSum + Test3
End If
Next
SumJON = xSum
End Function
You can try the following, with the below setup of data:
Formula in B2:
=SUM(IF(ISNUMBER(SEARCH(MID($A2,LEN($A2)-3,3),B$6:B$8)),MID(B$6:B$8,SEARCH(MID($A2,LEN($A2)-3,3),B$6:B$8)+4,SEARCH(")",B$6:B$8,SEARCH(MID($A2,LEN($A2)-3,3),B$6:B$8))-SEARCH(MID($A2,LEN($A2)-3,3),B$6:B$8)-4),0)*1)
NOTE: This is an array formula and should be confirmed through CtrlShiftEnter
Drag formula down and right.
It will also work on expending the amount of names in the list AND with decimals:
Note that my system decimal point is a comma.
Breakdown:
To extract the searchvalue, JON, DOE and PRK we can make use of MID since you have a certain pattern to take into account. It would always have to be:
=MID($A2,LEN($A2)-3,3)
This value needs to be used in a SEARCH function which will return a position of our searchvalue in the A,B,C rows. Since it's an array formula it will return all these numeric positions or an error.
Therefor we need to use ISNUMBER to check if the searchvalue is actually found through SEARCH. If so it will return a TRUE and if not it will return FALSE.
Because this is part of our IFstatement, the formula will do another MID function for the TRUE values. Again we will have to make use of our searchvalue formula, but this time we know it will be in these strings and therefor we can use the numerical position as our starting position (+4, because these strings are 3 positions long + the opening paranthesis) in this second MID.
Now we have a starting position to get the lookupvalues from, we just need to know the first position of the next closing paranthesis, and we can do that through SEARCH from this exact position.
Multiplying it by 1 in the end would turn these values in true numerical lookupvalues, which finally can be summed through SUM
Et voila, we got the total of values between the paranthesis where the searchcriteria meets the current user.

Get random numbers in the 3x3 table or larger

Please ads & support readers
I have a 3x3 table numbered from 1-9 as shown in the attachment.
Now how do I get the random number in this table shown in column E1?
And a little more upgrading (for myself) that is for example: E1 random cell B2 - ie 6 (according to his picture) - is always poured the same ink B2.
To actually get a random value from your table, which presumably might not always have the numbers 1 to 9 you can use this formula in E1
=INDEX($A$1:$C$3,RANDBETWEEN(1,3),RANDBETWEEN(1,3))
For differently sized tables you can use this generic formula
=INDEX(Table,RANDBETWEEN(1,ROWS(Table)),RANDBETWEEN(1,COLUMNS(Table)))
You can use conditional formatting to highlight which number was picked
Select the range A1:C3 and apply conditional formatting with this formula
=A1=$E$1
format as required
You can use the following code to act as random number generator, and print result on cell E1.
Sub random_num()
'Initialize the random number generator
Randomize
Dim wk As Worksheet
Set wk = ThisWorkbook.Sheets("x")
'Random whole number between 1 and 9 :
random_number = Int(9 * Rnd) + 1
wk.Cells(1, "E").Value = random_number
End Sub

Check id every value in ";" separated string exists on a LOV list using just Excel formulas

In a cell I have a multi value separated by semicolon like this:
Red;Blue;Green
I need to compare if each of those values exist on a list:
Black
Orange
Green
Blue
Red
I think it should be an array formula, but I have no idea how to set it.
Is it even possible?
Regards
MichaƂ
You've not mentioned what output you are looking for. Below are the two possible solution.
1. If you are looking for the count of words in a cell from the list use following formula:
=SUMPRODUCT(ISNUMBER(FIND($E$2:$E$6,$A2))*1)
2. If you want words in the cell that are in the list to be displayed in separate columns, use the following array formula
=IFERROR(INDEX($J$2:$J$6,SMALL(IF(ISNUMBER(FIND($J$2:$J$6,$A2)),ROW($J$2:$J$6)-ROW($J$1)),COLUMNS($A1:A1))),"")
Drag/copy above formula across and down as required.
Being an array formula you'll have to commit this formula by pressing Ctrl+Shift+Enter.
You can write this UDF and use it as a formula. Wasn't sure what output is required. This UDF gives number of items that match in the list.
Parameters:
myValue - the cell that contains multi value separated by semicolon
listRange - Range that has the list to check against. Should be a single column list
Function checkList(myValue As Range, listRange As Range) As Integer
Dim t As Variant
t = Split(myValue.Value, ";")
Dim c As Integer
c = 0
For i = LBound(t) To UBound(t)
For j = 1 To listRange.Rows.Count
If (t(i) = listRange(j, 1)) Then
c = c + 1
End If
Next j
Next i
checkList = c
End Function
Since you want to do this only with excel formulas, the input string has to be split to multiple cells before comparing it with the list.
If your input string is in A1, use the below formula and drag it right to split them based on the delimiter ;.
=TRIM(MID(SUBSTITUTE($A1,";",REPT(" ",999)),1+((COLUMN(A1)-1)*999),999))
Assuming your list is in column G, use the below formula which counts the strings Red, Blue and Green in your list and returns Found or Not found.
in C2,
=IF(COUNTIF($G:$G,C1),"Found","Not found")
Hope this helps.

Excel 2013: Count the occurrence of numbers/strings in a cell

I need to calculate how many time any given numbers appeared in an excel cell.
Conditions below:
Each cell can have different combination of repetitive numbers, separated by commas.
The excel formula will be robust enough to search for any numbers desired by user.
Max range up to 20.
Example, I would input any number and the formula should return me a sum of occurrence.
1, 2, 3, 1, 5, 11, 1, 11
=> 5 (input "1" and "11")
=> 3 (input "1" only)
=> 2 (input "11" only)
*****************************************************
Things i have tried:
=LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"1,","")) => Answer is 7 (wrong)
=LEN(A2)-LEN(SUBSTITUTE(UPPER(A2),"1","")) => Answer is 8 (wrong)
Main difficulty:
The formula seems to search for "1" and therefore "11" would often mistooken as "1" too, and thus added to the sum.
Same goes for "5", "15" etc, any numbers in the tenths.
My approach was to do a text search instead of numbers, that failed.
Appreciate if there are alternate solutions too (Text -> Column ??).
You need to include the delimiters front and back. This means that you also have to add them as a prefix and suffix to the original string.
=(LEN(", "&A2&", ")-LEN(SUBSTITUTE(UPPER(", "&A2&", "),", 1,", "")))/LEN(", 1,")
If VBA is feasible, add this to VBA code and use the following function (assuming your long string is in cell A2)
= getOccurence(A2,5)
VBA Code:
Function getOccurence(inputString As String, numberToSearch As Integer) As Integer
Dim strArray() As String
strArray = Split(inputString, ",")
For i = 0 To UBound(strArray)
If (strArray(i) = numberToSearch) Then
getOccurence = getOccurence + 1
End If
Next
End Function
I would use text to columns to get each number in its own cell. NOTE: you may want to insert blank columns to hold the numbers first so that you do not overwrite any data!
After this, I would use the function COUNTIF to count the number of occurrences of a certain number there is within a range. For example:
=COUNTIF(F5:I5,2)+COUNTIF(F5:I5,3)
This counts the number of 2's and 3's within the range F5:I5
For more on this, see:
https://exceljet.net/excel-functions/excel-countif-function

Splitting text to columns in Excel

I have a column in Excel that consists of data in the following format: "NAME OF BAND Album Title". I'd like to split this cell into two--one for the all-caps band name and another for Album Title. Below are a few examples from the data:
Column A
ABSORBED Demo '98
ABSTRACT CELL THEORY Act
ABSTRACT SATAN Elite 7512
ABSTRACT SATAN Aryan Blitzkrieg Union
ABSTRACT SATAN Satanic Blood Circle
ABSTRACT SHADOWS Symphony of Hakel
Splitting by space doesn't work since bands have varying numbers of words in their name. Any help would be appreciated.
Here is a formula solution. No VBA required.
Assuming your list starts in cell A1, enter the following formula in cell B1:
=LEFT(A1,MATCH(,--(CODE(MID(A1,ROW(OFFSET($A$1,,,LEN(A1))),1))<96),)-3)
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
And then in cell C1, enter this:
=MID(A1,MATCH(,--(CODE(MID(A1,ROW(OFFSET($A$1,,,LEN(A1))),1))<96),)-1,99)
This is an array formula and must be confirmed with Ctrl+Shift+Enter.
Now select the range B1:C1 and copy downward as far as needed.
.
Here is how they work. We'll discuss the first formula.
The MID function splits the value of cell A1 into individual characters. The CODE function returns the ASCII code number for each char. We test each code number to see if it is less than 96, which is "a" the first lower case char.
This gives us an array of Boolean values (TRUE or FALSE), one Booelan value for each char in cell A1.
We convert the Booleans to ONES and ZEROES by the double unary (--).
We search the array for the location of the first ZERO by using the MATCH function.
The end of the contiguous upper case letters is three char locations prior to the location returned by MATCH.
That's it.
What makes all of that possible is the array at the heart of the formula produced by the ROW/OFFSET combination. In conjunction with the LEN function, this combo produces a vector array that looks something like {1;2;3;4;5;6;7;8;9;10;11}. That array's last and largest number is equal to the length of the value in cell A1.
.
UPDATE
Here is a sample workbook showing these formulas work on the question's example data: http://www.excelhero.com/samples/torentino_excelhero.xlsx
Something like this might split off the band names from the albums.
Sub splt()
Dim rw As Long, p As Long, r As Long, sp As Long, v As Long
Dim bnd As String, ttl As String, tmp As Variant
With Worksheets("Sheet1")
For rw = 2 To .Cells(Rows.Count, 1).End(xlUp).Row
p = 1
bnd = vbNullString
ttl = Trim(.Cells(rw, 1).Value2) & Chr(32)
tmp = Split(ttl)
For v = LBound(tmp) To UBound(tmp)
If UCase(tmp(v)) = tmp(v) Then
bnd = Join(Array(bnd, tmp(v)), Chr(32))
Else
Exit For
End If
Next v
.Cells(rw, 2) = bnd
.Cells(rw, 3) = Trim(Right(ttl, Len(ttl) - Len(bnd)))
Next rw
End With
End Sub

Resources