我女儿的工作场所使用2010年.xlsx工作簿和多张工作簿。早些时候,我的妻子和我协助研究阵列并实施它们(使用2007年末)。 阵列工作得很漂亮......直到......添加了宏。
在很晚的时候,我被要求更简单地在一张纸上设置数据,该纸张用作使用两个打印区域的报告。有两张普通的字母数字数据。 (这些纸张中没有公式等,并且它们没有链接到任何其他纸张。)一张纸在输入数据时保存数据并且不打印。另一张纸仅保存相同数据的打印格式副本。她一直在通过手动复制粘贴将4列数据从一个工作表的长列设置为54行块中的另一个工作表来设置打印数据。这导致一页或两页打印数据,每个打印区域定义页面分为3组,每组4行54行。
从记录宏开始,我做了同样的事情,然后编辑了VBA以删除不需要的操作。宏运行顺利后,我将.xlsx文件保存为.xlsm。宏工作得很好。奇怪的是,现在另一张不相关的表中的数组已经停止工作。不知道为什么。我再次使用相同的结果重新创建了文件。
注意:最后一个(被认为是最终版本)版本是通过将宏导出为.bas文件,并在保存为.xlsm之前将其导入原始.xlsx文件的工作副本而创建的。
我很感激任何建议。我有几个想法尝试,虽然我没有信息表明这些会有所帮助。一种是重新访问每个阵列,并再次使用Ctrl + Shift + Enter。另一个是创建另一个版本,保留原始的.xlsx工作簿,就像添加宏之前一样,并将宏模块放在一个单独的.xlsm工作簿中。
答案 0 :(得分:0)
感谢您重新重新思考问题。虽然我没有在其他工作表中工作,但调查发现有人删除了一个列,将他们的工作表列向左移动。阵列中的$ AA $ 1等单元格现在指向$ AA $ 1,但这不再是具有预期数据的列。初始测试表明这是问题所在,插入空列会导致其表单公式(数组)工作。我将其余部分留给他们并考虑解决这个问题。
虽然我无法控制对工作簿所做的操作,但我至少可以建议某种锁定保护,以提醒负责的主要人员以后不要进行此类更改。我猜测某个VBA可能就位......在OnCurrent事件中如果有人取消保护会触发警告信息?
答案 1 :(得分:0)
这不是一个答案,而是对评论过于复杂的建议。
如果数组公式在删除列之前就已存在,我希望Excel能够根据需要调整它们。我假设工作表与宏运行时不符合预期。
在运行我的宏之前,我被更改工作表的人烧毁了。在那个场合,变化非常微妙,宏观似乎有效。在他们发现数据被破坏之前的某个时候。
现在我的宏的第一个阶段是检查关于我无法控制的工作表的假设,而不是宏所依赖的工作表。
如果有一种方法 - 除了检查单个值 - 确认列只包含数值,比方说,我不知道。如果我足够担心,我会检查不变的值,但你可以检查数字格式,这可能是一个足够的代理。我一定会检查列标题和表格标题的值。
我创建了一个新工作簿并将工作表“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