VBA - to change Formula Reference.

stucamps

Board Regular
Joined
Jul 3, 2003
Messages
114
I am wanting to change the formula reference in a cell dependent on an OptionButton Selected.

Currently the formula in the spreadsheet is:
=IF(AND(ISNUMBER(D12),ISNUMBER(D22)),+D12-D22,"-")

but if I select an option button then I want to change the D12 to D13, as I want to select a different row for analysis, and I have tried the following code:
Code:
Private Sub cmdConfirm_Click()
If OptionButton1.Value = True Then
ThisWorkbook.Sheets(2).Range("d22").FormulaArray = "=IF(AND(ISNUMBER(D12), ISNUMBER(D22)),+D12-D22," - ")"

End If

End Sub
I even tried to change the formula in VBA to
ThisWorkbook.Sheets(2).Range("d22").FormulaArray = "=IF(AND(ISNUMBER(R13C4), ISNUMBER(R22C4)),+R13C4-R22C4," - ")"

Please note that D22 does not need to change
using the RxCx method but to no avail.
Any ideas on how I can change this?
Thanks
Stuart
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
Have you tried using .Formula instead of .FormulaArray ?

eg;

Code:
Cells(1, 1) = 105.36
Range("A2").Formula = "=A1/2.36"
 

stucamps

Board Regular
Joined
Jul 3, 2003
Messages
114
Thanks but that didnt solve anything either just a type mismatch error.
 

Fat Cat

Active Member
Joined
Nov 5, 2004
Messages
336
The bit it is objecting to is the " - " symbol at the end of the formula. If you change this to a number (eg: 0 ) the formula works and is pasted. If you leave it as blanl (eg: "") or as you have it " - " is throws a spanner.
 

just_jon

Legend
Joined
Sep 3, 2002
Messages
10,473
That's not an array formula, but that wasn't the problem --

ThisWorkbook.Sheets(2).Range("d22").Formula = "=IF(AND(ISNUMBER(R13C4), ISNUMBER(R22C4)),R13C4-R22C4," & """" & " - " & """" & ")"


Need a lotta quotes to get a real one ...
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,995
Messages
5,834,779
Members
430,321
Latest member
yemisimi11

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
Top