Hi guys,
I'm having an issue with some VBA code in a work project.
Basically I created a Module (SumColor) which selectively sums the row based on defined cell fill colors. This works nicely, but Excel unfortunately does not include an event to recognize formatting changes, so my user defined formula (SumColor) does not update the totals when cell colors change. To get around this I used the code below to simulate a color change event and recalculate the formulas appropriately.
It works great, except that after closing and reopening the file, the below VBA does not run, and the rows are not recalculated. However, just making some inconsequential changes to the code (extra spacing, etc) will activate it again.
I've also tried adding "Application.EnableEvents = True" to the Workbook_BeforeClose event, but it hasn't had an impact.
I am running Excel 2007 with all Macros enabled.
Any ideas?
Thanks!
Sub Worksheet_SelectionChange(ByVal Target As Range)
Static OldCell As Excel.Range
If OldCell Is Nothing Then
Set OldCell = ActiveCell
End If
' movement within ColorCells:
If Not Application.Intersect(Target(1, 1), Range("F7", "AZ16")) Is Nothing Then
Me.Calculate
' movement out
ElseIf Application.Intersect(Target(1, 1), Range("F7", "AZ16")) Is Nothing Then
If Not Application.Intersect(OldCell, Range("F7", "AZ16")) Is Nothing Then
Me.Calculate
End If
End If
Set OldCell = Target(1, 1)
End Sub
I'm having an issue with some VBA code in a work project.
Basically I created a Module (SumColor) which selectively sums the row based on defined cell fill colors. This works nicely, but Excel unfortunately does not include an event to recognize formatting changes, so my user defined formula (SumColor) does not update the totals when cell colors change. To get around this I used the code below to simulate a color change event and recalculate the formulas appropriately.
It works great, except that after closing and reopening the file, the below VBA does not run, and the rows are not recalculated. However, just making some inconsequential changes to the code (extra spacing, etc) will activate it again.
I've also tried adding "Application.EnableEvents = True" to the Workbook_BeforeClose event, but it hasn't had an impact.
I am running Excel 2007 with all Macros enabled.
Any ideas?
Thanks!
Sub Worksheet_SelectionChange(ByVal Target As Range)
Static OldCell As Excel.Range
If OldCell Is Nothing Then
Set OldCell = ActiveCell
End If
' movement within ColorCells:
If Not Application.Intersect(Target(1, 1), Range("F7", "AZ16")) Is Nothing Then
Me.Calculate
' movement out
ElseIf Application.Intersect(Target(1, 1), Range("F7", "AZ16")) Is Nothing Then
If Not Application.Intersect(OldCell, Range("F7", "AZ16")) Is Nothing Then
Me.Calculate
End If
End If
Set OldCell = Target(1, 1)
End Sub