FormulaR1C1 returning nothing when using a variable

Zem32619

New Member
Joined
Jul 2, 2021
Messages
29
Office Version
  1. 365
Platform
  1. Windows
Hi All.

I have a code that works perfectly fine for users with the US excel and windows setting. I'm stuck why FormulaR1C1 doesn't work as it should when ran in a German setting.
Here is the line of code that works for US setting.

Range("L2").FormulaR1C1 = "=(1-(RC[14])- " & ACoS & " ))*RC[-1]"

But it returns nothing, as in empty when ran in a German setting. I'm thinking if it is the variable thing "ACoS".

Would really appreciate any help. Thank you so much in advance.
 
... and commented out On Error Resume Next.
What I got now is an error "Run-time error '1004': Application-defined or object-defined error", and the highlighted line is below:
There's a lesson here. There is a place to use On Error Resume Next, but not as a blanket provision to avoid errors displaying. Who knows what your code is (or isn't) doing if you suppress error messages.

You should NEVER, NEVER, NEVER used reserved words (the name of existing functions, properties, methods, etc) as names of your variables, procedures, and user defined functions.
Thanks @Joe4 and @Michael M. Sound advice, but I don't think it's the problem here as ACos isn't a VBA function, and I think the OP ruled this out in Post #10.

@Zem32619, what happens if you enter, say, 3000 in the InputBox, so that ACoS is an integer 30?

I suspect that when you enter 30, as you've done previously, the problem is the difference between the English 0.3 and the German 0,3. Try to put the wrong one in a formula and it will throw a Run-time error 1004.
 
Upvote 0

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
@StephenCrump
I fully agree with you on the Acos....but in this instance it IS being used within a formula, which I believe, will still cause issues....Why take the risk ? As Joe and I have mentioned, eliminate ALL possibilities for errors !!
 
Upvote 0
@StephenCrump
I fully agree with you on the Acos....but in this instance it IS being used within a formula, which I believe, will still cause issues....Why take the risk ? As Joe and I have mentioned, eliminate ALL possibilities for errors !!
And it is a good practice to get into too!

The point about error handling is good too. I never liked "blanket" error handling.
If there is a possibility of a specific error you want to ignore, better to add error handling specific to that particular error code, and return all others.
And in debugging, temporarily comment out all error handling, if necessary, so you can see all errors.
 
Upvote 0
Thank you all for your insights. Well noted. @Joe4 & @Michael M already changes the variable name to other names as suggested.
@StephenCrump I still got a Run-time error 1004, same line of error. The variable is storing the right value in it but the formula is not calculating. You're right it is the difference of English 0.3 & German 0,3. When I put 0.3 in the formula instead of the variable, it calculates in L2. I'm in stuck how to deal with different separators as it seems it is the problem.
 
Upvote 0
Put ACoS into a cell and use the REPLACE function to change the value
Change this
VBA Code:
ACoS = (Application.InputBox(Prompt:="Enter Target ACoS.",  Type:=1) / 100)
 If ACoS = False Then
 Exit Sub
 End If

To this

VBA Code:
 Cells(1, 1) = Replace((Application.InputBox(Prompt:="Enter Target ACoS.", Type:=2)), ",", ".") / 100
If ACoS = False Then Exit Sub
 
Upvote 0
I'm in stuck how to deal with different separators as it seems it is the problem.

Did you try this:
@Zem32619, what happens if you enter, say, 3000 in the InputBox, so that ACoS is an integer 30?
If the code works when we eliminate the decimal point, this will provide more conclusive evidence that the separator is the problem.

If it is a separator issue, it's not immediately obvious why, given that your code sets:
VBA Code:
.DecimalSeparator = "."
.ThousandsSeparator = ","
.UseSystemSeparators = False

and that VBA treats the number as 0.3 and not 0,3. You may need to play around with this, as it's going to be difficult to replicate on our machines/settings. A quick patch may be in order, e.g. as in Michael M's post above.

Or perhaps: Range("L2").FormulaR1C1 = "=(1-(RC[14])-" & Replace(ACoS, ",", ".") & ")*RC[-1]"
(or is it the other way around?)
 
Last edited:
Upvote 0
I fully agree with you on the Acos....but in this instance it IS being used within a formula, which I believe, will still cause issues....Why take the risk ? As Joe and I have mentioned, eliminate ALL possibilities for errors !!

Thanks @Michael M, I totally agree, and adopt a conservative approach to variable names myself to minimise the scope for error.

But in this particular case, my comments about AcoS were based on:
- The fact that the Excel formula isn't using the ACOS function. Rather, the code is passing to Excel the value (0.3) of the ACoS variable.
- The OP having already changed the variable name ACoS, without success.
 
Upvote 0
@Zem32619, do you by any chance have your Use system separators turned off ?
File > More > Options > Advance.
My testing indicates your original Type1 setting will use the regional setting when assigning to the variable, but when the formula gets entered if the below is not turned on it will validate the formula using the Excel setting on entry and error out.

That is the only way I have been able to make if fail.

Note: You indicated you are entering 30%, the Type2 code method that has / 100 at the end will error out if you enter a %

1644552057788.png
 
Upvote 0
That is the only way I have been able to make if fail.
Thanks Alex

If I change my system number settings to German format (including decimal separator ",") , I can replicate and fix the problem like this:

VBA Code:
Sub Test()
    
    Dim d As Double
    d = 30 / 100

    Application.DecimalSeparator = "."  'My system is changed to ","
    Application.UseSystemSeparators = False
    Range("A1").Value = d   'displays as 0.3
    MsgBox Range("A1").Value 'System setting unchanged: 0,3
    MsgBox Range("A1").Text 'Excel display only: 0.3
       
    'This fails
'    Range("L2").FormulaR1C1 = "=(1-(RC[14])-" & d & ")*RC[-1]"
    
    'This works
    Range("L2").FormulaR1C1 = "=(1-(RC[14])-" & Replace(d, ",", ".") & ")*RC[-1]"
        
End Sub

which makes me think that when @Zem32619 said the message box was returning 0.3, he was actually "translating" from 0,3 for our benefit?

I had a play with a couple of other variations, e.g. reverting the system settings to my normal "." decimal separator, and having Excel display using "|". For coding purposes, the behaviour was quite idiosyncratic.
 
Upvote 0
@Zem32619, do you by any chance have your Use system separators turned off ?
File > More > Options > Advance.
My testing indicates your original Type1 setting will use the regional setting when assigning to the variable, but when the formula gets entered if the below is not turned on it will validate the formula using the Excel setting on entry and error out.

That is the only way I have been able to make if fail.

Note: You indicated you are entering 30%, the Type2 code method that has / 100 at the end will error out if you enter a %

View attachment 57531
Hi Alex,
Yes, the Use system Separator is turned on.
Thank you for your reply.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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