2013-05-03 87 views
0

我有這個項目,與Excel 32位工作正常,但我有問題在64位運行它。無效的公式不會拋出錯誤-VBA Excel 2010 64位

我有這個部分,我處理無效的公式(那些無法由Excel評估的東西。)32位用於拋出錯誤,我可以捕獲,但在64位,我似乎有問題,我不知道的。代碼有點困難。

Sub Macro1() 
    Dim x As Variant 
    On Error Goto ErrH: 
    ReDim x(1, 1) 
    x(0, 0) = "=1+1" 
    x(0, 1) = "=1+ " ' <--this is a sample of what I refer to as Invalid formula 
    x(1, 0) = "=1+2" 
    x(1, 1) = "=1+1" 


    Range("A1:B2").Value = x ' <--Im stuck in this part. 
           ' the program does not proceed beyond this point 
           ' and does not throw error like it used to. 

    'I do something here 


    On Error Goto 0 
    Exit Sub 

ErrH: 


    ' I have bunch of stuffs that I do here, basically, Error handling. 


End Sub 

我需要爲Excel做些什麼來在我的代碼中指出的行上拋出錯誤?

回答

0

我已經找到一種方法這個工作爲了我。 我只是使用Range.FormulaArray而不是Range.Value,雖然我還不確定這兩者之間有什麼區別,它似乎工作得很好。

但我仍然不知道爲什麼excel 64位不會拋出它曾經使用過的錯誤,也許我下次有空看看它。

0

爲了驗證該式中,可以使用predifined功能VBA

Right(text,number of character) 
ISNUMBER(text) 

暗淡i設定爲整數:I = 0 暗淡J所示整數:J = 0

for i = 0 to 1 
    for j = 0 to 1 
     if ISNUMBER(Right(x(i,j),1)) OR Right(x(i,j),1)=")" Then 
     ' do anything you want if the formula is correct 
     else 
      Goto ErrH: 
     End If 
    next 
next 

我不認爲你可以使用這種方法輸出X的值,因爲X是一個二維數組。

Range("A1:B2").Value = x 

輸出正確的方法是用2循環

dim i as integer :i=0 
dim j as integer :j=0 

for i = 0 to 1 
    for j = 0 to 1 
    Cells(i+1).value = x(i,j) 
    next 
next 

錯誤處理有可能會輸出一個消息

ErrH: 
    MsgBox "Invalid Formula" 
    exit sub 
+0

感謝您的快速響應。但我不能用這個理由有兩個原因,第一,你的建議只限於我給出的那些公式。我們的代碼使用更復雜的Excel公式函數,如AVERAGE,COUNT,IF等,我們也使用命名範圍。第二,處理我們所做的事情需要時間,我們通常處理超過65k行和大約500列,循環遍歷單元格會花費時間。我保證在我們遷移到64位辦公室之前,以我所做的方式顯示2D並不會導致問題。我只想知道爲什麼64位Excel不會像過去那樣拋出錯誤 – user2345501 2013-05-03 05:32:52