Use a Range variable in FormulaR1C1

citroendealer

New Member
Joined
Sep 27, 2016
Messages
13
Why does the following code results in a error?
(The code is a simplified version of my code, in reality I want a much bigger formula.)

Code:
Sub test()


Dim a As Range


Set a = Selection
Range("A1").Select
ActiveCell.FormulaR1C1 = "=MAX(" & a & ")"


End Sub
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try:

Code:
Sub test()
Dim a As Range
Set a = Selection
Range("A1").Formula = "=MAX(" & a.Address & ")"
End Sub
 
Upvote 0
This works but now I can't use the R1C1 notation. I already explained that my formula is much bigger...

Code:
ActiveCell.FormulaR1C1 = "=IF(MAX(-MIN(" & psrange & ");MAX(" & psrange & "))<" & maxps & "*0,1;1;IF(ABS(RC[-1])>ABS(RC[-2])*2;0;IF(ABS(RC[-2])>ABS(RC[-1])*2;0;1)))"
 
Upvote 0
This?

Code:
Sub test()
Dim a As Range
Set a = Selection
Range("A1").FormulaR1C1 = "=MAX(" & a.Address(ReferenceStyle:=xlR1C1) & ")"
End Sub

If it's too much to write each time and you must use R1C1 for whatever reason, variable a can be a string with the address.
 
Last edited:
Upvote 0
I don't understand what is happening with my code. See code below:
line 1: is the same code you provided 'Sheetspread' and it works!
line 2: this line gives an error but I didn't do anything different...
line 3: this line works without the equals (=) sign, when I put the equal sign in front of the formula in the spreadsheet it works. But when I add the equal sign to the code it doesn't work...

The range a is two fields with a positive and a negative number and I need the maximal absolute value of these two.

Code:
Sub test()


Dim a As Range
Set a = Selection
'line 1
Range("A1").FormulaR1C1 = "=MAX(" & a.Address(ReferenceStyle:=xlR1C1) & ")"
'line 2
Range("A1").FormulaR1C1 = "MAX(-MIN(" & a.Address(ReferenceStyle:=xlR1C1) & ");MAX(" & a.Address(ReferenceStyle:=xlR1C1) & "))"
'line 3
Range("A1").FormulaR1C1 = "MAX(-MIN(" & a.Address & ");MAX(" & a.Address & "))"


End Sub
 
Upvote 0
Assuming your comma separator is right for your region then possibly....


Code:
Sub test()


Dim a As Range
Set a = Selection
'line 1
Range("A1").FormulaR1C1 = "=MAX(" & a.Address(ReferenceStyle:=xlR1C1) & ")"
'line 2
Range("A1").FormulaR1C1 = "=MAX(-MIN(" & a.Address(ReferenceStyle:=xlR1C1) & ");MAX(" & a.Address(ReferenceStyle:=xlR1C1) & "))"



End Sub

The above is just changing your syntax, I haven't tested your actual formula.
 
Last edited:
Upvote 0
I don't understand what is happening with my code. See code below:
line 1: is the same code you provided 'Sheetspread' and it works!
line 2: this line gives an error but I didn't do anything different...
line 3: this line works without the equals (=) sign, when I put the equal sign in front of the formula in the spreadsheet it works. But when I add the equal sign to the code it doesn't work...

The range a is two fields with a positive and a negative number and I need the maximal absolute value of these two.

Code:
Sub test()


Dim a As Range
Set a = Selection
'line 1
Range("A1").FormulaR1C1 = "=MAX(" & a.Address(ReferenceStyle:=xlR1C1) & ")"
'line 2
Range("A1").FormulaR1C1 = "MAX(-MIN(" & a.Address(ReferenceStyle:=xlR1C1) & ");MAX(" & a.Address(ReferenceStyle:=xlR1C1) & "))"
'line 3
Range("A1").FormulaR1C1 = "MAX(-MIN(" & a.Address & ");MAX(" & a.Address & "))"


End Sub

Here's my test:


Excel 2010
CD
4-7-1
5-23-77
6-7-80
74012
8-88-37
9-13-20
1042-14
1126-29
12313
13-114
14-77-52
1512-14
16-6-45
17330
181651
19-224
Sheet5


Code:
Sub test()


Dim a As Range
Set a = Selection
Range("A1").FormulaR1C1 = "=MAX(" & a.Address(ReferenceStyle:=xlR1C1) & ")"
Range("A2").FormulaR1C1 = "=MAX(-MIN(" & a.Address(ReferenceStyle:=xlR1C1) & "),MAX(" & a.Address(ReferenceStyle:=xlR1C1) & "))"
Range("A3").Value = "=MAX(-MIN(" & a.Address & "),MAX(" & a.Address & "))"


End Sub


Excel 2010
A
151
288
388
Sheet5
Cell Formulas
RangeFormula
A1=MAX($C$4:$D$19)
A2=MAX(-MIN($C$4:$D$19),MAX($C$4:$D$19))
A3=MAX(-MIN($C$4:$D$19),MAX($C$4:$D$19))


I just changed the ;s to commas (regional) and got rid of the R1C1 in your third formula--you can't mix A1 and R1C1. Mark noticed these too.
 
Upvote 0

Forum statistics

Threads
1,215,884
Messages
6,127,564
Members
449,385
Latest member
KMGLarson

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