2015-03-03 241 views
1

我想獲得單元格中包含的公式的結果,但我總是得到0返回,而不是實際結果。VBA - 如何獲得公式的結果? (.Value總是返回0)

我有以下幾點:

Set c = .Cells (5,5) 

MsgBox (c.Formula) ' this return the following: = ASIN (W22-V22/$D$7)*180/PI() 
MsgBox (c.Value) ' this returns 0 
MsgBox (c.Value2) ' this returns 0 as well 

即使我嘗試與評估:

evaluation = Application.Evaluate (c.Formula) 
MsgBox (c.Value) ' it still returns 0 
+1

我測試此代碼,它爲我工作得很好。 – 2015-03-03 12:49:05

回答

1

零是正確的答案,如果這兩個V22W22爲零。

+0

不,他們沒有,我的Excel在整個列中顯示了其他結果:1.1,0.6,0.9,....但是在VBA(.Value)中,它們都返回0。 – Leon 2015-03-03 13:37:48

+0

是計算模式手動還是自動? – 2015-03-03 13:46:05

+0

確保沒有多餘的空間會導致公式無效。 – 2015-03-03 13:48:23

0

也許試試這個:

Sub formVal() 

Dim c As Range 

Set c = ThisWorkbook.Sheets(1).Cells(5, 5) 

MsgBox (c.Formula) 
MsgBox (c.Value) 
MsgBox (c.Value2) 



End Sub 
+0

我看不到有任何變化,但我嘗試過,它確實返回了與以前相同的結果:0始終爲0 – Leon 2015-03-03 13:56:26

+0

也許你的行或列ID是錯的?它是單元格(rowIndex,columnID)。你也可以這樣寫:c = ... cells(「E5」) – 2015-03-03 14:04:19

+0

只是順便說一句,你的公式不應該是= ASIN((W22-V22)/ $ D $ 7)* 180/PI()? – 2015-03-03 14:10:36