VBA code to copy exact formula with some change

liiamra

Board Regular
Joined
Mar 31, 2013
Messages
50
Hello Everyone!
This is the first time for me to ask for assistance regarding excel! and so I don't know which forum is the most helpful- I have tried one forum yesterday but no one gave me not even a hint- so I hope I get the required assistance here.

I have an excel sheet with relatively large data set. I have a formula with locked and unlocked reference cells: I want to be able to copy the formula down - so that the unlocked references change; Now the problem is that I want to copy the exact formula to the right of the second cell from top, then change the locked reference cells to the next one below. For example: If the formula in C1 is $A$5+B1, then I copy it down so I have in C2 $A$5+B2, C3: $A$5+B3.... now I want to copy the exact formula in C2 to D2 but changing the locked reference to the one below so I have in D2: $A$6+B2; afterwards I copy the formula in the second populated cell in D coloumn to E so I have in E3:$A$7+B3 and so on. Below is a table for illustration:

ABCDEF
1$A$5+B1
2$A$5+B2$A$6+B2
3$A$5+B3$A$6+B3$A$7+B3
4$A$5+B4$A$6+B4$A$7+B4$A$8+B4

<tbody>
</tbody>




Any help is highly appreciated.

Thanks and all best//
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi liiamra and welcome to the forum,

Perhaps try this in C1 and drag/copy where required:
=INDEX($A:$A, 4 + COLUMNS($B1:B1)) + $B1

Alternatively, using an IF statement you can just drag the formula in C1 across to F1 and down to F4, something like this:
Code:
=IF(
   COLUMNS($B1:B1) <= ROWS(B$1:B1),
   INDEX($A:$A, 4 + COLUMNS($B1:B1)) + $B1,
   "")

Note:
 
Upvote 0
Hey CircledChicken!

Many many thanks- the index thing works like a charm! but is't possible to automate the process to definite number - for example a VBA code that runs 50 times and that is filling 50 columns?

To be more specific, is there a VBA code that fills the formula to the right cell below, for example it fills the formula from c2 to c50 first, then copy the formula to d3, fill to d51 and then copy to e4 - fill to e52 and so on?

Thanks a million mate!

P.S I am going to update the post there! telling that I have got the required assistance in here; I guess they wouldn't like it :D but going to do it anyways.
 
Last edited:
Upvote 0
You're welcome - thanks for the feedback!

The VBA version is possible but I'm not sure why you would want it. Here's an example:
Code:
Sub example()

Dim oRng As Range
Dim i    As Long

Set oRng = Range("C1:C50")
oRng.Formula = "=INDEX($A:$A, 4 + COLUMNS($B1:B1)) + $B1"
oRng.Copy

For i = 1 To 49
    oRng.Offset(i, i).PasteSpecial
Next i

Application.CutCopyMode = False

End Sub
Note that this starts in C1 as per your original post rather than C2 but you can adjust the ranges/formula as appropriate.
 
Upvote 0
You rock mate! :)

I modified it according to my needs and it worked perfectly - perfect answer and assistance!

I needed to automate the process because I had to copy the formula to thousands of cells - now it is solved :)

Thanks a million mate- I do really appreciate your help :) now and always.

You have a good day!
 
Upvote 0

Forum statistics

Threads
1,214,782
Messages
6,121,532
Members
449,037
Latest member
tmmotairi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top