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

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
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,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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