Set formula in cell using VBA

Arie Bos

Board Regular
Joined
Mar 25, 2016
Messages
224
Office Version
  1. 365
Platform
  1. Windows
I want to enter the following formula's in two cells:

Code:
    Range("D61").Select
    ActiveCell.FormulaR1C1 = "=SUM(D58:D60)"
    Range("D63").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(IG_TxnHist[ProfitAndLoss];"">0"")-1"

the result in D61 is =SUM('D58':'D60') and returns a #NAME ? error. How to avoid the apostrophs around the range reference?

the second one returns an 1004 error: "Application defined or object defined error", but I do not see why.

Any help is greatly appreciated.

Arie
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
.
I'm confused ... you are selecting either D61 or D63, then pasting a formula in A1 both times. ???
 
Upvote 0
Try
Code:
    activecell.Formula = "=SUM(D58:D60)"
 
Upvote 0
Try this.
Code:
Range("D61").FormulaR1C1 = "=SUM(D58:D60)"
Range("D63").FormulaR1C1 = "=COUNTIF(IG_TxnHist[ProfitAndLoss],"">0"")-1"
 
Upvote 0
You are right, I made a mistake with the R1C1.
Thanks for the help, both formulas work fine now!
 
Last edited:
Upvote 0
.
Oh geez ... I need more coffee.

Never mind. I'l be in the corner ... drooling.

:eek:
 
Last edited:
Upvote 0
It works for me if I replace the ; with a , but I suspect that that is a regional setting.
 
Upvote 0

Forum statistics

Threads
1,215,394
Messages
6,124,683
Members
449,180
Latest member
kfhw720

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