SUMPRODUCT if cells within range contains a string - excel

Is it possible to have SUMPRODUCT to sum values "if cells within range contain a certain piece of string"?
PS: it has to be a string since letters are actually names and words from the actual report.
Maybe a combination between SUMPRODUCT and SEARCH, but I couldn't get it to work.

With only one column use SUMIF() with a wildcard:
=SUMIF(A:A,"*" & D2 & "*",B:B)
If you are worried about false positive as #YowE3k stated:
a SUMIF(A:A,"*BERT*",B:B) will count the values for Bert, Robert, and Roberta.
You will need to limit the reference to only the data set and use SUMPRODUCT:
=SUMPRODUCT(ISNUMBER(SEARCH(" " & D2 & " "," " & $A$2:$A$6 & " "))*$B$2:$B$6)

Related

Vlookup for different cells location

I'm trying to do Vlookup for target data in different rows.
But unfortunately, I can't find the way to do it.
Vlookup, Hlookup can't get the target data correctly.
Can you advise?
Resource Data Sheet
Result Output (expectation)
Matching ID number consist of these target data.
Dim A As String
Dim B As String
Dim C As String
Dim D As String
Dim E As String
Dim F As String
A = Application.WorksheetFunction.Index(iSheet.Range("G1"), Application.WorksheetFunction.Match(iSheet.Range("A11"), iSheet.Range("A3:A5"), 0))
B = Application.WorksheetFunction.Index(iSheet.Range("I1"), Application.WorksheetFunction.Match(iSheet.Range("A11"), iSheet.Range("A3:A5"), 0))
C = Application.WorksheetFunction.Index(iSheet.Range("K1"), Application.WorksheetFunction.Match(iSheet.Range("A11"), iSheet.Range("A3:A5"), 0))
'*** Error Thrown On Next Line (unable to get the index property of the worksheetfunction class)
D = Application.WorksheetFunction.Index(iSheet.Range("G1"), Application.WorksheetFunction.Match(iSheet.Range("A12"), iSheet.Range("A3:A5"), 0))
E = Application.WorksheetFunction.Index(iSheet.Range("I1"), Application.WorksheetFunction.Match(iSheet.Range("A12"), iSheet.Range("A3:A5"), 0))
F = Application.WorksheetFunction.Index(iSheet.Range("K1"), Application.WorksheetFunction.Match(iSheet.Range("A12"), iSheet.Range("A3:A5"), 0))
iSheet.Range("C7").Value = A & ", " & B & ", " & C
iSheet.Range("C8").Value = D & ", " & E & ", " & F
You're on the wrong track with VLookup() and HLookup().
Try using this: (on multiple lines for readability purposes)
=TEXTJOIN(",",
TRUE,
IF(NOT(ISBLANK(I3)),"POWER", ""),
IF(NOT(ISBLANK(K3)),"MB", ""),
IF(NOT(ISBLANK(M3)),"ANT", "")
)
Let me explain:
TextJoin() concatenates information, based on:
"," : the delimiter to be used
TRUE: how to handle empty cells
the arguments to be concatenated
IF(NOT(ISBLANK(I3)),"POWER", ""): if I3 is filled in, you need to put the word "POWER". If not, put an empty string.
Are you looking for something like this?
Right... so, this will technically do what you have asked for. But I suspect that it may not be exactly what you actually want. I am afraid your original question was quite unclear and I have had to make some guesses/assumptions.
=CONCAT(
IF(INDEX($H$3:$H$5,MATCH($A11,$A$3:$A$5,0))<>"",$G$1 & ", ",""),
IF(INDEX($J$3:$J$5,MATCH($A11,$A$3:$A$5,0))<>"",$I$1 & ", ",""),
IF(INDEX($L$3:$L$5,MATCH($A11,$A$3:$A$5,0))<>"",$K$1 & ", ","")
)
The CONCAT function just adds strings together, one after the other.
Within it, we have one formula for each of your merged column headers.
Please note: If you have lots of column headers, this probably won't be a viable solution. However, making a formula to handle a dynamic number of headers would be far more complex and I would advise re-organising your data to allow use of a simpler formula instead.
So, the inner formula:
IF(INDEX($H$3:$H$5,MATCH($A11,$A$3:$A$5,0))<>"",$G$1 & ", ",""),
Similar to how VLOOKUP would, the INDEX/MATCH is returning the POWER reference number for the specific ID Num. If there is no Ref No., it returns an empty string. If there is a Ref No., this is returned along with a trailing comma and space.
This is repeated for each of your Ref columns, and then CONCAT sticks all the results together. This does leave a trailing comma, however, that is relatively simple to remove if required, and I will leave that for you to work out.
As for returning the Ref No.'s themselves this can be done in the same way, but instead of returning the header, you repeat the INDEX/MATCH formula:
=CONCAT(
IF(INDEX($H$3:$H$5,MATCH($A11,$A$3:$A$5,0))<>"",INDEX($H$3:$H$5,MATCH($A11,$A$3:$A$5,0)) & ", ",""),
IF(INDEX($J$3:$J$5,MATCH($A11,$A$3:$A$5,0))<>"",INDEX($J$3:$J$5,MATCH($A11,$A$3:$A$5,0)) & ", ",""),
IF(INDEX($L$3:$L$5,MATCH($A11,$A$3:$A$5,0))<>"",INDEX($L$3:$L$5,MATCH($A11,$A$3:$A$5,0)) & ", ","")
)
Why Not VLOOKUP?
You may wonder why I used an INDEX/MATCH formula, rather than a VLOOKUP formula.
VLOOKUP would work, however, INDEX/MATCH is better in literally every way. To my knowledge, there is NEVER a situation where VLOOKUP would be a better option.
INDEX/MATCH is faster. When you have hundreds or thousands of formulas, you can often speed up a workbook drastically simply by swapping out all of the VLOOKUPs.
INDEX/MATCH is more versatile. It can work on rows or columns and your lookup range can be anywhere. You can even lookup a value in a 2D table using INDEX/MATCH/MATCH.
INDEX/MATCH is more stable. Moving or re-ordering columns doesn't break your formulas.
INDEX/MATCH is easier to use. It may look confusing if you are used to VLOOKUP, but once you understand it, it is actually easier to use because you can directly reference the ranges, rather than counting the number of columns accross.

