Excel: Adding a row disrupts formula - excel

In a spreadsheet I use for cash management tracking, I have the following formula:
=IF(D176="Cash", F175+C176, IF(D176="Transfer", F175+C176, F175))
When I add a row, I use control+D to fill in the formula from the cell above (I'm using Excel for Mac 2011). This results in the correct formula as follows:
=IF(D177="Cash", F176+C177, IF(D177="Transfer", F176+C177, F176))
However, this has the effect of changing the formula in the cell in the row below:
=IF(D178="Cash", F176+C178, IF(D178="Transfer", F176+C178, F176))
Here you can see the rows for column F are not correct: F176 should be F177.
Can anyone offer any advice to ensure that when I insert a row the formula remains intact?
Thanks.

Replace all of the references to F175 in the original formula (the one if row 176) with INDEX(F:F, ROW()-1).
=IF(D176="Cash", INDEX(F:F, ROW()-1)+C176, IF(D176="Transfer", INDEX(F:F, ROW()-1)+C176, INDEX(F:F, ROW()-1)))
'or better as
=INDEX(F:F, ROW()-1)+(OR(D176={"Cash", "Transfer"}*C176)

Related

How to only run a formula if the cell is blank

I am writing a formula to run a matching analysis on some elements of a spreadsheet. The column containing the formula already has values filled in, but I need to run the formula on the blank cells. The formula is working but overwriting the current info I have within the filled cells.
I have tried a VBA code which got too complicated and did not work. With VBA I tried inserting the formula into only the blank spaces but was having some issues. I have also tried a basic excel formula.
=IF(COUNTIFS(A:A,A2,BU:BU,"CLOUD")=0,"Not Cloud",IF(COUNTIFS(A:A,A2,BU:BU,"NOT CLOUD")=0,"Cloud","Hybrid"))
VBA Code I tried:
If (IsEmpty(Bucket) Or Bucket = "" Or Bucket = vbNullString) And _
(GetCellValue(.Cells(i, "BU")) = "CLOUD") Then
Range("CC:CC").Formula = "=IF(COUNTIFS(A:A,A2,BU:BU,""CLOUD"")=0,"Not Cloud", _
IF(COUNTIFS(A:A,A2,BU:BU,"NOT CLOUD")=0,"Cloud"","Hybrid"))"
A reminder this is a high-level version
I want that formula to only run in the cells that do not have a value already in place. It should not overwrite the data from the VBA script that I previously ran before doing the formula. The disposition column should be completely full of values. The formula needs to run in only the blank cells.
You could achieve this with an additional column.
In the column to the right of Disposition (insert one if necessary) write the following formula (example for row 2, assuming Disposition is Column C):
=IF(C2=""; [Your formula here]; C2)
This will provide you with a column full of values, anytime you put a value into column C it will override the formula.
Edit: You can then copy this formula down the entire column to achieve the desired effect.

How to dynamically access column properties in a With Range statement for conditional formatting

I am trying to write a script to take an easy look into my data. The data is structured as follows:
Row 1: Parameter Name
Row 2: Lower Limit
Row 3: Upper Limit
Row 4: Unit
Row 5 and below: data (can go up to a couple thousands lines and couple hundred columns).
The thing I want to achieve is a script which formats each cell from row 5 down, to color green if it is in between the limits, and to color red if it is not. Each cell should look to it's own column row 2 and 3 for the limits.
I have tried going cell per cell, or column by column. Both worked fine on smaller datasets, but showed problems (excel freezing and eventually closing) on bigger datasets.
I am now trying to format a complete range (because excel has no problems when I do a big range in one piece by hand whatsoever) at once, but I can't access the individual column properties.
The code I am using:
With formatRange
.FormatConditions.Delete
.FormatConditions.Add Type:=xlCellValue, Operator:=xlBetween, Formula1:="=" & Cells(2, formatRange.Column).Address, Formula2:="=" & Cells(3, formatRange.Column).Address
.FormatConditions(1).Interior.Color = RGB(0, 249, 49)
End With
Now say my range is from A5:B10.
formatRange will be A5:B10.
I would expect every cell from A5:A10 to compare their values against A2 and A3. This is indeed the case
But the cells of B5:B10 also compare their values against A2 and A3.
So my question is, is there a scalable way I can make range B5:B10 look at B2 and B3 instead?
Edit
The answer of #Ryan B. is an easy and correct way of doing it by hand.
The problem in vba turned out to be the following:
Formula1:="=" & Cells(2, formatRange.Column).Address would ultimately result in Formula1:="=$A$2"
As suggested by the accepted answer, this needed to change to Formula1:="=A$2" to work.
The solution I found was to create a function which cut of the first character, and create the correct formula this way.
This can be done by tricking Excel's absolute and relative referencing within conditional formulas. No VBA is going to be required. Here is a quick mock of how I understand your data:
Select the first cell of your 'Row 5' range -- where you're going to apply the conditional formulas.
Open the conditional formatting dialog from the Styles button group in the Home Ribbon (this is B5 in my mock-up),
Create a 'New Rule'
Choose 'Use a formula to determine which cells to format' rule type
Begin typing the following rule. You will have to be quite careful and avoid all use of the arrow keys. If you need to get to a different point in the formula, use your mouse to move the insertion point. Make the proper adjustment in your formula if that initial column isn't column 'B' in your worksheet
=AND( B5 >= B$2, B5 < B$3)
Pay special notice to the Dollar Signs. There are NO absolute references used for the target cell, B5. There are absolute references (that's the dollar sign) in front of the row numbers for Lower and Upper, but not on the columns.
Set you're desired look for the "in-bounds" formatting and select OK.
Repeat the steps for your "out-of-bounds" formatting. Use the formula
=OR( B5 < B$2, B5 >= B$3)
Finally, to apply the formulas to your entire range:
Select the cell with the prepared formulas (B5 in this example) and hit [ctrl] + [c] to put excel into cut/copy mode
Select the entire target range
Right click and take 'Paste Special' from the context menu
Paste as formats
And your formatting should propagate through the worksheet.
Hope it helps. Always experiment on a copy of your workbook :)

Simple VB ststment deconstruction

Could somebody please explain this simple vba statment to me? I just want to know what each part is referring to, and basically what this statement is accomplishing within my workbook. Thank you
ActiveCell.FormulaR1C1 = "='Bren Template'!R[-3]C[-6]"
This is a cell reference. In the ActiveCell (the one chosen), it will put the formula ='Bren Template'!R[-3]C[-6]. The formula breakdown is "Bren Template" is a reference to a sheet with that name. The R[-3] refers to three rows ABOVE the active cell. The c[-6] refers to three columns to the LEFT of the active cell.
So, if the active cell is H5 that formula will read ='Bren Template'!B2
If your active cell is I6, then the formula will read ='Bren Template'!C3
Note: The r[-3] and c[-6] will be "translated" from R1C1 style (i.e. Row 1 Column 1) when the formula is actually set in the cell.

Conditional formatting of a column based off having any text in the first column of the same row

I'm not sure if this is even possible without going to VB, but I was trying to do it through conditional formatting. Basically I have a column (Column K) that will always be the same value (345) if there is a record entered in that row. Basically when I populate my reports I simply want the value (345) to be entered into Column K if there is any data in that row. I was trying to just use Column A as a reference. I was messing with =IF(ISTEXT(Col.A location),"345","") but that's getting nowhere. So, I'm looking for ideas outside of vba, but if there are no possibilities then vba is the way to go I suppose. :)
Assuming your data is in columns A to J, and that it starts in row 2, enter this in K2 and copy down as necessary:
=IF(COUNTA(A2:J2),345,"")
Edit: For a conditional formatting formula you don't need the "If" part, because the formatting is already ... conditional:
=COUNTA(A2:J2)
Will this work?
=IF(ISBLANK(A1),"","345")
This code works to tell whether column A has something in it or not COUNTA(INDIRECT("$A$"&ROW()))>0, but I don't think you can set the value of the cell using conditional formatting. But with conditional formatting you have to know ahead of time how far down your data is going to go unless you just put it in all the rows.
Why don't you just put it in your VBA code when you are copying, you can find out what the last row is then put the IF() formula in. You can use this code:
Dim r1 As Range
Set r1 = Range("K1")
r1.NumberFormat = "General"
r1 = "=IF(COUNTA(INDIRECT(""$A$""&ROW())>0,""345"","""")"
r1.AutoFill Destination:=Range(r1, r1.Offset(200))

extract the unique items

Col A,Col B,Col C
Test1,Test1,Test3
Test2,Test1,Test3
Is there a way to go thru all cells and pick out the unique values and place them in a new column or something?
Most of the answers on Getting unique values in Excel by using formulas only only work for values in a single column.
For a solution that works for values in multiple rows and columns, I found the following formula very useful, from http://www.get-digital-help.com/2009/03/16/unique-values-from-multiple-columns-using-array-formulas/
Oscar at get-digital.help.com even goes through it step-by-step and with a visualized example.
1) Give the range of values the label tbl_text
2) Apply the following array formula with CTRL + SHIFT + ENTER, to cell B13 in this case. Change $B$12:B12 to refer to the cell above the cell you enter this formula into.
=INDEX(tbl_text, MIN(IF(COUNTIF($B$12:B12, tbl_text)=0, ROW(tbl_text)-MIN(ROW(tbl_text))+1)), MATCH(0, COUNTIF($B$12:B12, INDEX(tbl_text, MIN(IF(COUNTIF($B$12:B12, tbl_text)=0, ROW(tbl_text)-MIN(ROW(tbl_text))+1)), , 1)), 0), 1)
3) Copy/drag down until you get N/A's.
If you are using Excel 2007 at least, then you can just use Remove Duplicates function from Data tab.
Otherwise I think a little bit of VBA fairy dust sprinkling is in order. I can mash up a quick VBA script if you need it.

Resources