Lee Z
Lee Z

Reputation: 964

Detect change from nested formulas

I have a very complex workbook with many tabs. The tabs may have either normal data or formulas in various cells. In the case of formulas, the formulas may be nested from one sheet to the next (i.e. a formula on sheet1 refers to a formula on sheet2 which in turn refers to a formula on sheet3, etc.).

I have a hidden tab that contains the following: source sheet, source range, target sheet, and target range.

A named range has been created over these 4 fields and all applicable rows.

When we wish to save data to the database, we loop through every row in the range mapping and copy the data from the source sheet/range to the target sheet/range. After this, the applicable data is serialized into XML and sent to a web service to be saved.

The problem that we wish to resolve is that we want to mark a cell on a hidden sheet when a change is made by the user to a source range. Since formulas can be nested, the Worksheet_Change event does not pick up the change.

Since a change on one sheet may affect another sheet that is not the active sheet, the Workbook_SheetChange event does not catch the change either.

Is there any way form me to catch when a sheet defined in the mapping is changed, even if it is the result of a formula change several levels deep?

Edit

Thank you for your responses. I was attempting to find the fastest and least process intensive way to determine if data changes within a monitored range. The data may consist of actual data or of nested formulas.

My research showed that I could not actually achieve this result by taking range intersections as I could not detect if the data within a monitored range was modified. This is due to the fact that the monitored range may not be on the active sheet and also may contain formulas.

I have shown the method used to actually detect a change below. If there is any feedback on a better way to achieve the same result, I would appreciated it.

Upvotes: 0

Views: 180

Answers (2)

Lee Z
Lee Z

Reputation: 964

There is no "easy" way to detect if a nested formula has changed when the formula being monitored is not on the active sheet. While my hope was to detect the modified range and use an intersection of ranges to set a flag, this was not possible because the Worksheet_Change event does not work on formulas and the Workbook_SheetChange event only works on the active sheet. Since my workbooks have over 20+ tabs and 20 - 30 ranges being monitored, this approach does not work. This approach was desired for speed purposes.

Instead, the workbook will need to "check" to see if the current values are the same as the last time the save to database event was called. If not, a dirty flag will be set.

The code for this approach is provided below.

An example of the mapping range is shown in the picture below though in practice there are 20-30 rows comprising this range.

Mapping Range

There are three other sheets where Sheet3 contains actual data in A1:H1 and Sheet2 has formulas pointing to Sheet3. Sheet1 has formulas pointing to Sheet2.

As the mapping range indicates, we are looking at a range on Sheet1, even though changes may be made to Sheet3.

The code used is as provided below.

Option Explicit
Public Sub DetermineIfEditOccurred()

Dim oMappingRange As Range
Dim szSourceTab As String
Dim szSourceRange As String
Dim oSourceRange As Range
Dim szTargetTab As String
Dim szTargetRange As String
Dim oTargetRange As Range
Dim oWorksheetSource As Worksheet
Dim oWorksheetTarget As Worksheet
Dim oRangeIntersection As Range
Dim nRowCounter As Long
Dim nCellCounter As Long

Dim szSourceValue As String
Dim szTargetValue As String
Dim oCell As Range
Dim bIsDirty As Boolean

If Range(ThisWorkbook.Names("DirtyFlag")).Value = 0 Then
    Set oMappingRange = Range(ThisWorkbook.Names("Mapping"))

    For nRowCounter = 1 To oMappingRange.Rows.Count
        szSourceTab = oMappingRange(nRowCounter, 1)
        szSourceRange = oMappingRange(nRowCounter, 2)

        szTargetTab = oMappingRange(nRowCounter, 3)
        szTargetRange = oMappingRange(nRowCounter, 4)

        Set oWorksheetSource = ThisWorkbook.Worksheets(szSourceTab)
        Set oWorksheetTarget = ThisWorkbook.Worksheets(szTargetTab)
        Set oSourceRange = oWorksheetSource.Range(szSourceRange)
        Set oTargetRange = oWorksheetTarget.Range(szTargetRange)

        nCellCounter = 1

        For Each oCell In oSourceRange.Cells
            szSourceValue = oCell.Value

            If szSourceValue = "#NULL!" Or _
               szSourceValue = "#DIV/0!" Or _
               szSourceValue = "#VALUE!" Or _
               szSourceValue = "#REF!" Or _
               szSourceValue = "#NAME?" Or _
               szSourceValue = "#NUM!" Or _
               szSourceValue = "#N/A" Then

               szSourceValue = ""
            End If

            szTargetValue = GetCellValueByPosition(oTargetRange, nCellCounter)

            If szSourceValue <> szTargetValue Then
                Range(ThisWorkbook.Names("DirtyFlag")).Value = 1
                bIsDirty = True
                Exit For
            End If

            nCellCounter = nCellCounter + 1
        Next

        If bIsDirty Then
            Exit For
        End If
    Next
End If
End Sub
Public Function GetCellValueByPosition(oRange As Range, nPosition As Long) As String

   Dim oCell As Range
   Dim nCounter As Long
   Dim szValue As String

   nCounter = 1

   For Each oCell In oRange
       If nCounter = nPosition Then
            szValue = oCell.Value

            Exit For
       End If

       nCounter = nCounter + 1
   Next

   GetCellValueByPosition = szValue
End Function

The Workbook_SheetChange event is as follows:

Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Call DetermineIfEditOccurred
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name <> "MAPPING" Then
        Call DetermineIfEditOccurred
    End If
End Sub

Upvotes: 0

Davesexcel
Davesexcel

Reputation: 6982

Worksheet_Change event will not work if a cell value is changed by a formula, you need Worksheet_Calculate.

Check out my example workbook here.

And Here for the WebPage of example codes

Upvotes: 1

Related Questions