How to insert formulas into cells using vba loops

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
I am really struggling with how to insert formulas in cells using VBA and loops and it would be very kind if someone can show me an example of how to insert different formulas in different columns.

I would like to learn so that I can use this in my workbooks.

Row1 contains headers from A1 to G1.

Row2 to row10 contains randbetween(0,10) in the Range(A2:E10)

I would like to use some kind of loop to insert the following formulas in these cells.

Code:
Formula#1 ="SUM(A2:A10)"

Formula#2 ="AVERAGE(A2:E10)"

Formula#3 = =SUMPRODUCT($B$2:$E$2;B2:E2)



I am trying to get formula#1 in column F in the range of F2:F10 and formula#2 in column G in the range of G2:G10 and formula#3 in column H.
 
Last edited:

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I think I have made some progress.

I can't figure out why this code doesn't work??

Code:
For i = 0 To 2 Step 1        
wk.Range("L3:L6").Formula = "=LARGE($J$3:$J$10,i)"
Next i

It gives me

Code:
=LARGE($J$3:$J$10;i)
.
 
Last edited:
Upvote 0
Code:
Dim i As Integer

For i = 1 To 5 Step 1
    wk.Range("L2:L6").Formula = "=LARGE($J$3:$J$10," & i & ")"
    'wk.Range("L2:L6").NumberFormat = "0.00"
Next i

This gives me
Code:
=LARGE($J$3:$J$10;5)

for all 5 cells. What am I doing wrong?
 
Upvote 0
You have to write it individually into each cell because the formula changes for each cell with your index i:
Code:
Dim i As Integer

For i = 1 To 5 Step 1
    Cells(i, 12) = "=LARGE($J$3:$J$10," & i & ")"
    'wk.Range("L2:L6").NumberFormat = "0.00"
Next i
End Sub
Note I always use number addressing when writing to cells specially when writng formula because one can use indices for rows and columns. Column 12 is column L
 
Last edited:
Upvote 0
Hi offthelip,

thank you for your reply! Your code works great!
 
Upvote 0
Or amend the formula so that it increments then you don't need to loop....

Code:
wk.Range("L2:L6").Formula = "=LARGE($J$3:$J$10,ROWS($A$1:$A1))"
 
Last edited by a moderator:
Upvote 0
Hi, thank you for your reply!

That's is very clever!

Is there anyway to use an array to hold the formulas and calculate the values in an array?
 
Upvote 0
like this?:
Code:
Sub test2()
Dim arr(1 To 5, 1 To 1) As Variant
For i = 1 To 5 Step 1
    arr(i, 1) = "=LARGE($J$3:$J$10," & i & ")"
    'wk.Range("L2:L6").NumberFormat = "0.00"
Next i
Range("L2:L6") = arr


End Sub
 
Upvote 0
Hi, yes but I was thinking about placing all of my formulas into an array, manipulate the array and then send the values back to the spreadsheet?
 
Upvote 0
How can I change a formula in a cell in my worksheet using an userform?

Code:
Private Sub CheckBox1_Click()

If CheckBox1 = True Then
     Sheets("Dash").Range("M279").Value = Sheets("Dash").Range("M279").Formula = "=(M277/365) * 1.1"
End If

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,385
Members
448,956
Latest member
JPav

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