Is it possible to add/subtract in one cell, and have it subtract/add in another cell that already has a value? - excel

Is it possible to add/subtract in one cell, and have it subtract/add in another cell that already has a value?
I am thinking it may be a if function but I can not wrap my head around how I would write out the formula.

Let's say you have 2 columns B and C that already contain data.
And if you Add a number to B you want that number to be subtracted from C.
My recommendation is to write a macro that will work as follows:
First the user selects the two columns and then runs the macro
For each row
Cell c = getCell("C" + row);
double cval = c.Value;
c.type = FORMULA;
c.Formula = "=" + (cval + getCell("B" + row).Value) + "-B"+row;
c.Recalculate()
Example:
Original:
A B C
1 Gas 5 10
2 Air 8 12
Replace with:
A B C
1 Gas 5 =15-B1
2 Air 8 =20-B2
so you only change B, and the value of C is automatically calculated.

Related

Can I use SUMPRODUCT to accomplish this?

Need to sum a range based on if a value is in a column and one of a set of values is in another column, or vice versa.
e.g. I have The following table:
A B C D
M C C 1
F C C 2
S N C 3
S N N 4
M - C 5
N C C 6
M C N 7
If (Column A contains "M" or "S") AND ((Column B contains "C" AND Column C Contains "C" Or "N" Or "-") OR (Column C contains "C" AND Column B Contains "C" Or "N" Or "-")) Then Sum column D
So from my table my results would be
1 + 3 + 5 + 7 = 16
You can use SUMPRODUCT like this:
=SUMPRODUCT(ISNUMBER(MATCH(A2:A10,{"M","S"},0)*MATCH(B2:B10&"^"&C2:C10,{"C^C","C^N","C^-","N^C","-^C"},0))+0,D2:D10)
MATCH is used to check for both valid possibilities in column A and then all 5 possibilities for concatenated columns B and C - if those conditions are met then column D will be summed. Extend column ranges as required but preferably don't use whole columns
.....or shorter with SUMIFS like this:
=SUM(SUMIFS(D:D,A:A,{"M";"S"},B:B,{"C","C","C","N","-"},C:C,{"C","N","-","C","C"}))
For that version you can use whole columns with no loss of efficiency.
Note that in this version all the separators in the array constants are commas EXCEPT for the semi-colon in {"M";"S"} which needs to be that way
I would add a fifth column with a condition for the current line returning the value in D if all conditions are true or 0 otherwise.
=Iif(AND(Or($A1 = "M", $A1 = "S"),OR(AND($B1 = "C",Or($C1 = "C",$C1 = "N",$C1 = "-")), AND($C1 = "C",OR($B1 = "C",$B1 = "N",$B1 = "-")))),$D1,0)
Then in a cell somewhere write =sum($E:$E). With your example, I get 16, as intended.

Excel for loop to get value from another row