How to find a specific number in a string of numbers in Excel

I am looking for help with a formula in Excel to find a specific number in a string of numbers.
Our accounting system pulls a report showing numbers from 1 to 10 and each line can have numerous numbers listed in the cell, each separated with a ";".
What I want to do is create a formula that allows me to simply look for the number in columns O to X (row 3) and fill that number in the corresponding cell.
So starting from row 4, I would like to create a formula that finds each number in the string and simply fills in the one I am looking for in each cell, for example I would like the end result to look like the below example:
If the number doesn't appear then that column is left blank, but if it is found it simply adds itself into the corresponding column.
Hopefully someone out there can help me with this.
Regards;
Greg
formula in B3
in German:
=WENN(ISTFEHLER(FINDEN(";" & B$2 & ";"; ";" & $A3 & ";"));"";B$2)
in English:
=IF(ISERROR(FIND(";" & B$2 & ";" , ";" & $A3 & ";")),"",B$2)
Try this in cell O4:
=IF(NOT(ISERROR(SEARCH(O$3,$N4,1))),O$3,"")
Notes:
Assumes your numbers 1 to 10 are in row 3 (i.e O3:X3)

How do I use variables in VLOOKUP in excel vba

I need to use a vlookup function where I would get all the arguments from different functions and use them here.
Eg: ActiveCell.Formula= “=VLOOKUP(B2, ‘sheet 2’!$A:$I, 4, FALSE)
I would like to write this as:
VLOOKUP(element, f_range, col_num, true_false)
Currently, I only want the column to be dynamic, but in the future, I would require all these to be dynamic.
When you write a formula you do it between "" because its a literal string which will be outputed to an excel cell. To use variables you need to write them out of the quotes.
i.e. "=VLOOKUP(" & element & "," & f_range & "," & col_num & "," true_false & ")"
Don't forget the commas because they are aswell on the formula. & is a concatenate operator, it will put together everything you are joining with it.
There is another way if you are willing to have values instead the formulas on your cells:
ActiveCell.Value = Application.VLOOKUP(element, f_range, col_num, true_false)
This will calculate the value and put it to your cell. No need to concatenate anything, just give it the parameters as variables.

