Nested 'For' cycle required - vba

I have four cells in an Excel workbook:
A1
A2
A3
A4
A1 and A2 cells include starting values such as 5 and 7. A3 has an formula and evaluates a result using A1 and A2's values. A4 cell has a target value. An iterational operation continue up to A3 cell's value equal to A4's 0,0001 approximation. For each A1's differential increment A2 will change depending on its range.
Can anybody help me with VBA including nested 'For' cycles?
My sample workbook:

There is an add-in with excel that is built to do this, it's called the solver addin.
There's a good tutorial here that explains how it works.

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))

For Excel VBA, If B2 value did not change then A2 will keep same, If B2 is changed then A2 will increase by one

I need a VBA for the whole column B and A says that
if Value of Cell B2 did not change then A2 value will be the same
if B2 value is changed then A2 value will increase by one
Use this formula.
Put your staring number in A1.
Put this in A2:
=IF(B2<>B1,A1+1,A1)
And copy down.

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

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!!

How to tell excel automatically add value in a cell of a sheet when the value is added in another cell of another sheet? [duplicate]

This question already has an answer here:
How to make excel automatically fill a value in a cell of a sheet when another corresponding cell of another sheet is filled the same value?
(1 answer)
Closed 8 years ago.
I have cells A1 and A2 in sheet1 with values 3, 4 respectively. I link the cell A1 and A2 to B1 and B2 in sheet2 respectively. I mean when A1 and A2 change, B1 and B2 change accordingly.
This is what I want to manipulate my spearsheet: When I add value 5 in cell A3 in sheet1, value 5 is also added in cell B3 in sheet2.
Without filling =A3 in B3 or using autofill, how can I do so?
Imagining that each time when you make a report you have to add a lot new cells -- A4 to A100 --, that the number of cells is varied each time, and that you have a lot of corresponding sheets to sheet1 -- sheet2 to sheet100 -- you will find why I need your help.
The best solution to your problem is to select the sheets on which you want the values to appear in corresponding cells by holding down the control key and clicking on sheet tab. So when you make an entry on Sheet1 in any cell the same appears in the corresponding cell of another sheet (s) selected.

Copy and paste column data based on date specified

In an Excel worksheet, cell C1 contains a date. I would like a macro that checks the date in C1 and if the same date is mentioned in row 3, then copy all matching date data beneath it from D6 downwards, paste to Sheet2, cell B3 and also copy column A downwards from row 6 to Sheet2, cell A3.
Use Excel's macro recorder to record your keystrokes and mouse actions (as Excel actions) then inspect the resulting macro. Using Excel's help, check the topic Create a macro or visit http://office.microsoft.com/en-us/excel-help/create-a-macro-HP005204711.aspx. This will get you started.
are you checking the date in C1 against any column in row 3? or in D3? or C3? It is not clear.
copy A6 down and D6 down, but switch column D to B on sheet2 and A to C on sheet2?
This could be easy if it were clearer what you are checking.