×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# VB Excel_Area calculated in inches_Inaccurate

## VB Excel_Area calculated in inches_Inaccurate

(OP)
Hi,
I had convert the shape area to inches by dividing the width and height by 72.
but the data shown is inaccurate.
For example,
I had the rectangle shape with Width = 10.95", Height = 8.87"
Thru calculator I got the area as 97.1265
But macro gave me 96.7

Sub ShowArea()
Dim Width As Double
Dim Height As Double

Width = Selection.ShapeRange(1).Width / 72
Height = Selection.ShapeRange(1).Height / 72

With Selection.ShapeRange(1)

Selection.ShapeRange(1).TextFrame.Characters.Text = Round(Width * Height, 1) & "m2"

End With
End Sub

### RE: VB Excel_Area calculated in inches_Inaccurate

It often pays to check that the values are what you think they are. We could examone via Watch points or using breakpoints. But for now, try the following:

#### CODE

Sub ShowArea()
Dim Width As Double
Dim Height As Double
Dim mySelect As Range

Set mySelect = Selection.Range

MsgBox "Width: " & Selection.ShapeRange(1).Width & " Height: " & Selection.ShapeRange(1).Height
Width = Selection.ShapeRange(1).Width / 72
Height = Selection.ShapeRange(1).Height / 72
MsgBox "Width: " & Width & " Height: " & Height

With Selection.ShapeRange(1)
Selection.ShapeRange(1).TextFrame.Characters.Text = Round(Width * Height, 1) & "m2"
End With
End Sub 

I'd expect to see something like:
Width: 788.4 Height: 638.6
Width: 10.9500003390842 Height: 8.86944410536024

What do you get?

### RE: VB Excel_Area calculated in inches_Inaccurate

(OP)
Hi Strongm,
Yes, I got
Width: 668.5715; Height: 144.3213
Width: 9.28571510314941; Height: 2.00446200370789
While Excel Shape format gave me
Width: 9.33; Height: 2

Based on my observation, the area of shape get from macro seems to be 0.4% different from what I got from excel shape format (own calculation thru calculator).

### RE: VB Excel_Area calculated in inches_Inaccurate

So, can you see what is happening? The problem is that the shape object rounds each of the values that it displays in the shape format dialog, whilst your calculation rounds AFTER you multiply.

For example, try:

#### CODE

Sub ShowArea()
Dim Width As Double
Dim Height As Double
Dim mySelect As Range

Set mySelect = Selection.Range

MsgBox "Width: " & Selection.ShapeRange(1).Width & " Height: " & Selection.ShapeRange(1).Height
Width = Selection.ShapeRange(1).Width / 72
Height = Selection.ShapeRange(1).Height / 72
MsgBox "Width: " & Width & " Height: " & Height

MsgBox Round(Width * Height, 2) & " " & Round(Width, 2) * Round(Height, 2)

With Selection.ShapeRange(1)
Selection.ShapeRange(1).TextFrame.Characters.Text = Round(Width * Height, 1) & "m2"
End With
End Sub 

### RE: VB Excel_Area calculated in inches_Inaccurate

(OP)
Hi,
I try with both code but it seems to be not much different.
Selection.ShapeRange(1).TextFrame.Characters.Text = Round(Width, 1) * Round(Height, 1) & "m2"
Area: 42.84

Selection.ShapeRange(1).TextFrame.Characters.Text = Round(Width * Height, 2) & "m2"
Area: 42.88

Calculator: 43.08

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!