Excel VBA: Auto-sum the value of three cells and insert into the fourth cell - vba

I have four columns A, B, C, D. The values of the cells start from A1,B1,C1,D1 and user may go up till A100,B100,C100,D100 (on click of a add button, after inserting values in each rows, one at a time)
When user will enter any value in A1 or B1 or C1, it should sum up and the value need to reflect in D1. There may be scenario like user may enter only values in one of the cell and leave other as it is. like A1 as blank, B1 as blank and C1 as 100. So in D1 it should reflect 100.
Just to mention, this is a protected sheet with a lot of different features, like cell level validation, sheet level validation, and the entire sheet will be locked other then one row where user will be able to enter the details and then he will click on ADD button, on click of this button, first all the fields will get validated and if successful then only a new row will be added.
Please help..

This doesn't require any VBA code. Just enter following formula in D1 cell: =A1+B1+C1 and drag it all the way down until 100th row. This way, blank cells are treated as 0.
So, if cells in A,B,C columns are blank, correspnding value in D column will be 0.
Also, value in D column would be raclculated on every change in column A,B,C in corresponding row.

Related

How do I insert a row in excel and include the inserted row in a formula?

Example:
I have a cell "A1" that sums up some values in a row "IF"-something: =SUM.IF($C$5:$C$10;"Blue";$D$5:$D$10)
meaning: If columns C5 to C10 contains the word "Blue" in any row for example. "C7" and "C9", then the sum of "D7" and "D9" will be shown in "A1".
My problem:
If I insert a new row "5", then I want the formula to contain this row as well:
=SUM.IF($C$5:$C$11;"Blue";$D$5:$D$11)
But what happens is that the formula is now:
=SUM.IF($C$6:$C$11;"Blue";$D$6:$D$11)
So the formula contains only the original 5 rows "5-10" and now named "6-11".
How do I make excel expand the formula to include the new row aswell so it sums up from "5-11" =SUM.IF($C$5:$C$11;"Blue";$D$5:$D$11)?
(I know I can make this work with a table, but this is a thought example and not my real situation. Actually I insert 5 rows at a time, and some cells are merged, so I can't convert the range of data into a table)
Any help is greatly appreciated.
This can't be done. Set the row range reference in the formula to one row above you actual list, then everything should work.
For example, leave row 5 empty. Hide the row if you like. In cell A1, use the formula =SUMIF($C$5:$C$10,"Blue",$D$5:$D$10) (starting at row 5). Start entering values in row 6.
Now you can right-click Row 6 and Insert a new row, and the formula will continue to work as required.

subtract the last two non-empty cell in excel

This is I think a simple problem but I can't seem to find the right solution for it. I don't know if VBA is needed for it. Basically I just want to subtract the last two non-empty cell in Excel. Example: I have the cell A1 and cell B1 and then subtract the value of A1 from B1 and place this value at the same row but another column C1 and so on. I have read this to get the last non-empty cell but I need two non-empty cell and perform operation on them.
=LOOKUP(2,1/(A:A<>""),A:A)
Is there an easier way to do this? Or should I use VBA?
EDIT:
the reason is I'm making a somewhat similar to a balance sheet but a simple one, the user will just enter deposited value at the first column and expenses at the second column and then display the balance in the third column.. and vice versa.
EDIT:
Sample
Put this in C4 and copy down:
=SUM($A$3:$A4)-SUM($B$3:$B4)
If you want to copy the formula past the data so it will automatically fill when data is inserted you can do what #Dirk stated:
=IF(A4&B4<>"",SUM(A$3:A4)-SUM(B$3:B4),"")
Put it in C4 and copy down as far as desired.
This way you can fill the column with the formula and as the data is filled it will change from the empty string to the running total.
EDIT:
Regular formula:
=IF(ROW($C1)=1,OFFSET($C1,0,-2)-OFFSET($C1,0,-1),OFFSET($C1,-1,0)+OFFSET($C1,0,-2)-OFFSET($C1,0,-1))
in cell C3 put "=A3-B3" in cell C4 put "=C3+A4-B4" then copy down.
This will start the balance at 3000 in C3 then add any deposits or minus any withdrawals to the rest of column C.

Searching and comparing various values using VBA

I have to create a macro which will:
Get the value from the cell A1 search this value in column C.
If the value in cell A1 exists in column C, the macro needs to be compare the value in cell B1 with values in column D.
If the value in cell A1 exists in column C AND the value in cell B1 exists in column D, then the text "Values found" should appear in cell E1.
The above needs to happen for all non empty rows in column A.
I was trying to use the following formula:
=IF(ISERROR(MATCH(A2,$C$2:$C$138,0)),"Load number not found","Load number found")
But it not working as I want. I have limited access to internet so I can't check all web sites. Would you please help me. Thanks a lot.
To check if A1 is in column C and if B1 is in column D (in the same row A1 was found in column C), then you need to do the following:
=IF(ISERROR(MATCH(A1,$C:$C,0)),"Load number not found",IF(B1=INDEX($D:$D,MATCH(A1,$C:$C,0),1),"Load number found","Load number not found"))
The first IF checks if A1 is in column C, the second IF checks if B1 is in column D (in the same row A1 was found in column C)
It will return "Load number found" only if both conditions are true. Otherwise it will return "Load number not found".
You can write a macro to do the same thing. But the easier way is to lock the cells in column E only and protect the sheet so that users will not accidentally change any of the formulas.
Update:
Since Column C can have duplicates, need to use the following array formula:
=IF(ISERROR(MATCH(1,(A1=$C:$C)*(B1=$D:$D),0)),"Load number not found","Load number found")
When you paste this formula to E1, make sure to press CTRL + Shift + Enter instead of just pressing the Enter key.
If I understand, a conventional solution with formulae is to concatenate your C and D column data and then search that. If you insert a new columnC with:
=D2&E2
copied down to suit you could apply (but say in ColumnF rather than ColumnE) your existing formula with only slight modification:
=IF(ISERROR(MATCH(A1&B1,$C$2:$C$138,0)),"Load number not found","Load number found")
subject to quite what is in which row.

excel vba: Specific data columns moving to the right

I am having trouble with writing a small code of VBA that does the following: In a huge dataset, search a particular column for entries that are obviously wrong (e.g. being above 50,when they should be from 1-10).I want this to find these entries,and copy alla the columns to the right of it and move them 3 columns further to the right.It is some sort of data cleaning.Thanks
Not sure why you would need VBA. Lets say that column A should be populated with numbers between 1 and 10. And columns B,C and D are populated with other data.
Select cell E1 and put in the formula =IF($A1>50,A1,"") in the formula box and push CTRL + Enter.
Use the drag handle to copy the formula into cells F1, G1, and H1.
With cells E1 through H1 selected, double click on the drag handle of cell H1.

Count rows that have cell don't existing in a list in Excel

Please see my image
I am have two questions:
Do you know what is the proper function to count number of rows that have F column value don't exist in range J3:J15 (expect result is 3, as the image)
List/or show values in Column A, B of above rows (the rows that have F column value don't existing)
Way №1
In cell H2 use:
=IF(ISNUMBER(MATCH(F2,$J$3:$J$15,0));"exist in list","not exist in list")
and drag it down.
Then in C12 you can use:
=COUNTIF(H2:H8;"not exist in list")
Way №2
In C12 use:
=SUMPRODUCT(1*NOT(ISNUMBER(MATCH($F$2:$F$8,$J$3:$J$15,0))))
This is an array formula, so type the formula then press CTRL+SHIFT+ENTER. Curly brackets will automatically appear at the start and end of the formula.
Then for returning list of corresponding values in column A use, say, in F12:
=IFERROR(INDEX(A:A,SMALL(IF(NOT(ISNUMBER(MATCH($F$2:$F$8,$J$3:$J$15,0))),ROW($F$2:$F$8)),ROW()-ROW($F$12)+1)),"")
this is also array formula, so press CTRL+SHIFT+ENTER to evaluate it and then drag formula down.
and in G12 for returning corresponding values from column B:
=IFERROR(INDEX(B:B,SMALL(IF(NOT(ISNUMBER(MATCH($F$2:$F$8,$J$3:$J$15,0))),ROW($F$2:$F$8)),ROW()-ROW($F$12)+1)),"")
also with array entry(CTRL+SHIFT+ENTER).