I have an spreadsheet that contains various data. It looks like this:
A A A B B C C C C
a 1 2 3 2 1 4 2 3 2
b 0 2 3 3 0 1 2 3 0
c 6 6 3 0 2 1 0 4 0
etc.
What I want is to add all the Aa's and come up with a Aa total, all the Bb's and come up with a Bb total, all the Ab's etc.
What I want to do is, for every column, check if it is A, B or C. I want to do that because the data may change I might end up with four columns for A, two for B, etc. I know however that a, b and c will stay where they are.
I also don't know the order of A, B and C. There could be two A's followed by two C's and then one B.
My final result will be a table containing all the totals:
Aa Ab Ac
Ba Bb Bc
Ca Cb Cc
Where in the previous example would mean that Aa = 1 + 2 + 3 = 6, Ab = 5, etc.
Something like that.
I think the way to go is for 1-1 (the total of Aa's) is to go through every column in the first row. Check if it is an A. If it is, then get the value of the same column but second row. Add it to the total. When gone through all the columns, show up the total in 1-1.
What I have so far (for A):
Sub getA()
Dim x As Integer
Dim total As Integer
'cols = Find number of columns with data in them
For x = 1 To cols
'cell = cell in Ax
If InStr(1, cellvalue, "a") = 1 Then
'val = value from row 5 in same column
total = total + Val
End If
Next
End Sub
But I don't really know how to proceed with the commented lines.
Finally, another thing I would like to know is how will these values be presented in their respective cells without any extra event being carried (button for example). They should just appear in their cells from the moment someone opens the spreadsheet.
Any help is greatly appreciated.
Thanks.
Just an FYI, this can be done using the SUMPRODUCT formula:
=SUMPRODUCT(($B$1:$J$1=D$9)*($A$2:$A$4=$C10)*$B$2:$J$4)
EDIT
To compare the first letter then use this formula:
=SUMPRODUCT((LEFT($B$1:$J$1,1)=D$9)*($A$2:$A$4=$C10)*$B$2:$J$4)
Are you looking for something like:
Function countletter(strLetter As String) As Double
Dim x As Double, y As Double, xMax As Double, yMax As Double
xMax = Range("A1").CurrentRegion.Columns.Count
yMax = Range("A1").CurrentRegion.Rows.Count
For x = 1 To xMax
For y = 1 To yMax
If Cells(y, x).Value = strLetter Then
countletter = countletter + 1
End If
Next
Next
End Function

Hierarchical auto-numbering (with three levels) in Excel

What if we have a three-level hierarchy and need to enumerate only "B"s (according to the pattern), but some "C-"s interfere with "B"s.
Problem: to get column B result from a given column A.
A+
B 1
B 2
С-
B 3
A+
B 1
С-
B 2
A+
С-
B 1
B 2
B 3
P.S. The task arose from the need to enumerate complex hierarchy in Excel.
Imagine that A - level 1; B - level 2; C - level 3. (with some abuse of logic in the example above as C- in the pattern goes after A, which in practice is usually not the case).
The simple case of two-level hierarchy is shown here.
Easiest would be to add in two intermediary helper columns, the first of which we'll call column C. Here, we will count only which "A+" we're on, like so [starting at C2; C1 is hardcoded as 1]:
=IF(A2="A+",A1+1,A1)
This will increment every time a new row has "A+" in column A.
Then column D will track the highest # reached so far, for that iteration in column C [starting at D2; D1 is hardcoded at 1]:
=IF(A2="A+",0,if(A2="B",B2,D1))
This will restart at 0 for each new "A+", and for each "B" it will take the value shown in column B. Then for each "C", it will simply repeat the value from the row above (the previous "B" reached).
Finally you can put in your sort, as follows [starting at E1]:
=IF(A1="B",B1,"")
This will show BLANK for either "A+" or "C", and will show the B-count if column A = "B".
I also went with a helper column (as much as I detest them personally) to show the row of each A+
Put this in C1: =ROW(A1)
Put this in C2: =IF(A2="A+",ROW(A2),C1)
It uses an expanding range with a rebasable starting point. Drag down as far as your data goes.
Put this in B2: =IF(OR(A2="C-",A2="A+"),"",IF(A1="A+",1,MAX(INDIRECT("B" & C2 & ":B" & ROW(A1)))+1))
Drag down as far as your data goes.
Hope that helps. Here are the results I received:
A+ 1
B 1 1
B 2 1
C- 1
B 3 1
A+ 6
B 1 6
C- 6
B 2 6
A+ 10
C- 10
B 1 10
B 2 10
B 3 10

How to sum constants if the values of a row contian a specific value in excel?

I have the following row in excel:
12 4 12p 12a 12b
I need to sum this elements with their values from the legend.
12 = 12;
4 = 4;
12p = 12,5;
12a = 12,2;
12b = 12,3;
For example
=12 + 4 + 12,5 + 12,2 + 12,3
Any ideas?
If you have all the elements within one cell as a single string of text, the optimal approach would be to start by using text-to-column to split them up. So you'll have 12 in A, 4 in B, 12p in C, 12a in D, 12b in E. If that's not an option, I can show you string manipulations that can be an alternative.
You'll need to turn your "legend" into a look-up table, (perhaps on sheet2?), with column A having: p, a, b, etc.. and column B having the relative values.
Once that's done, place this formula on sheet1, in F column:
=A2+IFERROR(VLOOKUP(RIGHT(A2),Sheet2!$A:$B,2,FALSE),0)
Then drag it to the right 5 times, and it will have the values of the elements "translated".
You can sum the translated range easily.

Finding SUM except some columns

Below is what I have.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ A + B + C + D + E + F + G + H + I + J + K +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 50 +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
To find the SUM of this row excluding Cell E I am using formulae as below.
=SUM(A1:D1)+SUM(F1:J1)
NOTE : Cell K have SUM.
Is there any alternate way??
Above is just an example however in my sheet there are around 100 columns and I need to exclude around 10-15 columns.
If you add a row like this:
Row2: yes yes yes yes no yes yes yes yes yes no ...
(being columns A:K)
Then the answer could be
=SUMIF(2:2,"yes",1:1)
I believe
=SUM(A1:J1)-E1
conveys your intention more clearly. (provided the excluded columns contain numbers, of course)
I do think that this is an alternate way
=SUM(A1:D1;F1:J1)
The common approach to address this problem in Excel is to apply the SUM function to the entire range and to substract the exception list out of it, again with the SUM function, this time with a discrete list of individual cells to sum:
=SUM(<range to sum>)-SUM(<exception cell 1>, <exception cell 2>, ...)
This applied to your current example of ~100 cells to sum with say 5 exceptions:
=SUM(A1:CV1)-SUM(E1,M1,Y1,AB1,BU1)
If you want to get your Excel spreadsheet more flexible to those columns exclusion, you may consider to define a named range on the discrete selection of your columns to exclude. If you use it in the substraction SUM formula, you will avoid to change the formula when additional columns need to be excluded : you will only need to change the named range specification.
=SUM(A1:J1)-SUM(A1:J1 Exclusion_List)
where Exclusion_List is a named range on the distinct columns to exclude

Resources