我正在使用Excel 2013,并且遇到SelectionChange Event的问题,这使我发疯。我想知道是否有人可以帮助我。我已经呆了这么久(一个星期!),我可能会错过一些容易被人看见的东西。下面的代码在Excel 2007和2010中可以正常工作。
最初,当您激活页面时,代码将立即将您返回到调用页面(在此菜单主菜单中)。该代码的第二次正常工作。在某个阶段,只需选择新的单元格,它将使您返回到另一页。以我为例,它带我回到“主菜单”页面。
我的问题被调用了三个例程(1)选择更改事件,(2)SetHighlightRows1(ByVal目标为范围),(3)MinRowsHeight_ActiveCell
感谢您的协助/ sglxl
Option Explicit
Private Declare Function LockWindowUpdate Lib "USER32" _
(ByVal hwndLock As Long) As Long
----------------------------------------------------------------------------------------------
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Dim Msg, Style, Title, Response
' Similar to ScreenUpdating but this locks the Shapes from continuous Flickering
LockWindowUpdate Application.hWnd
' Initialise
ActiveSheet.Unprotect Password:=pw
Application.ScreenUpdating = False
' Highlight selected rows
Call SetHighlightRows1(ActiveCell)
' Reset ScreenUpdating to False
Application.ScreenUpdating = False
' Headings in all sheets set to Max 53
' Build Message
Msg = "You cannot access this area!"
Style = vbOKOnly + vbInformation
Title = "Company Secretary"
On Error Resume Next
' Limit access area so that row heights remain constant
If Not (Intersect(Target, Range("A1:O8")) Is Nothing) Or Not (Intersect(Target, Range("A1011:O1011")) Is Nothing) Then
Response = MsgBox(Msg, Style, Title)
Range("ptrCursor").Select
GoTo CleanUp:
Else
Target.Select
End If
' Set Row Height
' EnableEvents set to TRUE
Call MinRowsHeight_ActiveCell
' Unprotect AkSht as MinRowsHeight_ActiveCell set Protect = True
ActiveSheet.Unprotect Password:=pw
Rows(3).EntireRow.RowHeight = 53
CleanUp:
' CleanUp
ActiveSheet.Protect Password:=pw, AllowFiltering:=True
Application.ScreenUpdating = True
' Unlock the window updating in the end by passing a null to the LockWindowUpdate API function.
LockWindowUpdate 0
End Sub
' ---------------------------------------------------------------------------------
'----------------------------------------------------------------
Public Sub SetHighlightRows1(ByVal Target As Range)
'----------------------------------------------------------------
Dim MyRng As Range
Dim TargetCol
Dim TargetRow
Dim BeginColumn As Long
Dim EndColumn As Long
Dim BeginRow As Long
' Initialise
' Disable Events before SelectionChange occurs. There may be other events that
' may Trigger the SelectionChange
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error Resume Next
' Define Row and Column ranges to make routine dynamic
TargetCol = Target.Column
TargetRow = Target.Row
BeginColumn = ActiveSheet.Range("ptrColumnBegin").Column
EndColumn = ActiveSheet.Range("ptrColumnEnd").Column - 1
BeginRow = ActiveSheet.Range("ptrBeginCell").Row
' ***** Set Range parameters *****
Set MyRng = Range(Cells(TargetRow, BeginColumn), Cells(TargetRow, EndColumn))
' Initialise
' Disable Events before SelectionChange occurs. There may be other events that
' may Trigger the SelectionChange
Application.ScreenUpdating = False
Application.EnableEvents = False
On Error GoTo CleanUp
If TargetCol > EndColumn Then GoTo CleanUp
' ***** Set range limits *****
' ActiveSheet.Range("ptrEndCell").Row - 1 - This will ensure that if the user inserts additionalRows
' The highlighter bar will follow to include the additional Rows
If TargetRow < BeginRow Or TargetRow > ActiveSheet.Range("ptrEndCell").Row - 1 Then GoTo CleanUp
' ***** End Range Limits *****
Application.Cells.FormatConditions.Delete
' Highlight Columns
With MyRng
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="TRUE"
With .FormatConditions(1).Font
.Bold = True
.Italic = False
' .Color = RGB(192, 0, 0) ' Seaxl Red
.Color = RGB(83, 141, 213) ' Dark Blue
.Color = RGB(0, 51, 204) ' Dark Blue
End With
' .FormatConditions(1).Interior.Color = RGB(225, 234, 204) ' Green
' .FormatConditions(1).Interior.Color = RGB(220, 230, 241) ' Light Blue
.FormatConditions(1).Interior.Color = RGB(248, 248, 248) ' Light Grey
End With
CleanUp:
' CleanUp
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
' ---------------------------------------------------------------------------------
Sub MinRowsHeight_ActiveCell()
'Initialise
Application.ScreenUpdating = False
ActiveSheet.Unprotect Password:=pw
Application.EnableEvents = False
' Only Visible Cells are set to min height
ActiveSheet.Range("tblDatabaseSort").SpecialCells(xlCellTypeVisible).RowHeight = 22.5
' Adjust only the ActiveCell Row height to AutoFit
ActiveCell.EntireRow.AutoFit
If ActiveCell.EntireRow.RowHeight < 22.5 Then
ActiveCell.EntireRow.RowHeight = 22.5
End If
' CleanUp
Application.ScreenUpdating = True
ActiveSheet.Protect Password:=pw
Application.EnableEvents = True
End Sub
我一直在审查您的一些代码,下面是我的注释:
我在excel 2013上设置了您的代码并运行了它。(我根据需要添加了命名范围)。我通过注释掉所有带有LockWindowUpdate和Application.ScreenUpdating以及Application.EnableEvents的代码行来查看代码。该代码工作正常。但是,当我重新添加它们时,代码无法按您预期的方式工作。
当我在立即窗口中输入Application.EnableEvents = true时,您的代码将再次开始工作。
我建议您做同样的事情,并逐步重新添加其中的每一项,并细化正在发生的事情。我怀疑Application.EnableEvents正在关闭或关闭。请参阅下面的poitn -1,其中显示了这种情况可能发生的地方...(我不知道是什么错误引起的)。
我认为您需要改进错误处理,以便报告更多信息。
感谢您发布此信息,我发现您的代码非常有用且有趣,并且学习了主要技术,即使用条件格式将格式临时应用于单元格。好的。
这是我注意到的
-1。调用MinRowsHeight_ActiveCell时,如果引发错误(例如,不存在名为tblDatabaseSort的范围,等等),它将引发错误并跳过Application.EnableEvents = True将其打开的清除操作。因此,将其关闭!
添加活动表。在:Range(和Cells()的前面。
'*****设置范围参数*****设置MyRng = Range(单元格(TargetRow,BeginColumn),单元格(TargetRow,EndColumn))
如果未定义命名范围,代码将继续...
关于错误继续
'定义行和列范围以使例程动态TargetCol = Target.Column TargetRow = Target.Row BeginColumn = ActiveSheet.Range(“ ptrColumnBegin”)。Column EndColumn = ActiveSheet.Range(“ ptrColumnEnd”)。Column-1 BeginRow = ActiveSheet。 Range(“ ptrBeginCell”)。Row
此代码段在您的代码中不必要地重复-我认为
'Initialize'在SelectionChange发生之前禁用事件。可能还有其他事件可能触发SelectionChange Application.ScreenUpdating = False Application.EnableEvents = False
Application.Cells.FormatConditions.Delete
'使用MyRng .FormatConditions.Delete突出显示列
我本来会写ActiveSheet.Cells.FormatConditions.Delete,但是您的代码却做同样的事情。上面的代码(上面的代码)再次删除了它们,这是不必要的。
(PS。如果您在具有其他格式条件的工作表上使用此格式,则需要更智能地删除格式条件)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句