How to use formula or formulalocal in VBA

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
I am trying to use formula or formulalocal in VBA but the following code snippets gives me a runtime error.

Code:
Worksheets("Test").Range("F10").Formula = "=IF(B4="All countires","*",B4)"

Worksheets("Test").Range("F10").Formula = "=IF(B4="All countires";"*";B4)"

Worksheets("Test").Range("F10").FormulaLocal = "=IF(B4="All countires";"*";B4)"

Worksheets("Test").Range("F10").FormulaLocal = "=IF(B4="All countires","*",B4)"

What am I doing wrong?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
You need to double up the quotes inside the formula
Code:
Worksheets("Test").Range("F10").Formula = "=IF(B4=""All countires"",""*"",B4)"
 
Upvote 0
When putting quotes within quotes, you need to double them to let the compiler know that they are included in the string, and not delimiters:

Code:
Worksheets("Test").Range("F10").Formula = "=IF(B4=[COLOR=#ff0000]""[/COLOR]All countires[COLOR=#ff0000]""[/COLOR],[COLOR=#ff0000]""[/COLOR]*[COLOR=#ff0000]""[/COLOR],B4)"
 
Last edited:
Upvote 0
Also, I imagine that should be countries and not countires?
 
Upvote 0
Thank you for your reply Fluff! It works when I double up the quotes!
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0
How would I loop through a range and place formulas in them using VBA?

Code:
Sub test()
Dim rng as range
set rng = Rnge("A2:D10")

Worksheets("Test").Range("E2:E10").Formula = "=SUM(A2:D10)"

End Sub
 
Upvote 0
What's wrong with what you have?
 
Upvote 0
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 two different formulas in two different columns.

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

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

Say, formula#1 in column F in the range of F2:F10 and formula#2 in column G in the range of G2:G10.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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