How to create a long row of names out of two lists in Excel

I would like to make a long column of names that are made up of two lists, like so:
List 1: Library, Theater, Cinema, Casino
List 2: CityCentre, CentreEdge, OuterCity, Rural
Turn it into a column:
Library_CityCentre,
Library_CentreEdge,
Library_OuterCity,
Library_Rural,
Theater_CityCentre,
Theater_CentreEdge,
Theater_OuterCity,
etc...
Edit:
Here is a sample picture of the excel:
Excel Sample of the desired result
Try this to have your output in a single column.
=IFERROR(INDEX(List_1,INT((ROWS($1:1)-1)/COUNTA(List_2))+1,1) & "_" & INDEX(List_2,MOD(ROWS($1:1)-1,COUNTA(List_2))+1,1),"")
and fill down as far as needed.
If your lists are set up differently, you can adjust the formula to fit, but the idea is using the INDEX function and a mathematical formula to return the appropriate items from each list (array).
Try the below:
=$B$1 & "_" & A2
=$C$1 & "_" & A2
=$D$1 & "_" & A2
=$E$1 & "_" & A2
Results:

excel function to combine cells into single line of text?

I'm new to stack overflow so I apologize if this is a horrendously stupid question. I am wondering if there is a function or way to code a function in excel that will combine a column of cells with plain text and convert them into one cell with the text on a single line? Specifically I want to convert a column of random numbers into a single line of text and insert SPACE+AND+SPACE between them.
Ex.
15133484
12345188
12345888
to
15133484 AND 12345188 AND 12345888
Currently I am copying and pasting all this information into google and then into Word and using find/replace and it is taking forever everytime. If it is possible to just get Excel to do this for me that would be amazing.
Thanks!
If you have Office 365 Excel use TEXTJOIN():
=TEXTJOIN(" AND ",TRUE,A:A)
otherwise one would have to use:
=A1 & " AND " & A2 & " AND " & A3
Or one can use a helper column, B1 put:
=A1
put this in B2 and copy down:
=IF(A2<>"",B1 & " AND " & A2,B1)
And grab the last cell in column B.
A little late, but still:
Reference here
Step 1:
=concatenate(transpose(rngBeg:rngEnd & " AND "))
Step 2:
highlight the transpose statement and then press F9, which substitutes the actual values for the formula.
Step 3:
Remove the curly braces, { }, from the formula. The cell will display the range of reference cells combined with whatever separator chosen after the ampersand sign.
Not a "live" formula, but still far easier than manually concatenating a range of values.
Press ALT+F11 to open Microsoft Visual Basic for Applications,
Insert-> Module
Paste this:
Function Combine(WorkRng As Range, Optional Sign As String = " AND ") As String
Dim Rng As Range
Dim OutStr As String
For Each Rng In WorkRng
If Rng.Text <> "," Then
OutStr = OutStr & Rng.Text & Sign
End If
Next
Combine = Left(OutStr, Len(OutStr) - 5)
End Function
In any cell type =Combine(Range)
i.e.
=Combine(A1:A500)
use concat function if you can add an additional column in the excel like this:
=CONCAT(D3:E5)
Attached sample image with input, additional column, output and formula
I assume you want to merge the data in the 3 cells into a single cell with a space between the 3 data set.
If that is the case then you can do it simply by using the Concatenate function in excel.
In the above example, you have data in Cells A1, A2 & A3.
Cell C1 has the merged data. As you can see, we have used CONCATENATE Function.
The space has been defined in Double quotes. So if you need a Hyphen (-), you can put that in Double Quotes with space “ - ” and it will display the result with Sanjay - Singh - Question
Hope this helps.

Resources