Lookup a value from a cell with multiple values - excel

I have an excel file containing 2 columns (Area Code) & (State).
**Area Code** **State**
217, 224, 309, 312, 331, 618, 630, 708, 773, 779, 815, 847, 872 Illinois
219, 260, 317, 574, 765, 812 Indiana
319, 515, 563, 641, 712 Iowa
316, 620, 785, 913 Kansas
270, 502, 606, 859 Kentucky
I want to use vlookup() for a given area code like "620" and get "Kansas". please note that all the values in a row are stored in one cell (i.e. "270, 502, 606, 859" are stored in one cell)

=VLookup("*620*", A2:B6, 2, false)
In VBA:
Function FindState(code as integer) as string
FindState = Application.VLookup("*" & code & "*", mySheet.Range("A2:B6"), 2, false)
End sub

=VLOOKUP(INDEX(A:A,MATCH(TRUE,ISNUMBER(FIND(d2,A:A,1)),0)),A:B,2,0)
Assuming the number you want to find is in d2 and the data is stored in columns a and b.
I asked to find the first find without and error and return the row number. After that, I a lookup for it.
Use array formula ctrl+shift+enter

Related

combine all cells, numbers and symbols to a sum

Good day,
I'm at a loss on this problem.
I have a group of cells that contain words, like apple, this word would be the value. It is separated by a symbol for completing the math. They can be changed by the user to make custom calculations.
Cell A1 is "apple", B1 is "+", cell C1 is "apple", cell D1 is "*", cell E1 is "apple", call F1 is "=" and cell G1 is the suggested total, in this case would be "6".
It would be posted as | apple | + | apple | * | apple | = | 6 |
The legend holds the value for the word, so if you enter 2 in the legend, apple would be 2.
The logic would determine that the formula would be 2+2*2= if written in excel, I would like to combine all the cells and calculate this.
I tried using =sum, sumproduct, concate and the like to no avail.
Any head way I did make, I ended up getting BEDMAS wrong as it calculated it as 2+2=4*2=8, instead of the correct 2*2=4+2=6.
Anyone know of a way to combine these cells and properly sum the values to the correct total?
Thank you for your time!
Go to the Name manager and create named range Eval, into Refers to field add formula:
=EVALUATE(CONCATENATE(VLOOKUP(Sheet1!A1,Sheet1!$A$3:$B$5,2,0),Sheet1!B1,VLOOKUP(Sheet1!C1,Sheet1!$A$3:$B$5,2,0),Sheet1!D1,VLOOKUP(Sheet1!E1,Sheet1!$A$3:$B$5,2,0)))
In range A3:B5 I have legend.
Change references as you need. Then in cell G1 write formula =Eval.
Sample:
This is a UDF based solution. Its advantage is that it's more versatile and by far easier to maintain if you learn its elements of code. The disadvantage is in that you do have to learn the elements of code and you have an xlsm macro-enabled workbook which isn't welcome everywhere.
The setup is simple. I created a table with columns Item and Value. I placed it on another sheet than the task. In fact, you could make the sheet with the table VeryHidden so that the user can't look at it without access to the VBA project, which you can password protect. I called the table Legend. The item columns has names like apple, pear, orange. The Value column has the numeric values associated with each name.
Note that, since this is a VBA project, the entire list can be transferred to VBA leaving no trace on the sheet. You could have a function to display the value of each item as the user clicks on it and have it disappear as he clicks elsewhere.
Next I created a data validation drop-down in A1 with the List defined as =INDIRECT("Legend[Item]"). Copy this cell to C1 and E1.
Then I created another Data Validation drop-down in B1 with the list as +,-,*,/. This drop-down must be copied to D1.
Now the code below goes into a standard code module. Find the way to create it because it isn't any of those Excel sets up automatically. It's default name would be Module1. Paste the code there.
Function Evalue(Definition As Range) As Double
Dim Task As String
Dim Fact(2) As Double
Dim C As Long
Dim i As Long
With Definition
For C = 1 To 5 Step 2
On Error Resume Next
Fact(i) = Application.VLookup(.Cells(C).Value, Range("Legend"), 2, False)
i = i + 1
Next C
Task = "(" & Fact(0) & .Cells(2).Value _
& Fact(1) & ")" & .Cells(4).Value _
& Fact(2)
End With
Evalue = Evaluate(Task)
End Function
Now you are ready for testing. Call the function from the worksheet with a call like
=Evalue(A1:E1). You can use it in comparisons like =IF(G6 = Evalue(A1:E1), "Baravo!", "Try again"). As you change any of the components the cell with the function will change.
Remember to use absolute addressing if you copy formulas containing the range. If you need to get a result in VBA while testing, use this sub to call the function.
Private Sub TestEvalue()
Debug.Print Evalue(Range("A1:E1"))
End Sub
My Sheet
Here is what I have.
In cells M - U, i count all the instances of the word from cells E, G and I from the legend.
=SUMPRODUCT((LEN(E3)-LEN(SUBSTITUTE(E3,$B$3,"")))/LEN($B$3))
In cells W - AE, I multiply the instances with the value to give me a total each time the word appears.
=SUM(M3*$C$3)
In cell E8 - I8, i add the three possible values together.
=SUM(W3:Y3) so each worded cell is now a number.
I'd like to take the cells E8 - I8 to make a calculation in k8 and so on.
So, each cell is put together to make an
=SUM(E8:I8)
statement, which all works except E11 - I11 which equates to 26 instead of 43.

