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

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.

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.

How to substract two cells of x row on excel

I'm making an Excel sheet to keep track of some activities. The thing is that I have 2 cells that are date type; I want the third cell to subtract the them to get the time that the x person spent on the activity.
I know that if I type =A2-A1 it's going to give me what I want, but, since its going to be a big Excel sheet with lots of records, I don't want to input the same formula for each row just changing the row number.
Is there a way to make Excel detect the row that the user is inputing data in and then make the requested formula to get the time?
you can turn your data range into a table by highlighting the range and going to the insert tab and clicking table. Then when you type the formula into the first cell and click in the cells when selecting instead of typing it out, you will notice that it is using the column names instead, also it will automatically fill the column with the new formula. That would be my suggestion.

Speed up the deletion of duplicates

I coded a VBA script in Excel which adds new data into a Datasheet with previous information. Before doing that, the new data is copied into a provisional Datasheet. To prevent duplicates, I create an additional column and do a VLOOKUP of IDs. If the ID from the new imported data is already in the Datasheet with the old data, this row is marked as duplicated and will be deleted. The "non-duplicated rows" are then copied into the final Datasheet, where all the data is stored.
Right now I use a column reference (A:A) in the VLOOKUP and I donĀ“t know if maybe this is the reason why the VBA script needs every day more resources and time to run. When I coded for the first time, I did the test with no more than 4,000 rows in the original Datasheet and 4,000 rows in the imported data. The macro was done after 90 seconds. Right now, it needs more than 5 minutes and the Datasheet with data is just 40,000 rows large, while the new data is always around 4,000 rows.
Should I dynamically reference the range in the VLOOKUP instead of using A:A or it doesn't matter in terms of speed?
As mentioned in my comment, there certainly is a way to accomplish this task using VBA, but sometimes the simpliest solution is best. I would reccomend added all 40K records each time and using the "Remove Duplicates" function under the "Data" ribbon using the column that holds your unique value.

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)