vba code to find and replace text across columns in excel - excel

I have spent the day trying to learn vba code and searching other questions for the same example but have been unable to crack this so help would be greatly appreciated.
I have worked out how to do it in vlookup but only to a single row.
This code from kutools is the closest i could find and almost does what i need but is only reading single digits in my range (which goes up to 700) so the value i return is multiples of the value string. This would need to be modified to only search and replace the specific value. http://www.extendoffice.com/documents/excel/1873-excel-find-and-replace-multiple-values-at-once.html
I have 3 columns: A has a random list of numbers, many repeating; B has a list of reference numbers which correlate to a place name in C.
A | B | C
1 | 1 | Melbourne
1 | 2 | Adelaide
1 | 3 | Sydney
2 | 7 | Auckland
2 | |
3 | |
7 | |
The code should result in the number in column A being replaced by a place name from column C by first finding the matching number in column B. Note: There are less rows in columns B&C than there are in column A.
There are thousands so i need a code to do this.
Thanks in advance.

Well first off I'd place the VLOOKUP data into a new column so to preserve your original data set, but here's the code that you should be looking for:
=VLOOKUP(A1,$B$1:$C$4,2,FALSE)
Here's the syntax of a VLOOKUP:
=VLOOKUP(lookup_value, table_array, col_index_num, [range_lookup])
Now for each part. The lookup_value is what you're using as a reference to search for, this will be your information for columb A. The table_array is a static reference of where to look for this value, and then the resulting values you want to display, so in your sheet you'll make this all of columb B and C. The col_index_num represents the index of the table_array you chose previously, so since the name of the cities is in the second column of this table, we put 2. range_lookup is whether or not you want an exact match. We do, so we set it to true. And then you can just replicate this value down a column to have the results for everyone.
That should be exactly what you're looking for. I even created a test sheet to try it out. In the future I suggest posting the code you've already tried.

Related

Excel Offset: Automating the Reference Cell by using Index Match

If the answer was already provided, please feel free to post the link, but I did not see one.
This is a real estate example. I have a table of a lot of properties and information. One of the fields is "Occupancy." I am trying to use the AVERAGE OFFSET functions combined with CELL and INDEX/MATCH to calculate the average Occupancy of the top 5 and top 10 buildings in this table. To accomplish this, I wanted to create a "Summary Table" below the Property table to list the averages.
Specifically:
For OFFSET, in the "Reference" section, I want to look up "Occupancy" in my summary cell, use INDEX MATCH to find "Occupancy" in the table, and then;
I want the Cell Reference returned AS A VALUE and then move one row down and then highlight the top 5.
Most importantly, I want everything cell referenced without hardcoding or navigating the table to highlight the cell reference. I am not sure if this is possible, but after pouring through a bunch of articles, I am having trouble finding a solution.
To make things simple, let's pull out all the columns I had in the original table and go off the below. Assume all the data types are in the proper format:
**COLUMN A** **COLUMN B**
ROW 1 | Properties | Occupancy |
ROW 2 | Property A | 58% |
ROW 3 | Property B | 56% |
ROW 4 | Property C | 77% |
ROW 5 | Property D | 85% |
ROW 6 | Property E | 92% |
ROW 7
ROW 8 |Top 5 | | 'NOTE: I used a custom formula to format:
"Top "#. Please do not think this is text.
ROW 10|Occupancy | **CODE HERE**| 'Format is "General"
ROW 11| | |
ROW 12| | |
Here's how I initially coded it in B10:
'=AVERAGE(OFFSET(CELL("address",INDEX($A$1:$B$1,MATCH(A10,$A$1:$B$1,0))),1,0,5,1))
I am getting "there is a problem with your formula" since I am guessing it is due to the cell returning text in lieu of the cell reference as a value. So I error checked myself and stripped out the AVERAGE, OFFSET and put this is B11:
'=CELL("address",INDEX($A$1:$B$1,MATCH(A10,$A$1:$B$1,0)))
I know I get $B$1, so I then put $B$1 to replace the CELL function within B10 to see if my average offset is wrong:
=AVERAGE(OFFSET($B$1,1,0,5,1))
I now get the right solution since I went and hardcoded the reference, which does not solve the issue.
If I use INDIRECT and then CELL in B12:
'=INDIRECT(CELL("address",INDEX($A$1:$B$1,MATCH(A10,$A$1:$B$1,0))))
I then get the title "Occupancy" and not the cell reference.
Obviously, I am aware of the fact that the CELL function will return text and not a value. I can't use INDIRECT because when I do, INDIRECT will just display the title of the column in text and not return the cell reference as a value. ADDRESS is not useful because you have to hardcode and the goal is to have zero hardcodes.
How can I code finding the cell reference of specific text in a table without actually going to the table to cell reference when I am building out my OFFSET function?
As you can see, I went through a variety of ways to dissect the problem and I would appreciate anyone's help to come to a solution. Thanks in advance.

Excel: Find out if cell contains/includes value from a range of cells

I have a list of job titles (A) and list of phrases (B). For each title in A I want to check if it contains a phrase (any phrase, I don't care which) from B.
| 1 |-----Example Column A------|----Example Column B----
| 2 | Head of Marketing | Senior Developer
| 3 | Lead Product Engineer | Marketing Manager
| 4 | Sales Development | Sales Development
| 5 | Senior Marketing Manager |
In the above example, I want to know that the last two cells in column A contain cells in column B.
I found lots of examples online of how to do the reverse, using * to find if a value is contained in a range. I also found the following three examples offered as solutions for problems similar to mine, but none worked for me.
{=MAX(ISNUMBER(SEARCH($B$2:$B$4,A2))+0)}
{=MATCH(A2,$B$2:$B$4&"*")}
=IFERROR(LOOKUP(2^15,SEARCH(B:B,A2),B:B),"")
I've also tried writing the contents of each cell in column B to start and end with an asterisk (*Senior Developer *, etc.), trying VLOOKUP, SUMPRODUCT, COUNTIF without success.
Is it possible to do what I want?
Solution
Slightly modified Mrig's formula:
=SUMPRODUCT(ISNUMBER(FIND(B$2:B$4,LOWER(A2)))*1)
Try this
=SUMPRODUCT(ISNUMBER(FIND($B$2:$B$4,$A2))*1)
This formula will return the number of phrases matched.
You can put this formula in IF if you don't want the count.
=IF(SUMPRODUCT(ISNUMBER(FIND($B$2:$B$4,$A2))*1)>0,"Exist","Does Not Exist")
See image for reference
EDIT: After sorting Column A in descending order output I get is:
Put the following formula in col C for each row in col A (starting at C2)
=IF(ISNA(INDEX(B$2:B$4,MATCH(A2,B$2:B$4,0))),"","x")
This is a combination of the INDEX and MATCH functions. INDEX returns a cell within a range based on a count, which in this case is provided by the MATCH function which returns the position of a cell within an array by matching against a criteria string.
The ISNA function is there to hide the "N/A" output in any non-matching cells and display "x" against any matching cells.
Have a look here for a more in-depth explanation.
UPDATE
As per the comment below, in order to see if Col A contains the text in Col B (rather than being a direct match) then use this formula in col C for each row:
=IF(SUMPRODUCT(COUNTIF(A2,"*" & B$2:B$4 & "*"))>0, "X", "")

Return all the cell whose next one contains a value range

I need to return in a cell all the cell values where the cell next to it contains a value range.
For example, if I have a table like this:
|Name |Evaluation
|------|------
| John | 3
| Sue | 4
| Jim | 2
| Andy | 6
| Tim | 1
| Bruce| 4
I'm looking for a formula to have all the names whose evaluation is >= 4, so, if applied to the table it should give as output in a single cell:
Sue
Andy
Bruce
I've already tried VLOOKUP, INDEX, MATCH and FIND functions but they all return a single value (the first cell that match) and not all of them.
If possible, I'm looking for an Excel Formula and not for VBA (this way I can share it easily with my working group that, as myself, is not very proficient in VBA).
Thank you very much!
=IF(B1>=4,A1,"") write the command in c1 column and drag the C1 column to the end of the column till the end name
(assuming you write first name in A1 column and Evaluation in B1 )
I've solved the issue by using (partially) the solution posted by dhS and a support table.
I've created the support table of the same height of the original one. This table goes from F1 till F120 (the end of the original table).
In the first cell i've used the formula
=IF(B1>=4;$B1;"")
In all the subsequent cells (from the second to the final one)
=IF(B2>=4;IF(F1="";B2;F1&CHAR(10)&B2);F1)
This way, in the last cell there will be all the names separated by a return (CHAR(10)).
To anyone who want to use this solution, remember to enable the Wrap Text option on the cell, otherwise you won't be able to visualize the returns.
Thanks to everybody for the help you gave me.

VLOOKUP not giving expected results

I've used VLOOKUP plenty of times, but this has really stumped me. I have a VBA code that copies data from one workbook to another and then I'm trying to perform the VLOOKUP on the data that I've copied.
Here's an example of how the data I've copied is layed out in Sheet2:
5009 | Materials | Store Items
The table that has been copied is given the name "AutoList" for the range of the table (Theres a lot more than this obviously)
The table I'm trying to use the VLOOKUP on looks like this:
[VLOOKUP] | Materials | Z | X | C | V
The formula I am using is:
=VLOOKUP("Materials",AutoList,1,0)
I can't for the life of me figure out why I am getting #N/A as the result. If I remove the 0 on the end (for the logic), I end up with the last number in the AutoList table for every result as it's only doing a partial match.
Any help would be appreciated!
Is the "lookup_value" in the first column of your "table_array"...?
According to Excel's help file vlookup looks, "for a value in the leftmost column of a table." Does AutoList define your range with "Materials" in the first column? If so, then shouldn't the result of your vlookup formula return "Materials"?

Find one cell inside a string and then populate another cell based off of that

I have a basic financial spreadsheet that I am trying to make even nicer. I have a table that looks like this:
| Description | Category | Amount
| 2341234 Chick-fil-a | Eating Out | 5.89
| 11234 Redbox/Georgetown | Entertainment | 1.21
And another table that looks like this:
| Name | Category |
| Chick-fil-a | Eating out |
| Redbox | Entertainment |
I want to find the strings from the first column of the second table in the first column of the first table and automaically populate the second column of the first table. I am pretty sure I can find the position of the string by doing a {=find(2ndSheet!A:A,1stSheetA:A)} or something of the like, but I have no idea how to populate another cell based off of that.
Does it have to be a macro?
This is a bit ugly but seems to work. It handles the situation where your description may not necessarily match the lookup table (for instance, with Redbox | Redbox/Georgetown). Replace the [Lookup_Table] with the absolute range of your lookup table, and enter as an array formula (Control+Shift+Enter):
=INDEX([Lookup_Table],MATCH(TRUE,NOT(ISERR((FIND([Lookup_Table],A2)))),0),2)
As for what it's doing:
=INDEX(
[Lookup_Table],
MATCH(
TRUE,
NOT(ISERR((FIND([Lookup_Table],A2)))),
0),
2)
It's using an INDEX formula with the [Lookup_Table] as the base range. For the row to match, it looks for instances where running the FIND formula using the current cell does not cause an error. So in the case of Redbox, you'd get an array like {#VALUE!, 7}. You then set the values to TRUE/FALSE based on whether they are errors, and then flip them so that non-errors are True. You then match the TRUE condition against that array, and a hit will return the row number in your lookup table. You then use that row number and column 2 to find the corresponding value.
Again, that is not a pretty one (and there is probably a better way to handle all of your cases), but it seems to work in my extremely exhaustive test of three rows :)
Assuming that your first table is starts in column A, substituting this formula starting in B2 should find the value for you.
=VLOOKUP(RIGHT(A2,LEN(A2)-FIND(" ",A2,1)),[Range of Helper Table],2,FALSE)
You'll need to substitute [Range of Helper Table] with the actual range. Make sure you use absolute references using $, for example: 2ndSheet!A$2:B$20. so that you can copy and paste.

Resources