How to enter formulas in to cells using a macro

ste33uka

Active Member
Joined
Jan 31, 2020
Messages
471
Office Version
  1. 365
Platform
  1. Windows
Hi would anyone have a vba that would send theses formulas to cells for multiple sheets?
my workbook has 50 pages named,1,2,3,4,5,6 and so on
Thanks

J4 would be =IF(C4<>"",INDEX(Comments!$A$2:$A$22222,MATCH(C4,Comments!$N$2:$N$22222,0)+3))
J8 would be =IF(C8<>"",INDEX(Comments!$A$2:$A$22222,MATCH(C4,Comments!$N$2:$N$22222,0)+3))
J12 would be =IF(C12<>"",INDEX(Comments!$A$2:$A$22222,MATCH(C4,Comments!$N$2:$N$22222,0)+3))
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
try this code:
VBA Code:
Sub test()
Dim shtn As String

For i = 1 To 50
shtn = i
With Worksheets(shtn)
.Range("J4").Formula = "=IF(C4<>" & Chr(34) & Chr(34) & ",INDEX(Comments!$A$2:$A$22222,MATCH(C4,Comments!$N$2:$N$22222,0)+3))"
.Range("J8").Formula = "=IF(C8<>" & Chr(34) & Chr(34) & ",INDEX(Comments!$A$2:$A$22222,MATCH(C8,Comments!$N$2:$N$22222,0)+3))"
.Range("J12").Formula = "=IF(C12<>" & Chr(34) & Chr(34) & ",INDEX(Comments!$A$2:$A$22222,MATCH(C12,Comments!$N$2:$N$22222,0)+3))"
End With
Next i
End Sub
 
Upvote 0
Solution
try this code:
VBA Code:
Sub test()
Dim shtn As String

For i = 1 To 50
shtn = i
With Worksheets(shtn)
.Range("J4").Formula = "=IF(C4<>" & Chr(34) & Chr(34) & ",INDEX(Comments!$A$2:$A$22222,MATCH(C4,Comments!$N$2:$N$22222,0)+3))"
.Range("J8").Formula = "=IF(C8<>" & Chr(34) & Chr(34) & ",INDEX(Comments!$A$2:$A$22222,MATCH(C8,Comments!$N$2:$N$22222,0)+3))"
.Range("J12").Formula = "=IF(C12<>" & Chr(34) & Chr(34) & ",INDEX(Comments!$A$2:$A$22222,MATCH(C12,Comments!$N$2:$N$22222,0)+3))"
End With
Next i
End Sub
Thanks but result is
=@IF(C4<>"",INDEX(Comments!$A$2:$A$22222,MATCH(C4,Comments!$N$2:$N$22222,0)+3))
excel adds @ at beginning
 
Upvote 0
Are you using xl 365? if so use Formula2 rather than Formula
 
Upvote 0
Thanks offthelip and fluff, code works when i add formula2.
 
Upvote 0
As you are using 365 you might want to update your profile. ;)
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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