0
votes

My daughter's workplace uses a 2010 .xlsx workbook with several sheets. Earlier my wife and I assisted by researching arrays and implementing them (using 2007 on our end). The arrays worked beautifully...until...macros were added.

At a much later time, I was asked to make it simpler to setup data in a sheet which functions as a report using two print areas. There are two sheets of plain alphanumeric data. (No formulas, etc. in either of these sheets, and they are not linked to any other sheets). One sheet holds the data as she entered it and is not printed. The other sheet holds only the print-formatted copy of the same data. She had been moving the 4 columns of data from one sheet's long columns set to the other sheet in 54-row blocks via manual copy-paste to setup the data for printing. This resulted in either one or two pages of print data, with each print area-defined page in 3 sets of four 54-row columns.

Starting with record macro, I did the same thing she had done, then edited the VBA to remove unneeded actions. After the macros were working smoothly, I saved the .xlsx file as .xlsm. The macros work great. Oddly enough, now the arrays in another unrelated sheet have stopped working. No idea why. I recreated the file again with the same result.

Note: The last (thought to be final) version was created by exporting the macros as a .bas file, and importing that into a working copy of the original .xlsx file prior to saving as .xlsm.

I'd appreciate any suggestions. I've got a couple ideas to try, although I have no information suggesting these would help. One is to revisit each array, and use the Ctrl+Shift+Enter on each one again. The other is create another version, keeping the original .xlsx workbook as it was before the macros were added, and put the macros module in a separate .xlsm workbook.

2
Could you please post your code. Possibly the 'worksheet' is not explicitly referenced in the code and if focus moves to another sheet... well, not good. - Wayne G. Dunn
Is there any code which sets calculation to manual ? What does "stopped working" look like exactly - wrong result, not calculating when inputs changed, error values? There's typically no problem with array formulas in xlsm files. - Tim Williams

2 Answers

0
votes

Thanks for making me re-re-rethink things. Although I wasn't working in the other sheets, investigation discovered someone had deleted a column, shifting their sheet columns to the left. Cells such as $AA$1 in the arrays were now pointing at $AA$1, but that was no longer the column with the data intended. Initial testing indicates this was the problem, and inserting an empty column caused their sheet formulas (arrays) to work. I'm leaving the rest up to them and consider this issue resolved.

Although I don't have control over what is done with the workbook, I can at least advise some sort of locking protection to remind the main person responsible to not make such changes in the future. I'm guessing that some VBA could be in place...in an OnCurrent event to trigger a warning message if someone unprotects?

0
votes

This is not an answer but advice that is too complicated for a comment.

If the array formulae were in place before the column was deleted, I would expect Excel to adjust them as necessary if it can. I am assuming the worksheet was not as you expected when your macro was run.

I have been burnt by someone changing a worksheet before running a macro of mine. On that occasion, the change was quite subtle and it appeared the macro worked. It was sometime before they noticed that the data was being corrupted.

Now the first stage of a macro of mine is to check assumptions about worksheets I do not control but on which the macro depends.

If there is a way - other than checking individual values - to confirm a column only contains numeric values, say, I do not know it. I will check invidual values if I am worried enough but you can check the number format which may be an adequate proxy. I will certainly check the values of column headings and table headings.

I created a new workbook and set worksheet "Sheet2" to:

Image of test data for macro below

The macro below produced the following output:

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]

You can see it is fairly easy to check for unexpected formatting, missing worksheets and incorrect cell values. I have developed a set of checking functions for myself which are not suitable for sharing but you can see what is possible.

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