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

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Have you tried using .Formula instead of .FormulaArray ?

eg;

Code:
Cells(1, 1) = 105.36
Range("A2").Formula = "=A1/2.36"
 
Upvote 0
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.
 
Upvote 0
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 ...
 
Upvote 0

Forum statistics

Threads
1,213,504
Messages
6,114,016
Members
448,543
Latest member
MartinLarkin

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