2012-10-23 23 views
1

我正在用excel編寫一個應用程序,可以幫助您瞭解Completing the Square過程。如果變量是一個整數,我希望單元格被格式化爲數字,如果不是,則格式化爲分數。更改VBA中的單元格數據類型?

If h = (Int(h)) Then 
Range("D1").Select 
Selection.NumberFormat = "Number" 
Else 
Range("D1").Select 
Selection.NumberFormat = "?/?" 
End If 

此代碼不能出於某種原因,讓我有以下錯誤:

Unable to set the NumberFormat property of the Range class

是否有任何其他的方式,我可以做到這一點?

回答

2

嘗試:

If h = (Int(h)) Then 
Range("D1").Select 
Selection.NumberFormat = "0" 
Else 
Range("D1").Select 
Selection.NumberFormat = "# ?/?" 
End If 

但是,如果格式化一個整數作爲一小部分,它仍然顯示爲一個整數(即「1」的一小部分仍然是「1」)。那麼爲什麼不把它作爲一個分數來格式化呢?例如:

+2

id選中並帶範圍(「D1」)。例如numberformat =「0」 – scott