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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

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