How To Create Dynamic Hyperlink To Another Sheet In Excel based on the value of another cell

I have a value in the cell A1 that can be "PERSON, REGISTRATION,CONTRACT"
Then I have to add in the cell A2, an hyperlink to another spreadsheet:
If the value of cell A1 is PERSON go to spreadsheet2
If the value of cell A1 is REGISTRATION go to spreadsheet3
If thevalue of cell A1 isCONTRACT go to spreadsheet4
How can I do this please ?
Thank You -
Something like this:
=HYPERLINK("#'" & CHOOSE(MATCH(A3,{"person";"registration";"contract"},0),
"Sheet2","Sheet3","Sheet4" ) & "'!A1", "Go")
Edit: some explanation
MATCH(A3,{"person";"registration";"contract"},0) -finds the position of the value in A3 in the array of possible values.
Given that position, use CHOOSE() to pick the correct one from the three possible worksheet names to use in the hyperlink address.
The rest is just stringing all of that together to create the link formula.
Try this one:
=HYPERLINK("#"& LOOKUP(A1;{"contract";"person";"registration"};{"sheet4!";"sheet2!";"sheet3!"}) &"$a$1";"LINK to " & LOOKUP(A1;{"contract";"person";"registration"};{"sheet4!";"sheet2!";"sheet3!"}))
It is important the lookup_value must be in alphabetical order and you should be sort the lookup_vector accordingly.

Multiple Words in one Cell, How to separate without delimiting

Due to an erroneous online survey setup, the answers of a multiple choice, select all that apply question have all come together in one cell. For example:
All the selectable options are as follows:
A12 B1234 C3 D845 E00091 F
Cells with responses look as follows:
Cell A1: A12C3E00091
Cell A2: B1234F
Cell A3: C3D845F
And there are 100 cells like these with random responses.
I need to somehow automate the data extraction and then count each option.
I tried using Left, Right etc. Its not really helping.
I did use Find and then tried to extract data, but I'm unsure of a function that works from a specific start point within a cell.
If anyone could please help with this.
Thanks.
Copy your data to range A2:A101
into B1:G1, enter A,B,C,D,E,F
into B2, enter =LEN($A2)-LEN(SUBSTITUTE($A2,B$1,""))
copy B2 to B2:G101
result:
Kartike,
If I understand you right, you want to know which options are represtented in a cell with responses. I would say that you are right to use find. To test if option 1 is in cell A1 run:
=ISNUMBER(FIND("A12"; A1))
which returns TRUE if the string "A12" is included, and FALSE otheriwse. With the answer strings in a column down from A2 and the options strings in a row right from B1 you could get the full table of options by filling the rows and columns with
=ISNUMBER(FIND(B$1;$A2))
starting from cell B2.
Regards,
Mats
Copy this into a vba module and use Countwords(RangeString, SearchTerm) as a Cell function:
Public Function CountWords(r As String, Search As String) As Integer
Dim a As Variant, str As String, Count As Integer
For Each a In Range(r).Value
str = str & a
Next a
Count = (Len(str) - Len(Replace(str, Search, ""))) / Len(Search)
CountWords = Count
End Function
So =CountWords("A1:A10";"A12") Counts the A12s in the Range A1:A10.

How to compare a string within a string list in Excel

