2016-07-06 33 views
1

我的VBA代碼工作得很好。我可以改變多重大小的形狀,但只適用於四捨五入的數字。如果我把魔杖變成2.5或5.5的形狀不起作用,但是變量的數據類型是雙倍的。有任何想法嗎?改變形狀大小的數據類型

謝謝

Function ConvertPointToCm(ByVal pnt As Double) As Double 
ConvertPointToCm = pnt * 0.03527778 
End Function 

Function ConvertCmToPoint(ByVal cm As Double) As Double 
ConvertCmToPoint = cm * 28.34646 
End Function 


Sub test() 

Dim objHeigh As Double 
Dim objWidth As Double 
Dim oSh As Shape 

On Error GoTo CheckErrors 

With ActiveWindow.Selection.ShapeRange 
If .Count = 0 Then 
    MsgBox "You need to select a shape first" 
    Exit Sub 
End If 
End With 

objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh")) 
' give the user a way out 
If objHeigh = 0 Then 
Exit Sub 
End If 
objHeigh = ConvertCmToPoint(objHeigh) 

objWidth = CInt(InputBox$("Assign a new size of Width", "Width")) 
' give the user a way out 
If objWidth = 0 Then 
Exit Sub 
End If 
objWidth = ConvertCmToPoint(objWidth) 

For Each oSh In ActiveWindow.Selection.ShapeRange 
If objName <> "" Then 
    oSh.Name = objName 
End If 

oSh.Height = CInt(objHeigh) 
    oSh.Width = CInt(objWidth) 
Next 
Exit Sub 

CheckErrors: MsgBox Err.Description 

End Sub 
+0

你是什麼意思是「不工作」嗎? –

回答

2

這是因爲你在

objHeigh = CInt(InputBox$("Assign a new size of Height", "Heigh")) 

使用CInt鑄造結果作爲integer
所以,答案應該是如下:

objHeigh = CDbl(InputBox$("Assign a new size of Height", "Heigh")) 
+0

爲什麼你不更新你的答案建議使用'CDbl'而不是 - 它會使它更好的答案;-) –

+0

Re:'羅賓'喲,剛剛修改。謝謝 – Grace

+0

回覆:'諾比'如果你發現答案有幫助,你可以請標記爲可接受的答案,非常感謝:) – Grace