How to substract two cells of x row on excel - vba

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.

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.

fix for moving rows in google-spredsheet breaks array formula

I have an array formula in google sheets for an entire column, e.g. the following formula in C1
ArrayFormula(A1:A+B1:B)
And there is data in columns A and B.
If I were to grab a row and move it to another location. As soon as I move it the respective value in column C of that row is pasted as hard value and breaks the entire array formula.
Is there a way around this?
Unfortunately, there is no simple way around it. With arrays, the formula is usually tied with the positioning of the values as results vary according to the position of each value in an array. Hence, moving anything will result in the distortion of the formula.
The only (simple) way around it is to move your values by the cut-copy-paste method instead of dragging the whole row around. OR (For a more robust but complex implementation) write a script for Custom Functions in your sheet which will perform the necessary calculations and will not be affected when you move the values as it takes inputs from cell positions that have been pre-defined in the script.
Workaround found.
I was doing a manual sort which changed the row of my ARRAYFORMULA(), but has the same consequence as drag-n-dropping that "special row".
You will have to work with two sheets however.
Suppose that in your original data sheet (sheet 1), your have data on two columns (A and B), and you want to use ARRAYFORMULA() on column C, like in your example.
Leave sheet 1 "as is", create another sheet (sheet 2) and in top left cell type this:
={A1:B}
In sheet 2's column C, one cell below top (to leave room for header), enter:
=ARRAYFORMULA(A2:A+B2:B)
Then you can sort data as you wish in sheet 1, and ARRAYFORMULA() will always work in sheet 2 👍

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.

VBA msgbox duplicate value base on 2 columns

I'm trying to get a msgbox when a value is duplicate base on 2 columns. The first column Value can be repeated but the second column will determine if it's a duplicate or not.
i.e.
Column B = Code,
Column L = Month
The user can enter the Code several times, but if he enters it on the same month I want the msgbox pop up
Is your intention to warn\inform the user? If so, I would do this without a macro. I would use conditional formatting to make the cell change color whenever the duplicate information is entered.
Create a column on your worksheet with a formula that concatenates the information in column B&L the formula would be =B1&L1 (copy this formula down the table). You can hide the column so nobody sees it. For this example, let's say you used column "M".
Select the entire Code or Month column (or both) and click the CONDITIONAL FORMATTING button on the Home tab, choose NEW RULE, USE FORMULA TO DETERMINE WHICH CELLS TO FORMAT, then enter the following formula: =COUNTIF($M$4:$M$1000,M1)>1 (note I am assuming your range of data is less than 1000 records, otherwise increase that number). Set the format to something like a red fill and instantly duplicates will be flagged. The user will also be able to quickly locate the record where this combination was already entered as that will turn red too.
If you really do want a macro to do this, you could simply write a loop to compares the active cell value of B(activerow) & L(activerow) to each previous B#&L# combination. If a match is found, use the intersect method to pop-up a the message. Here is really a good article about the intersect method: http://www.ozgrid.com/VBA/vba-intersect.htm.

How to duplicate cell data in excel between rows if the cells are empty when another cell matches

I know the title is really long winded, I will try to explain
I am trying to get Magento products into Zen Cart (background only - shouldn't be relevant).
I have an excel 2010 xls spreadsheet and in it I have a load of product data: skus, prices, categories, etc... you get the idea. I have each of the products along with their data listed once, each with a unique SKU field.
But... Some of these products are in more than one category and the way I am importing, each product needs to be listed multiple times, once for each category.
So, I have done an sql dump which contains two columns; sku and category. I have pasted these values into excel as new rows, so now I have a situation like this (with a lot more entries):
As you can see for each sku (left highlight) there is one entry containing all the data apart from the category (right highlight) and then there is an additional entry for each category it belongs to which contains only the category and sku but none of the other data.
So, what I need to do is some how copy all the data (apart from the categories column) across all cells with the same sku. Does anyone have any idea how I can achieve this without hitting Ctrl+V several hundred times. I realise VBA can probably handle this pretty easily, but I dont have a clue on that front.
Any help greatly appreciated
I hope you’re willing to use a second sheet, and to reference the data rather than actually copying.
On Sheet2, set A1 to =IF(Sheet1!A1="", "", Sheet1!A1).  Drag (extend/fill) this down to A500 (as much data as you have on Sheet1, or further, to allow for growth).  Also drag A1 over to AA1 and then drag that down to AA500.
Then set B2 to =IF($AA2="", Sheet1!B2, B1), drag it to Z2, and drag B2:Z2 down to B500:Z500.
P.S. If any of your data (columns) are dates, you will probably need to explicitly format them as dates on Sheet2.  Ditto for any other values that are formatted any non-default way (e.g., Currency or Percentage).  It may be necessary to do this only to cells that have values in them (and not blank cells).
You don't need VBA, unless you need to do this many times.
Assuming the sheet with your data is called Source and the sheet with the result is called Dest, you can get what you want following these steps on the sheet Dest:
On A1 type =Source!A1
On A2 type =IF(ISBLANK(Source!A2),A1,Source!A2)
Select the range A1:XX1 (where XX is the last column of the sheet Source)
Press Ctrl+R (to copy the first cell to the right)
Select the range A2:XX## (where ## is the last row of the sheet Source)
Press Ctrl+R and Ctrl+D (to copy to the right and down)
Here is an explanation of what's going on:
The first row is copied from the Source as it is.
Each cell of the second row is copied from Source only if that cell is empty, otherwise the cell above is copied.
Here's the quick and dirty solution:
Select the columns you need to be filled
Press Goto Ctrl-G
Special `Alt-S'
Blanks (Alt-K, Enter)
This should select all blank cells. Now type =B2 (assuming you're in B3, i.e. use the cell above the active cell) Important: Press Ctrl-Enter instead of Enter to enter the formula.
Done!
On a new Worksheet get the numbers 1-26 running across the top in Row1 by typing 1 in cell A1, 2 in cell A2, and then selecting those two cells and filling through Z.
Now in A2 type the following formula =VLOOKUP(Sheet1!$A1,Sheet1!$A:$AA,VALUE(A$1),FALSE)
Fill this formula in the Range A2:Z## (where ## represents your last row of data).
Then copy Row1 from Sheet1 to Row1 on Sheet2.
This formula will copy the rows of data straight down into the empty rows below them (assuming the sku only changes where there is a row of new information.
This will then all be active formulas, so I would recommend selecting all on Sheet2 and right-clicking in cell A1, and paste special by value.