There are some food names and prices as you can see between I2 and J22. For instance AYÇICEK YAĞI(SUNFLOWER OIL IN ENGLISH) is 4$ per kg. In the left of the sheet, you can see other list. What I need is;
I want to compare all A* columns with Strings between I2:I22 and get the price which is written between J2:J22 then write it to the D* columns.
There are more than 500 rows and I need to do it for all rows.
And there are some headings as u can see in bold font, they should be protected.
You seem to have come up with a formula; now you need a way to dispense it. Your worksheet design does not lend itself to simply filling down a formula. However, with the numbers in column C identifying valid entries that require a formula in columns D and E, a short sub procedure can make quick work of putting the formulas into the correct places.
Sub fillFormula()
Dim w As Long, vWSs As Variant, vFRMLs As Variant
vWSs = Array("ogle_aksam_gramaj", "kahvalt" & ChrW(305) & "_gramaj", _
"araogun_gramaj")
For w = LBound(vWSs) To UBound(vWSs)
With Worksheets(vWSs(w))
With .Columns(3) '<~~ going to look at column C for numbers
With .SpecialCells(xlCellTypeConstants, xlNumbers)
.Offset(0, 1).FormulaR1C1 = _
"=IFERROR(VLOOKUP(RC1, 'urunler'!C1:C2, 2, FALSE), """")"
.Offset(0, 2).FormulaR1C1 = _
"=IFERROR(RC4*RC3, """")"
End With
End With
End With
Next w
End Sub
The IFERROR function has been used to 'wrap' the lookup and mulltiplication formulas. It catches errors and offers alternative results; in this case, zero-length strings that look blank.
The kahvaltı_gramaj worksheet causes problems in VBA due to the unicode character. You might try other methods of cycling through the worksheets.
      
That binary (macro-enabled) workbook is available from my own public dropbox here.
In the workbook you have attached, VLOOKUP will return #N/A when there is no available value.
In Sheet ogle_aksam_gramaj Cell D4 use the following Formula:
=SUMIF($I:$I,$A4,$J:$J)
You can then drag it down and it should be giving you the prices based on the details provided in the same sheet (Range I:J)
The good thing (or bad, depends on you) of sum if is that it will return 0 if there is nothing to sum. in your sheet, the items must be unique in the list, otherwise, it will keep summing every instance. So if AYÇICEK YAĞI is there 2 times, it will be summed twice.
You can use Advanced Filter with (unique values only) to make sure that all the ingredients are accounted for and are mentioned only once.
Thanks.

If values in Column B = specific text, insert specific text into a value in Column A

Simply - if any cell in Column B contains thisvalue then append to the adjoining cell in Column A with sometext.
How is this done?
A simple if statement. For example:
=IF(ISNUMBER(SEARCH(thisvalue, B1)), sometext, "")
EDIT: The ISNUMBER(SEARCH(thisvalue, B1)) searches for thisvalue in B1, and if it finds it, it returns a number (that number being the starting index of thisvalue within B1).
EDIT #2: To append the inserted value to the end of the current value in cell A, use the CONCATENATE formula.
Example:
=CONCATENATE(A1, sometext)
Put this formula in A1, then drag down as necessary:
=IF(B1="thisvalue","sometext","")
EDIT
Using a the Visual Basic Editor, you can update the contents of cell A like this:
Private Sub UpdateColumnA()
Dim x As Long
For x = 1 To 65536
If InStr(1, Sheet1.Range("$B$" & x), "thisvalue") > 0 Then
Sheet1.Range("$A$" & x) = Sheet1.Range("$A$" & x) & "sometext"
End If
Next
End Sub
Repeated runnings of the macro, however, will append the text again; you'll need more validation code if you don't want this to happen.
copy-paste in A1 , considering that you have values in B
=IF(ISNA(VLOOKUP("thisvalue",B:B,1,FALSE)),"",VLOOKUP("thisvalue",B:B,1,FALSE)&"ADDITIONAL VALUE")
it is saying:
if value of vlookup is is empty (if lookup returns nothing) , then show empty value ( double quotes)
but if the value of lookup returns something, then do this lookup and append "ADDITIONAL VALUE" text to found result
I think I have what you are looking for, let me know if you are still interested and if you want me to elaborate further. This formula in cell F2: =IF(ISNUMBER(SEARCH($U$2,E:E)),$V$2,"")&IF(ISNUMBER(SEARCH($U$3,E:E)),$V$3,"")&...
where you are searching for a value that you specify in U2 across all cells in column E:E, if it finds a match it appends the value you specify in V2. To search for multiple words assigning corresponding value simply concatenate as shown as much as you like. I am able to specify hundreds of words (and corresponding values). I hope it helps.

Resources