Insert formula multiple times using a variable set in a cell

tls123

New Member
Joined
Sep 23, 2014
Messages
7
This is my first post. I'm just a novice using VBA so I constantly look for help on this board.

I want to use VBA to insert a formula (for ease, let's say it's simply "=D2") several times in column A and the number of times is determined by a value in another cell (e.g. C1). Then below that list, insert another formula ("=D3") the same number of times.

Any help would be greatly appreciated.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

bbott

Well-known Member
Joined
Feb 5, 2010
Messages
2,350
Welcome to the board.

Depending on whether or not you want your formula to use absolute references, try each of these:

Code:
Sub FrmColA()
    
    Range("A1:A" & Range("C1").Value).Formula = "=D2"
    Range("A" & Range("C1").Value + 1 & ":A" & Range("C1").Value * 2).Formula = "=D3"

End Sub
Code:
Sub FrmColA2()

Range("A1:A" & Range("C1").Value).Formula = "=$D$2"
Range("A" & Range("C1").Value + 1 & ":A" & Range("C1").Value * 2).Formula = "=$D$3"

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,191,056
Messages
5,984,397
Members
439,884
Latest member
BrownEyedGirl

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
Top