从woorkbook事件访问工作表variables

我有一个Worksheet_SelectionChange事件需要知道更改事件运行之前的范围的细节。 为此,事件的一部分logging当前状态,另一部分查看事件从前一次运行的日志,并与之一起工作。 我将日志保存为全局variables。 我的代码步骤如下所示:

 'Declarations: Private oldT() As Double Private oldHigh As Range 

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) ''' '1) Read range stored in oldHigh ''' Dim newHigh as Range Dim newT() As Double Set newHigh= Target 'range we are currently working on ReDim newT(1 To newHigh.Cells.Count, 1 To 3) ' ''' '2) Loop through each Cell in newHigh and pass 3 details to newT() ' This saves the current state ''' '3) Make temporary change to newHigh ''' '4) Loop through oldHigh, using the values saved in oldT() to reset it ''' Set oldHigh = newHigh'log where you made the temporary changes oldT = newT 'and what the cells were before the changes End Sub 

我不用Workbook_Open初始化variables,而是在oldHigh为空的情况下,在我的select更改代码(步骤1之前)中检查错误。

这一切都很好,除了我现在意识到,如果我保存并退出,最后的重置行动不会发生。 所以我决定在Workbook_BeforeSave事件中执行步骤4) ,这样,工作簿不会保存临时更改(使其变为永久)。代码如下所示:

 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim i As Long Dim subCell As Range Dim oldHigh As Range: Set oldHigh = [a1] For i = 1 To lastCol 'reset Set subCell = oldHigh(1, i) With subCell.Interior .Color = oldT(i, 3) .TintAndShade = oldT(i, 2) .Pattern = oldT(i, 1) End With Next i Set oldHigh = Nothing 'Required if user saves but does not exit, 'This makes the workbook look as if it is freshly opened 'Which is what my code is designed to handle End Sub 

(正如你所看到的,保存的数据是填充信息,我暂时改变填充以突出显示select,然后改回到以后的样子)

问题

当我运行保存事件时,代码不能访问oldT()oldHigh 。 我猜这是因为他们是Private所以我让他们Public ,但这给了以下错误: 错误信息

如何从工作簿事件访问这些worsheetvariables?

实际代码:

为了避免混淆。 注意highRng在这里被称为highRng

 Private oldT() As Double Private oldHigh As Range Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim myTbl As ListObject Dim oldHighTest As Boolean Set myTbl = Me.ListObjects(1) If oldHigh Is Nothing Then oldHighTest = True Else oldHighTest = Intersect(oldHigh, Target) Is Nothing End If If Not Intersect(Target, myTbl.Range) Is Nothing And oldHighTest Then ' highlight cells in table only when new row of table is selected appMod False On Error GoTo e Dim tl As Range, highRng As Range, subCell As Range Dim newT() As Double, lastCol As Long, i As Long Set tl = myTbl.HeaderRowRange(1) Set highRng = Me.Range(Cells(Target.Row, tl.Column), Cells(Target.Row, tl.Column + myTbl.ListColumns.Count - 1)) lastCol = highRng.Columns.Count ReDim newT(1 To lastCol, 1 To 3) ' For i = 1 To lastCol 'save Set subCell = highRng(1, i) With subCell.Interior newT(i, 1) = .Pattern newT(i, 2) = .TintAndShade newT(i, 3) = .Color End With Next i With highRng.Interior 'highlight .Pattern = xlSolid .TintAndShade = 1 .Color = 4189681 End With For i = 1 To lastCol 'reset last highlight using last save Set subCell = oldHigh(1, i) With subCell.Interior .Color = oldT(i, 3) .TintAndShade = oldT(i, 2) .Pattern = oldT(i, 1) End With Next i e: appMod True Set oldHigh = highRng oldT = newT End If End Sub 

放置在Sheet1代码中,并突出显示一行table1(或者页面中的第一个ListObject ,以防我更改表名称) appMod调用的位置

 Public Sub appMod(t As Boolean) With Application .EnableEvents = t .ScreenUpdating = t End With End Sub 

改变时出现问题

 Private oldT() 

 Public oldT() 

但是没有什么问题使得oldHigh Public ,只是它仍然没有被拾取在ThisWoorkbook代码(它被视为空白,当它不是)

如我的评论中所述,将指令插入到一个新的模块而不是sheet1中

 public oldT() 

错误消息提到公共数组在对象模块中是不允许的。 我无法findmsdn上的更多信息。