BACKGROUND
I have a workbook with 31 worksheets.
Thirty of those sheets (let's call them the Data Sheets) are identical to each other. The 31st sheet (let's call it the Reference Sheet) contains a table with information from each of the 30 Data Sheets, with each Data Sheet taking a single row.
In each of the 30 Data Sheets is a function that uses the Sheet Name to determine which row in the Reference Sheet table that Data Sheet appears. This function sits in Cell A5 of each Data Sheet and results in an integer from 4 to 33. These functions are working perfectly.
Each Data Sheet contains the code below:
CODE IN QUESTION
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Row As Integer
If Not Intersect(Target, Range("C7:L161")) Is Nothing Then
Row = ActiveSheet.Range("A5").Value
Worksheets("Lookups and Calculations").Cells(Row, 9) = Now
End If
End Sub
INTENDED FUNCTION
The code is triggered by a Change Event on the sheet. If a change is detected in any of the cells in Range (C7:L161), then the date and time of the change is logged in the table on the Reference Sheet ("Lookups and Calculations"). The row where that sheet's data is to be entered is taken from Cell A5 of the sheet, and the target cell is then identified using the CELL (Row,9) function. All results for this fall into column 9 (Column I).
Each of the 30 Data Sheets contains the code above.
PROBLEMS ENCOUNTERED
QUESTIONS
I have a workbook with 31 worksheets.
Thirty of those sheets (let's call them the Data Sheets) are identical to each other. The 31st sheet (let's call it the Reference Sheet) contains a table with information from each of the 30 Data Sheets, with each Data Sheet taking a single row.
In each of the 30 Data Sheets is a function that uses the Sheet Name to determine which row in the Reference Sheet table that Data Sheet appears. This function sits in Cell A5 of each Data Sheet and results in an integer from 4 to 33. These functions are working perfectly.
Each Data Sheet contains the code below:
CODE IN QUESTION
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Row As Integer
If Not Intersect(Target, Range("C7:L161")) Is Nothing Then
Row = ActiveSheet.Range("A5").Value
Worksheets("Lookups and Calculations").Cells(Row, 9) = Now
End If
End Sub
INTENDED FUNCTION
The code is triggered by a Change Event on the sheet. If a change is detected in any of the cells in Range (C7:L161), then the date and time of the change is logged in the table on the Reference Sheet ("Lookups and Calculations"). The row where that sheet's data is to be entered is taken from Cell A5 of the sheet, and the target cell is then identified using the CELL (Row,9) function. All results for this fall into column 9 (Column I).
Each of the 30 Data Sheets contains the code above.
PROBLEMS ENCOUNTERED
- The code works some of the time, but other times it throws an "Application-Defined or Object-Defined Error" message at the underlined line above.
- Sometimes the code is triggered even when no change in Range (C7:L161) has been made. This happens when the sheet is being hidden or unhidden via VBA.
QUESTIONS
- First, am I doing anything that's obviously wrong?
- Am I running afoul of the code confusing which sheet is active at the time?
- Any ideas why it would work some of the time but throw errors otherwise?