Round variable to 4 decimal places

A

achidsey

Excel Experts,

In my code, I create a variable that I later incorporate into a forumula I
enter in a cell.

I only want to enter the variable with four decimal places. How would I
code this?

For example, my code is similar to,

Sub EnterBuyPrincipal( )

Dim TPrice As Variant

TPrice = 2220/850

Range("A2").FormulaR1C1 = "=100*" & TPrice

End Sub

The way it is now, the code enters "=100*2.61176470588235". I'm not trying
to change the number of decimals displayed, but rather the text of the
formula. I want it to read "=100*2.6118".

Thanks in advance,
Alan
 
R

Ron Rosenfeld

Excel Experts,

In my code, I create a variable that I later incorporate into a forumula I
enter in a cell.

I only want to enter the variable with four decimal places. How would I
code this?

For example, my code is similar to,

Sub EnterBuyPrincipal( )

Dim TPrice As Variant

TPrice = 2220/850

Range("A2").FormulaR1C1 = "=100*" & TPrice

End Sub

The way it is now, the code enters "=100*2.61176470588235". I'm not trying
to change the number of decimals displayed, but rather the text of the
formula. I want it to read "=100*2.6118".

Thanks in advance,
Alan

========================
Sub EnterBuyPrincipal()

Dim TPrice As Variant

TPrice = Round(2220 / 850, 4)

Range("A2").FormulaR1C1 = "=100*" & TPrice

End Sub
========================

or

==========================
Sub EnterBuyPrincipal()

Dim TPrice As Variant

TPrice = Application.WorksheetFunction.Round(2220 / 850, 4)

Range("A2").FormulaR1C1 = "=100*" & TPrice

End Sub
=========================

Check the MSKB for VBA Round vs Round worksheet function for the differences.
The worksheet function does arithmetic rounding; the VBA Round does what is
sometimes called "banker's rounding".




--ron
 
A

achidsey

Ron, Thanks for your help. Alan

--
achidsey


Ron Rosenfeld said:
========================
Sub EnterBuyPrincipal()

Dim TPrice As Variant

TPrice = Round(2220 / 850, 4)

Range("A2").FormulaR1C1 = "=100*" & TPrice

End Sub
========================

or

==========================
Sub EnterBuyPrincipal()

Dim TPrice As Variant

TPrice = Application.WorksheetFunction.Round(2220 / 850, 4)

Range("A2").FormulaR1C1 = "=100*" & TPrice

End Sub
=========================

Check the MSKB for VBA Round vs Round worksheet function for the differences.
The worksheet function does arithmetic rounding; the VBA Round does what is
sometimes called "banker's rounding".




--ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top