How to use variables decimal with formulas ? - vba excel

rodrigo_m_almeida

New Member
Joined
Jan 13, 2022
Messages
42
Office Version
  1. 2021
Platform
  1. Windows
Good afternoon people
could someone help me with this
I am trying
VBA Code:
Dim xPrice As Variant: xPrice = CDec(4.1)

Range("A1").FormulaR1C1 = "=" & xPrice & "+1.2"
Display Error 1004

How can I use decimal variable in formulas ?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Good afternoon people
could someone help me with this
I am trying
VBA Code:
Dim xPrice As Variant: xPrice = CDec(4.1)

Range("A1").FormulaR1C1 = "=" & xPrice & "+1.2"
Display Error 1004

How can I use decimal variable in formulas ?
That seems to work perfectly fine for me.

Are you sure there isn't something else going on?
Cell A1 isn't protected or part of a merge cell, is it?
Do you have any other VBA code in this block that might be interfering with it?
Perhaps some Event Procedure code?
 
Upvote 0
That seems to work perfectly fine for me.

Are you sure there isn't something else going on?
Cell A1 isn't protected or part of a merge cell, is it?
Do you have any other VBA code in this block that might be interfering with it?
Perhaps some Event Procedure code?
No and no, I'm trying - error 1004
 
Upvote 0
Are you using Excel 2021?

Can you post your ENTIRE block of VBA code?

When you get the error, does it offer a "DEBUG" button?
If you click that, exactly which line of code does it highlight?
 
Upvote 0
Are you using Excel 2021?

Can you post your ENTIRE block of VBA code?

When you get the error, does it offer a "DEBUG" button?
If you click that, exactly which line of code does it highlight?
Yes I am.
This is all my code
My range
 

Attachments

  • Captura de tela 2023-03-17 160111.png
    Captura de tela 2023-03-17 160111.png
    82.9 KB · Views: 5
  • Captura de tela 2023-03-17 160154.png
    Captura de tela 2023-03-17 160154.png
    15.8 KB · Views: 5
Upvote 0
Hmmm... I don't see what it would doing that, and I cannot reproduce that. Don't know if it is a language thing.

However, why are you using the "CDEC" function? That is usually used to convert a string to a decimal. But 4.1 is already a decimal, so I don't think that is necessary.

Try this out and see what happens:
VBA Code:
Sub Test()

Dim xPrice As Single
xPrice = 4.1

Range("A1").Formula = "=" & xPrice & "+1.2"

End Sub
 
Upvote 1
Solution
Hmmm... I don't see what it would doing that, and I cannot reproduce that. Don't know if it is a language thing.

However, why are you using the "CDEC" function? That is usually used to convert a string to a decimal. But 4.1 is already a decimal, so I don't think that is necessary.

Try this out and see what happens:
VBA Code:
Sub Test()

Dim xPrice As Single
xPrice = 4.1

Range("A1").Formula = "=" & xPrice & "+1.2"

End Sub
I'm not getting
I Tried to do
Shows the same error :cry:
 
Upvote 0
If you are still getting an error, why did you mark it as a solution?
You only do that if the post solved your issue.

What is the name of the VBA module you have placed this VBA code in?
If you open up a brand new blank workbook, insert a new VBA module, place the code in it and try running it, does it work then?

If it does, then we know that the issue is specific to your particular workbook.
 
Upvote 0
If you are still getting an error, why did you mark it as a solution?
You only do that if the post solved your issue.

What is the name of the VBA module you have placed this VBA code in?
If you open up a brand new blank workbook, insert a new VBA module, place the code in it and try running it, does it work then?

If it does, then we know that the issue is specific to your particular workbook.
I can't edit my answer
your solution
It helped me, it worked 🙏
Thxxx !
 
Upvote 0

Forum statistics

Threads
1,215,201
Messages
6,123,617
Members
449,109
Latest member
Sebas8956

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