這不是一個答案,但建議太複雜,不能評論。
如果數組公式在列被刪除前就位,我希望Excel根據需要調整它們,如果可以的話。我假設你的宏運行時工作表不像你預期的那樣。
我被運行我的宏之前更換工作表的人燒燬了。那一次,這個變化非常微妙,而且看起來這個宏觀工作起來了。有一段時間他們才注意到數據被破壞了。
現在我的一個宏的第一階段是檢查關於工作表的假設,我不控制宏觀依賴的工作表。
如果有一種方法 - 不是檢查單個值等 - 進行確認一列只包含數值,比方說,我不知道。我會檢查invidual值,如果我有足夠的擔心,但你可以檢查這可能是適當的代理的數字格式。我一定會檢查列標題和表格標題的值。
我創建了一個新的工作簿和工作組的工作表「Sheet2的」到:
下面將產生以下輸出宏:
The number formats within Range E1:E16 are 0.00
The number formats within Range E1:E17 are not all the same
Check Values failure: Worksheet Sheet4 not found
Check Values failure: Cell E2 has a value of [2] but I was expecting [5]
你可以看到它是很容易檢查對於意外的格式,缺少工作表和不正確的單元格值。我爲自己開發了一套不適合分享的檢查功能,但您可以看到什麼是可能的。
Option Explicit
Sub Test()
Dim ErrMsg As String
Dim Rng As Range
Dim NF As Variant
With Worksheets("Sheet2")
Set Rng = .Range("E1:E16")
NF = Rng.NumberFormat
If IsNull(NF) Then
Debug.Print "The number formats within Range " & _
Replace(Rng.Address, "$", "") & " are not all the same"
Else
Debug.Print "The number formats within Range " & _
Replace(Rng.Address, "$", "") & " are " & NF
End If
Set Rng = .Range("E1:E17")
NF = Rng.NumberFormat
If IsNull(NF) Then
Debug.Print "The number formats within Range " & _
Replace(Rng.Address, "$", "") & " are not all the same"
Else
Debug.Print "The number formats within Range " & _
Replace(Rng.Address, "$", "") & " are " & NF
End If
End With
Call CheckValues(ThisWorkbook.Name, "Sheet2", ErrMsg, _
"C2", "Date", "C5", "Name", "C9", "Id")
If ErrMsg <> "" Then
Debug.Print "Check Values failure: " & ErrMsg
End If
Call CheckValues(ThisWorkbook.Name, "Sheet4", ErrMsg, _
"C2", "Date", "C5", "Name", "C9", "Id")
If ErrMsg <> "" Then
Debug.Print "Check Values failure: " & ErrMsg
End If
Call CheckValues(ThisWorkbook.Name, "Sheet2", ErrMsg, _
"E1", 1, "E2", 5)
If ErrMsg <> "" Then
Debug.Print "Check Values failure: " & ErrMsg
End If
End Sub
Sub CheckValues(ByVal WbkName As String, ByVal WshtName As String, _
ByRef ErrMsg As String, ParamArray CellDtl() As Variant)
' If the specified cells have the expected values, ErrMsg will be empty
' on return. Otherwise ErrMsg will report the first cell with an
' unexpected value.
' WbkName The name of an open workbook.
' WshtName The name of an worksheet within the workbook.
' CellDtl Must contain an even number of values. The first value
' of each paid must be a cell address such as "C1". The
' second value must be the expected value of that cell.
' for exampe ... "B1", Name", "C1", "Date", ... indicates
' that cell B1 should have a value of "Name" and cell C1
' should have a value of "Date".
Dim Found As Boolean
Dim InxCD As Long
Dim InxWbk As Long
Dim InxWsht As Long
Found = False
For InxWbk = 1 To Workbooks.Count
If WbkName = Workbooks(InxWbk).Name Then
Found = True
Exit For
End If
Next
If Not Found Then
ErrMsg = "Workbook " & WbkName & " is not open"
Exit Sub
End If
With Workbooks(WbkName)
Found = False
For InxWsht = 1 To .Worksheets.Count
If WshtName = .Worksheets(InxWsht).Name Then
Found = True
Exit For
End If
Next
If Not Found Then
ErrMsg = "Worksheet " & WshtName & " not found"
Exit Sub
End If
With .Worksheets(WshtName)
For InxCD = 0 To UBound(CellDtl) Step 2
If .Range(CellDtl(InxCD)).Value <> CellDtl(InxCD + 1) Then
ErrMsg = "Cell " & CellDtl(InxCD) & " has a value of [" & _
.Range(CellDtl(InxCD)).Value & "] but I was expecting [" & _
CellDtl(InxCD + 1) & "]"
Exit Sub
End If
Next
End With
End With
' All value match
ErrMsg = ""
End Sub
能否請您發佈您的代碼。可能是「工作」是不是在代碼中明確提到,如果焦點移動到另一個表...好了,也不好。 – 2014-12-03 21:22:50
是否有任何將計算設置爲手動的代碼?什麼「停止工作」看起來像是完全錯誤的結果,不計算輸入改變時的錯誤值? xlsm文件中的數組公式沒有問題。 – 2014-12-03 21:49:41