Excel: A function to replicate built-in change in relative references when copying formulas - vba

I need a function that will do what Excel does automatically when you dreag a formula: change the referneces automatically.
For example:
In A1 I have "= A2 + A3"
If i copy this to say C3 it will have: "= C4 + C5"
I need to WRITE a formula in C3 that will produce this.
Any ideas? VBA solution is also welcome
CLARIFICATION:
In need this to be as general as possible.
Meaning A1 can contain ANY formula of any type, containing references to other cells.
for example: "= A2 + A3" or "= VLOOKUP(A2, $C$1:$E$7, 2, True)"
In need to move have this formula, whatever it is, copied to another cell (say C3), w/o the built in copy/paste, and have the references (that aren't set with $) change relatively.
I thought there might be a function to write in the destination file to do this.
I have tried writing an Eval function, and i managed to copy the formula from A1 and have it evaluated in C3, but the references would not change

This question lacks a bit of clarity, but I think this might be what you're after:
=SUM(OFFSET(C3,1,0,2))
This will sum the two cells directly below the given cell (in this case, cell C3). That is, it offsets C3 by 1 row, 0 columns, and grabs a height of 2 cells and then passes the result to the SUM function.

This VBA code would do what you are looking by setting the formula in the active cell:
ActiveCell.FormulaR1C1 = "=R[+1]C+R[+2]C"

You can use the Indirect() function using relative reference style.
For example, if you were in A1 and wanted to sum B1 & C1, it would look as follows:
A1: =INDIRECT("RC[1]",0)+INDIRECT("RC[2]",0)
That will change as you move the cell around to always sum the 2 cells to the left of the cell.
For your specific example (A1 = A2 + A3 || C3 = C4 + C5), it would be as follows:
=INDIRECT("R[1]C",0)+INDIRECT("R[2]C",0)
Hope that does the trick!!

Related

how to copy values in a diagonal down a range using vba

I have a range of data in one column like so:
and I'm trying to copy each value after the one in "A1" and move it one column over and up one row, for example (A2 copied to B1, A3 copied to B2, A4 copied to B3 etc.) like so:
Is there a few lines of code that can do this successfully?
In B1:
=IF(ISBLANK(OFFSET($A1,COLUMN()-1,0)),"",OFFSET($A1,COLUMN()-1,0))
fill down and across
Alternate:
=IF(ROW(B1)>COUNTA($A:$A)-COLUMN(A1),"",INDEX($A:$A,COLUMN(B1)+ROW(B1)-1))

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.

EXCEL VBA Countifs with dynamic range based on cell value [duplicate]

I'd like to know how to pull cell references from the value of another cell and insert them into a formula.
For a simple example:
In cell A1 I have this: COUNT(B4:H4)
Instead of choosing the range when I set up the formula, I'd like this range to be dynamic, based on the value in other cells.
So, I want to type in cell references: in cell B4, I'd type in C5, and in H4 I'd type in C8. How can I set up my formula to look inside cells B4 and H4, but then return the range that is defined by the values in these cells (C5:C8 in this case...)
So in the end, cell A1 would return the count of C5:C8
I hope that makes sense. It seems like something pretty simple but I can't seem to find info on it.
Use INDIRECT()
=SUM(INDIRECT(<start cell here> & ":" & <end cell here>))

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.

Trying to compare two strings in excel

I have something like this:
A1: 0069
B1: 030069
In every case, I need to check if A1 is equal to B1 if I were to ignore the 03. I have a lot of cells, and the 03 is just there because its the format of the data. However, A1 can be multiple different lengths, so I can't just use the MID formula and just check B1 after the 03 because I would need to make multiple cases depending on how long A1 is.
Originally, I get the A1 data by using the formula:
D1 =FIND("-",C1), where C1 is A1 in the format: 0069-XXXX-XX.
I then use:
A1 =LEFT(C1,D1-1), which gives me the number 0069 as you see above. This way, I'll always have the correct length when I actually start the real problem.
My question is, can I use some kind of concatenation to just add a 03 to A1 and then see if it is equal to B1. I've tried using & and CONCATENATE, but because they are in general terms, I get the #VALUE error. Maybe I'm just not typing it in correctly, but yea.
Thanks!
You can just use & correctly in A1:
="03" & LEFT(C1, D1 - 1)
To compare these values you could use a formula like below:
=IF(A1=RIGHT(B1;4);"true";"false")
Actually it is not necessary to use the formula in A1 nor D1, you can use your data from column C straight away:
=IF(LEFT(C1;4)=RIGHT(B1;4);"true";"false")
If the pattern is Column C is nnnn-XXXX-XX then no helper column (Columns A and D) is necessary:
=MID(B1,3,4)=LEFT(C1,4)