【发布时间】:2013-12-20 00:46:22
【问题描述】:
我的最终目标是突出显示与打开电子表格时不同的单元格。这包括用户直接编辑的单元格,以及由于依赖于用户更改的单元格而发生更改的单元格。如果用户将单元格更改回原始值,我想突出显示消失。
我的方法是在打开工作簿时将包含感兴趣单元格的范围写入全局二维数组。然后,我编写了一个 worksheet_change 事件,该事件将更改的单元格及其依赖项与打开工作簿时存储在数组中的相应值进行比较。
Private Sub Workbook_Open()
arr_1 = Sheet26.Range("A1:AR331")
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dep As Range
Dim r As Range
'Check the changed cell to see if it has changed from when the workbook was opened
If Target <> arr_1(Target.Row, Target.Column) Then
'If cell has changed, mark it red
Target.Interior.ColorIndex = 3
'Check to see if cells that depend on the changed cell have changed
Set dep = Target.Dependents
For Each r In dep
'Check to see if the dependent cell(s) is within the print area
If Not Intersect(r, Range("A1:AR331")) Is Nothing Then
If r <> arr_1(r.Row, r.Column) Then
'if cell has changed mark it red
r.Interior.ColorIndex = 3
Else
'if cell hasn't changed unhighlight it
r.Interior.ColorIndex = 0
End If
End If
Next r
Else
'If the target cell matches the value stored in the array, unhighlight the cell
Target.Interior.ColorIndex = 0
End If
End Sub
我的问题是处理包含#N/A 的单元格。当我将单元格写入数组时,它存储为“”。然后,当我将存储在单元格中的#N/A 与存储在数组中的“”进行比较时,我得到了类型不匹配。有没有办法解决这个问题?另外,如果我的整体方法不是解决这个问题的最佳方法,我欢迎另一种方法。
提前致谢!
【问题讨论】: