Looking up values in a table with VBA - vba

I am trying to lookup values in an excel table using VBA and having trouble getting it all together. I get most of the way there but my code is not worth posting.
What I need to do is write a function with several parameters:
1. What value to look up
2. What column name to look this up in
3. What other column to return the value from
For example, in a table containing Customer_ID, Last_Name, First_Name: pass it "DoeJohn", "Customer_ID", "First_Name" and have it return John. I am not concerned with duplicate values of what I am looking up, finding the first instance is good enough.
The cell it finds may be text, numeric, or date so it needs to return that.
If it doesn't find a match it should return something that could never be an actual cell value, and my main program will check for that.

If you are familiar with writing Functions() in vba you could simply have a custom function that contained a few vlookup() functions then organize the output and return it however you want.

Related

Coloring Excel Cell based on a table condition

I am going through the users of a system and reviewing if they have appropriate role names. I then completed an excel table that looks abit like this:
I'm trying to turn the table into a more readable format. I have made a pivot that looks like this:
But I'm not sure how to highlight the cells to reflect the 'Access Appropriate? Yes/No' column. Ideally, it should be colored yellow if the 'Access Appropriate?' = 'No'. I'm thinking of using VBA, but was wondering if there is an easier solution using formulas or pivot table?
Your pivoted data isn't an actual excel pivot table, is it? I know what the x mean, but where do they come from?
Two possibilities come to mind if you want a flexible setup without VBA, aswell as an rather simple VBA-approach that uses an UDF.
Quick'n'dirty (really dirty) would be to
use 1/0 instead of yes/no (you could write that into a helper column with an if-function)
create a new pivot with ROLE_NAME for columns, USER_NAME for rows and SUM or MAX of [Access appropriate] for values
that means: instead of your x you will end up having 1 and 0. Empty cells will still be empty.
conditional format the value-range, e.g. If 1 then green If 0 then yellow if "" then Nothing
Alternatively, you could build your output-table with formulas like INDEX, MATCH and VLOOKUP-formulas.
An additional Key-Column with USERNAME&ROLE_NAME will be needed
conditional format the value-range
VBA: Provided your Rows are distinct a user defined function could do the following
read data into a recordset IF that hasnt been done already (meaning: declared on module-level, the first function call will fill it)
access the data in your recordset with a Recordset.Filter based on your input parameters - USERNAME and ROLE_NAME, in your case
output a certain Field.Value based on your input parameter - Access Appropriate in your case
conditional format the TRUE/FALSE values you get (since this can't easily be done inside an UDF)

Need a simple search function to display most common value in a column. (with ambiguous choices)

I have a very large array of data with many columns that display different outputs for the values presented. I would like to add a row above the data that will display the most common occurring value or word below.
Generally I would like to have each top of the column (right under the column label in row 1) have the most common value below. I will then use this value for various data analysis functions!
Is this possible, and if so, how? Preferably this will not require VBA, but simply a short code in the cell.
One caveat: The exact values may vary, so there is no set list where I can say "it will be one of these."
Any ideas appreciated!
Try a series of =COUNTIF(A:A,"VALUE TO SEARCH") functions if you want to stay away from VBA.
Otherwise, the best method would be to iterate through each column via VBA. With this method, you can even count the "varying" values and return the count and/or the value itself.
http://www.excel-easy.com/examples/most-frequently-occurring-word.html
This is a single formula you would write at the top of each column. Does not require VBA. You can replace the set range to an entire column, such as (A:A) instead of (A1:A7).
If you mean an array as in a data type, it could work differently but it depends what you're trying to do.
With data from A3 through A16, in A2 enter:
=INDEX($A$3:$A$16,MODE(MATCH($A$3:$A$16,$A$3:$A$16,0)))
This will work for text as well as numbers. Adjust this to match the column size.

Getting an Index/Match with a single criteria to sum its results

I am using an INDEX/MATCH to locate a facility number, then go to the appropriate column and return the first non-blank answer it receives. this is great. However, I can't figure out how to add a SUM function to this, so it will add all of the index match results, and not just stop at the first one it finds. I'd like to only use a formula, not VB. This is what my array currently looks like.
=INDEX(INDIRECT("$H"&(MATCH(M45,$B:$B,0))&":$H$10000"),MATCH(FALSE, ISBLANK(INDIRECT("$H"&(MATCH(M45,$B:$B,0))&":$H$10000")), 0))

Count unique string variants

There could be quite a simple solution to this, but I am trying to find the number of times a unique variant (i.e. non-duplicates) of a string appears in a column. However this string is only part of the text contained in a cell, and not the entire cell. To illustrate:
EuropeSpainMadrid
EuropeSpainBarcelona
AsiaChinaShanghai
AsiaJapanTokyo
EuropeEnglandLondon
EuropeSpainMadrid
I would like to find how many unique instances there are of a string that contains "EuropeSpain". So using this example, I would find that a variant of "EuropeSpain" appears only twice (given that the second instance of "EuropeSpainMadrid" is a duplicate).
A solution to this is to use pivots to summarise the data and remove duplicated; however given that my underlying dataset changes often this would require manual adjustments and corrections. I would therefore like to avoid adding any intermediate steps (i.e. PivotTables, other data sets etc) between my data and the counts.
UPDATE: I now understand to use wildcards to solve the first part of my question (counting the occurrences of "EuropeSpain"), however I am not yet clear on the second part of my question (how to find the number of unique occurrences).
Is there a formula or VBA code that could do this?
Using wildcards:
=COUNTIF(A1:A6,"="&"*"&C1&"*")
For without VBA but with some versatility, I suggest with Text in ColumnA (labelled), ColumnB labelled Flag and EuropeSpain in C1:
=FIND(C$1,A2)
in B2 copied down.
Then pivot A:B with Flag for FILTERS (and 1 selected), Text for ROWS and Count of Text for Sigma VALUES.
Apply Distinct Values if required (and available!), alternatively a formula of the kind:
=MATCH("Grand Total",E:E)-4
would count uniques.

Create list from data range

I was hoping to get a bit of help regarding formulas in Excel/VBA, I'm looking to do the following but I'm not sure how best to do it:
Okay, so I have a materials export from a piece of software and it comes out with the following sort of data:
In essence I would like to have a catalogue stored on a separate sheet which the function references for text strings that may be contained in a takeoff.
i.e. in E2 the function will check the column B for cells containing the text strings in A16:A18 (the consoles) and return any results along with the corresponding quantity to columns E and F. Similarly this will occur for the controller and cables.
I'm sure there's a solution somewhere but I've been unable to find one for lack of knowing what to search exactly.
Thanks for any help guys, it's appreciated.
What you may want to use is the COUNTIF function. Try this:
COUNTIF(A$2:A$13,"*" & A16 & "*")
If I am not mistaken you can check for the values in A16:A23 in the database dump column B1:B13, then you want the summer quantity for the matching values, where a match means the text you look for must be contained in the database dump column.
To get the quantity accordingly use the following formula in cell B16:
=SUMIFS(C$2:C$13;B$2:B$13;"*"&A16&"*")
then drag it into the other cells. The asterisks are text wildcards, so there may be text in front or behind the text part we look for.