How to replace an empty cell in formula with last numbered cell - vba

Working on a budget worksheet. I am trying to get the cell to keep looking up the column for one part of the formula if the previous cell is blank.
I currently have the formula to give a blank cell in the 'Remaining' column (E) if the 'Budgeted' column (C) is blank, as seen here
Budget clip
The issue comes when trying to get a number in the 'Remaining' column when the previous cell above is blank. I would like it to keep looking up the column until it finds a number, then use that number in the equation.
So in the picture, for Expense 5 (A9) I would like it to take cell E6-C9.
And then repeat the formula down the column.
I would like to copy the formula down the column since filled and unfilled cells will change from month to month.

Try this in cell E9
=IF(C9>0,$C$3-SUM(C$5:C9),"")
I think this will serve the same purpose as calculating E6 - C9 (i.e. calculating how much of the budget is remaining), but it's simpler code.
You should be able to copy it from E5 to the bottom of your table using regular copy-and-paste.

Related

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.

Highlight X number of cells based on numeric value

I'm creating a vacation tracker in excel. The first sheet pulls the data from SharePoint which has Start Date, End Date and a Date Difference calculation.
Sheet1
A2=12/16/2015
B2=12/20/2015
C2=5
The second sheet is the visualization of the data. It starts with cell B1 and goes out for 90 days. It is a word representation of the date. Here is the formula I'm using =UPPER(TEXT(B2,"DDD"))
Sheet2
B1=WED
C1=THU
D1=FRI
E1=SAT
F1=SUN
The next row always B2 has a formula which is always today's date. From there I add one date to increment the dates out to 90 days. B2=Today() and the other cells =B2+1 and so forth
B2=16
C2=17
D2=18
E2=19
F2=20
What I'm trying to do is have the start date and end date highlighted on Sheet2. For example the boss is out on vacation from 12/16/2015 until 12/20/2015. I need the cells B3-F3 on Sheet2 highlighted in Red. The secretary who is on leave from 12/17/2015 to 12/19/2015 have her cells highlighted in another color from cells C4-E4.
This will probably take some VBA I just need a starting point. I tried using a VLOOKUP to pull the data, however the using a range from another sheet is not working. And to be honest I do not know if this is the correct approach.
Conditional formatting for B2:ZZ2 at Sheet2
=AND(B2>=Sheet1!$A$2,B2<=Sheet1!$B$2)
And select the color you like

Dynamic calculation in Excel based on a cell value

I've got a challenge in Excel i hope you guys can solve for me.
I have a drop down list (weeks) where i select the week 2015-18 to 2016-17. (nr 1 in picture)
In the cell called LY (nr 2 in picture) I want that to type the result of a dynamic sum range, based on the weeks input.
Picture of setup of the text above
The calculation logic is:
If i select week 2015-20, the VBA or formula should sum(F5:F7)/(G5:G7).
So in other words, I want a dynamic calculation that starts from week 2015-18 (F5/G5) and then sums the values down to the value that i have selected in "Weeks"
Picture of the setup of the logic values
The value of this calculation should be shown in LY (first picture, nr 2).
I really hope you can solve this for me. I've got more rows to calculate, so if you could come up with a "global" code that works for that, it would be great
ok, so you do not need VBA for this.....
for the sake of easier updates; mark the weekrange, right click them and give them a name, with Define name
I called it yearlyweeks
The two drop downs lists, will contain the value corresponding to the text indicating that week.
So we can use this to get a cells address by value (if that text is unique in the range named yearlyweeks):
=ADDRESS(MATCH(H8,yearlyweeks, 0), 2)
where H8 is the cell address of a dropdownlist cell. 2 is the column index of the "B" column
this will result in something like:
$B$2
where the 2 indicates the relative row index, in the range, not the absolute row number.
we are really not interested in the column, or relative row, only the absolute row, but so far we can live with relative..:
=ROW(INDIRECT(ADDRESS(MATCH(H8,yearlyweeks, 0), 2)))
this will give you the relative row of the cell coresponding to what you selected on the drop downlist for one of the lists. So I would do this in two calculation cells, just to avoid the next piece getting too long..
lets let the calculation cells be in I10, and J10..
I10 hold the relative row for the beginning week
J10 for the ending week
below create a new set of cells with:
="F"&(I10 + 7)
and
="G"&(J10 + 7)
where 7 is the row offset of yearlyweeks
in yet one more cell J12 we make the range string:
=CONCATENATE(I11;":";J11)
and then does the calculation in the final cell:
=SUM(INDIRECT(J12))
Now I can do this as a one liner, but you really would hate that

Setting FormulaArray to Formula skips every other row and column

A very fast way to enter formulas is like this:
Range("E5:H10").Formula = "=A1"
The column and row references adjust, so that for example the formula this code enters in cell H10 is "=D6".
However, that doesn't work for array formulas:
Range("E5:H10").FormulaArray = "=A1"
That puts the array formula {=A1} in each of the cells; no adjustments for rows or columns.
Of course you can loop through all the cells and enter a unique array formula in each one, but that isn't fast.
So I tried this:
Range("E5:H10").Formula = "=A1"
Range("E5:H10").FormulaArray = Range("E5:H10").Formula
But the results were surprising -- the references skip every other row and every other column; for example:
-- The formula in F5 is {=C1} instead of the expected {=B1}, and
-- The formula in D6 is {=A3} instead of the expected {=A2}, and
-- The formula in H10 is {=G11} instead of the expected {=D6}.
WTF?? Why would it skip rows and columns like that?? BTW I'm in Excel 2007 SP3.
For now I'm doing this, which was the fastest way I found that worked:
Range("E5").FormulaArray = "=A1"
Range("E5:H10").FillDown
Range("E5:H10").FillRight
That works but is probably slower than the direct way would be. So -- why does setting the FormulaArray property to the Formula property skip referenced rows and columns as shown above?
The A1 formula is evaluated relative to the top left cell of the range you are populating. So A1 works fine in E5, but B1 is up 4 and left 3 from E5, so the formula entered into F5 refers to the cell up 4 and left 3, which is C1. This repeats for the other cells. The simple solutions is to use:
Range("E5:H10").FormulaArray = Range("E5:H10").FormulaR1C1
since that formula is the same for every cell.

Sum of unfixed number of cells if corresponding cell has given value

I am strugling how to make this happen. I have a worksheet where the data is going to be read in. The size of the data will vary from time to time. I have programmed a generated summation column after the data is read in. I want each cell to sum all values in the row, with the index value in the first row equal to the value in the first row in the summation column. The picture might give you a less abstract visualization of the case. I included the manual formulas for row 3 in row 12 as text. I want to do this in VBA. There might be up to 50 sets ([2010,2011,avvik] or [2010,avvik] or [2011,avvik]). There are two variables with a saved number (column number) for both the start and the end of the data area.
In other words; The money column under "SUM 2010" (which in my program actually is only 2010) should sum every cell in the given row, which has the value 2010 in row 1 in the same column. The same goes for 2011.
(You might want to save/open the picture for details)
You can do this with the formula SUMIF. For Q3, you would write:
=SUMIF(A1:P1, "2010", A3:P:3)
In layman's terms, you are saying, look at all the cells in the range A1 - P1 and for each one, if the value happens to be "2010", I want you to add the value in the range A3 - P3 to the sum.
BTW, you can also use this formula in cell Q11 to get the totals instead of the current formula you have. The fact that the year dates and numbers are in the same column make this really easy.