Excel VBA Conditionally Remove Duplicates By Lookup Value - vba

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).

Related

IfError, leave formula in place/don't do anything?

I have a spreadsheet, where I'd like to drag down a particular index/match formula, but where the formula does not return a value, I'd like it to keep the formula that is already in place (which sums up a few of the items below it).
I know that you could just use the cell reference for the if_error part of the formula, but this would return the value of the cell as it is now, and wouldn't use the current formula to generate a new value based on the values returned by the index match formula.
I have attached pictures below. Basically, I want to leave the sum formulas as is, but just be able to drag down that first index formula (the actual spreadsheet I'm dealing with has many different spaces, and is very long, otherwise I'd just copy the formula manually).
If this isn't possible, are there any other solutions? Another thing I tried was for each index/match that didn't return a value, I had it return the formula as a string, and then I'd copy/paste special with values, replace the column in the formula that is a string to the column I'm looking for, and then it would evaluate the formula that was, before, a string. But then you lose the formulas for all the other cells.
So the issue is that some cells are used to sum, and I don't want to drag the formula over those cells, but at the same time, I do need to use the formula over the whole range, otherwise it would just take too long.
Once you put a formula in G1, the previous formula in that cell is no longer available, so referencing G1 in your new formula would just produce a circular reference.
Instead think of a formula that combines both formulas into one: it should detect in which situation it is and then perform the appropriate calculation.
In your case, I think this formula will do what you want:
=IFERROR(INDEX($M$3:$M$9, MATCH(F1,$L$3:$L$9)), IF(E1="", "", SUM(G2:G4)))
Put it in cell G1 and copy it down.
Note how it looks at column E to decide whether it should do the sum. I also adapted a bit the part you already had, by making some references absolute (adding some $), because the area in the L and M columns is positioned at fixed rows.
Add a helper column in row H for your Index-Match formula and copy it all the way down. Then, in row I do an if statement. If row H meets the criteria you want, do that, else use row G.
So I think I found a good solution, especially in the case where you are going to be using the spreadsheet over and over, and the format won't change much. This might be too specific for anyone to use, but posting it just in case someone gets some use out of it.
First I created two macros, one to hide the sum rows, and another to unhide all the sum rows. I got the sum rows from another column by copying all the formulas across to the new column I'm looking at. Numbers will of course be wrong, but the sum formulas will be what we want to keep. You can speed this up by finding "sum" in formulas and then selecting all of the results.
Next, use the macro which hides all the sum rows.
Next, create the index formula in the first row. Control shift down to select all rows beneath. Then, "find", and "go to special" and select "only visible cells", and then hit F2, and control enter, and this will copy the formula down to all the visible cells, ie the non-sum cells.
Then use your unhide macro, and it should be golden!
You can use this technique for any spreadsheet where the source data format is different from target, and where you have fixed formulas in the target which you always will need.

extract data in exel sheet using macro

you most probably going to think "what an idiot" but remember i never done any type of coding before so this is all new to me,
My problem are that i'm working on a HUGE excel sheet with loads of data that is not needed. i need to sort the data into a few columns, i only need column "A,K,AN,AQ" but in column "AS" i only need certain values (yes,no,blank) i only want the yes and blank values. like i said never done any coding before but i know that you can use an macro to do it so please help, how do i go about this?
before trying to get into macros, try to use functions with if else statements. They are quite easy to handle. Like: If (yes) then put it into X. Later, you could select all needed. Also, check the, how the dollar sign is used
use this links to see, if it is something for you.
One quick and dirty way of getting this job done would be to:
Delete the columns you don't need.
Select all cells in the range you're interested in, click the Insert menu, and choose "Table". If your columns have titles, select the box for "My Table has Headers."
-This turns your data into an array so that Excel recognizes that each row is an entry (instead of thinking that the cells are unrelated).
Now you can use the filter icon in the column headers to select and display only the rows containing the values in column X that you're interested in.
Note that there are some limitations to what the table feature is good for, so, as always, whether this is a good solution for you depends on what you want to do with the data.

Deleting data in last cell of data in Column A

I am in need of assistance agian. When my macro works, the number 1 is populated in the final cell in column A because the coding is set up to place a number 1 or 2 depending on other data until the last row of data. Since the last row of data has necessary information in other columns, I wanted to delete the 1 in column A because it is not needed. How do I code to take away the value in the final cell of a column? I've tried referencing to it and if statements. I am sure I am missing something simple.
This is my first macro using VBA.
Thanks.
I just started working with VBA in the last day or two, but I believe the thing you are looking for is: Cells(x,y).End(xldown).ClearContents
Alternatively, I think Range("A1").End(xldown).ClearContents would be equally effective.
But I just started doing this myself, so take with spoonful of salt.

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:

Excel VBA: Find out size of foreign lookup table

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)