Excel VBA: Find out size of foreign lookup table - vba

I have an Excel VBA sub that (among other things) pastes a lookup formula into some cells. The lookup formula is presently of the form
LOOKUP(RC5,lookup.xlsx!Item,lookup.xlsx!R2C:R559C)
but the size of the lookup table will change. Is there a good way to find the size of the lookup table, that is, the last row?
In a pinch I suppose I could open the lookup file (what's the command for that?) and do
Windows("lookup.xlsx").Activate
last = Range("a65536").End(xlUp).Row
Windows("whatever.xlsx").Activate
but I'm not sure if there's a better way.

Finding the last used cell has a bad reputation in Excel AFAIK. See for example this , although a bit outdated. If you can, just be sure to insert an EOF value in the last used row and scan for it.
Edit
Remember:
A null value (or spaces) may serve well as an EOF flag. Be sure to be consistent.
You may always include full columns in the lookup range and excel will take care of the end (if there are no more used cells under your range. Like in =LOOKUP(A1,C:C,D:D)

Related

Look for a word of column 1 and then search that word in another column of another excel sheet and use the adjacent information to fill the cell

I am trying to organize the data into one sheet. I am looking for a way through which I will be able to extract the potential failure mode from 2nd sheet to the first sheet. The key point is that the potential failure mode should match with its respective component. So the list of components is mentioned. A way through which Potential failure modes of that respective component is detected in another excel sheet and the information which is available in the adjacent column to be extracted on the first sheet.
Your problem is going to be solved with the function =VLOOKUP
Since you are hiding your column and sheet names, I am making some assumptions (assuming the first pic is called Sheet2 PAF is on column B and PFM is on column C). Try on Sheet1!D3 the following formula
=VLOOKUP(A3,Sheet2!B:C,2)
and it will fill in the FPM if a match of Sheet1!A3 is found in Sheet2!B column. You may want further reference
https://support.office.com/en-us/article/vlookup-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1

Excel: How to find out value from table that is missing in database

I'm trying to make some code that takes data from a table and compares it with data in another table.
I have two tables. The first one contains all my customers, the second is a kind of Excel database which contains the names of customers and amount of sold goods.
I was trying to find out the way for making a message box that would show which customer from table 2 is missing in table 1 after pressing a command button.
I tried to do something in VBA, but I'm not too skilled to make that.
Until you get your VBA to the point where you can add it to your question, perhaps an array formula¹ will suffice.
In G2 as an array formula¹,
=IFERROR(INDEX(A$2:INDEX(A:A, MATCH("zzz",A:A )), MATCH(0, IFERROR(MATCH(A$2:INDEX(A:A, MATCH("zzz",A:A )),D:D, 0), COUNTIF(G1:G$1, A$2:INDEX(A:A, MATCH("zzz",A:A )))), 0)), "")
Fill down as necessary to catch all missing customer entries.
   
¹ Array formulas need to be finalized with Ctrl+Shift+Enter↵. If entered correctly, Excel with wrap the formula in braces (e.g. { and }). You do not type the braces in yourself. Once entered into the first cell correctly, they can be filled or copied down or right just like any other formula. Try and reduce your full-column references to ranges more closely representing the extents of your actual data. Array formulas chew up calculation cycles logarithmically so it is good practise to narrow the referenced ranges to a minimum. See Guidelines and examples of array formulas for more information.

Excel VBA Conditionally Remove Duplicates By Lookup Value

I wasn't quite sure how to title this question, but it's as concise as I could think of. Sorry if it's junk.
I have a list created on Sheet2 based on info from Sheet1. What I'd like to do is examine ColumnA and remove duplicate rows if the matching value on Sheet1 ColumnB's offset ColumnE doesn't equal "SUB-ASSY".
Basically:
Find the value from Sheet2, ColumnA on Sheet1, ColumnB.
Get the value from Sheet1, ColumnE
If it does NOT match "SUB-ASSY", delete any duplicates on Sheet2
This should leave one instance of all non-subassembly entries and leave subassemblies alone.
Does this make any sense?
I think I need to do some INDEX/MATCH or VLOOKUP, but I don't know if that'd be the right way to go (or exactly how I'd do it). Does anyone have a good way to do this?
I appreciate any help. I can include the workbook if needed, but it looks up a bunch of stuff on an sql server, so it might not work correctly if you can't connect.
Thanks Again,
-stu
Are you looking for a general idea on how to do this, or a VBA macro?
One way I'm thinking that could get you going is in your second sheet (which, in the end, will have only unique entries) create a 'helper column'. Let's say Column F is empty. You can do a simple "if" statement that will check for the value on that row, in column A is in that other sheet. If so, return "DUPLICATE" (or some note, "Duplicate, delete", etc.). Then run that down the columns, then sort that whole table by your new column. This will sort all the duplicate values in one big block, which you can highlight and delete.
It could help if you show a little bit of what the data looks like on your Sheet1 and Sheet2 (can be made up data, doesn't have to be SQL server info).

VBA- Need help to do average rows if data present in the other columns

I have a excel sheet which we may keep adding rows/ deleting them.
And I have an average value present in some cell.I would want the excel formula to identify if there is text in another column to average the columns
So now if I insert another row, I have to manually update the average formula.
Is there a way to have a formula which check if column A is not empty, it should consider the data in column G for the average
There's a lot of approaches to this. My current favourite is a CELL:INDEX(...) expression. For instance, to find the last populated cell in the first continuously populated range between B1 and B5000, I would use (probably as a named range) $B$1:INDEX($B$1:$B$500,MATCH(TRUE, $B$1:$B$500="", 0)-1).
This approach is great because it's non volatile, so it shouldn't bog your worksheet down. It might be vulnerable to $B$500 gradually shrinking if you're only ever deleting rows, though. Alternatives are referencing the whole column ($C:$C), but that's usually dog slow in modern excel, or using OFFSET which never shrinks, but is volatile.

Excel - How do I find all relevant rows by typing unique invoice# listed Col A

I have a Worksheet with 10 columns and data range from A1:J55. Col A has the invoice # and rest of the columns have other demographic data. Goal is to type the invoice number on a cell and display all the rows matching the invoice number from col A.
Besides auto filter function, the only thing comes to my mind is VBA. Please advice what is the best way to get the data. Thanks for your help in advance.
Alright, I'm pretty proud of this one. Again avoiding VBA, this one uses the volatile formula OFFSET to keep moving its VLOOKUP search down the table until it's found all matches. Just make sure you paste enough rows of the formula that if there are many matches, there's room for all of them to appear. If you put a border around your match area then it would be clear if you ever ran out of room and needed to copy down the formula some more.
Again, in the main section, it's just a single formula (using index):
=IFERROR(INDEX($A$1:$J$200,$M3,MATCH(N$2,$A$1:$J$1,0)),"")
This gets to be so simple because the hard work of the lookup is done by an initial column which looks up the next row that matches the invoice number. It has the formula:
=IFERROR(MATCH($L$2,OFFSET($A$1:$A$200,M2,0),0)+M2," ")
Here is the working example that goes with those formulas:
Let me know if you need any further description of how it works, but it mostly uses the same rules as above so that it's robust in copying and moving around.
I've uploaded the Excel file so you can play with it, but everything you need to reproduce this feature should be in this solution.
Google Docs - Click link and hit Ctrl+S to download and open in Excel.
A popular solution to this problem is a simple VLookup. Lookup the invoice the user types in on the table A1:J55, and then return an adjascent column's data.
Here's an example of it working:
The formula in the highlighted cell is:
=VLOOKUP($L3,$A:$J,MATCH(N$2,$1:$1,0),FALSE)
What's nice about this formula is you only need to type it once and then you can copy it across and it'll automatically pick out the correct column of the table (that's the match part). The rest is very simple:
The first part says lookup value $L3 (the invoice number typed in),
The second part says look it up in range $A:$J (which is where your table is located). I've shown how you can select the entire columns $A:$J so that you can add and remove data without worrying about adjustin the range in your lookups. (Excel takes care of optimizing the formula so that unused cells aren't checked)
The third part picks the column from which the resulting data will be drawn once a matching row is found.
The FALSE part is an indication that the invoice number must match exactly (no approximate matching allowed)
The $ signs ensure that fixed ranges like the location of your source table ($A:$J) and your lookup value ($L3) don't get automatically changed as you copy the formula across for multiple columns.
The formula is pretty easy to adapt if you want to move around your table and the area where you do your lookup. Here's an example:
Bonus
If you want to add a little spiff, you can add a dropdown to the Invoice # field so that the user gets auto-completion and the option to browse existing values like so: