我正在尝试编写一个VBA脚本,以允许Excel工作表在某个点冻结窗格之前正常向下滚动几行。以下是每当用户滚动页面时我想要运行的代码:
Sub FreezeView()
Dim lngTopRow As Long
With ActiveWindow.VisibleRange
lngTopRow = .Row
End With
If ((lngTopRow >= 7) And (ActiveWindow.FreezePanes = False)) Then
ActiveSheet.Range("B10").Select
ActiveWindow.FreezePanes = True
Else
If ((lngTopRow = 10) And (ActiveWindow.FreezePanes = True)) Then
ActiveWindow.FreezePanes = False
End If
End If
' MsgBox (lngTopRow)
End Sub
据我所知,Excel VBA没有任何直接事件来检测滚动;但是,基于This Article,可以通过使用InkPicture对象来实现。不幸的是,此页面上的示例表不再托管。我试图访问InkPicture的_Painting事件(以及其他事件)来完成这项工作,但是还没有能够将这些事件中的任何事件发送到#34; fire。"以下是我用来尝试实现此目的的代码:
首先,我创建了一个名为" clsInkPicEvents "的类模块。使用以下代码:
Public WithEvents InkPicEvents As InkPicture
Private Sub Class_Initialize()
MsgBox ("Initialized")
End Sub
Private Sub InkPicEvents_Click()
MsgBox ("Click")
End Sub
Private Sub InkPicEvents_Painted(ByVal hDC As Long, ByVal Rect As MSINKAUTLib.IInkRectangle)
MsgBox ("Painted")
End Sub
Private Sub InkPicEvents_Painting(ByVal hDC As Long, ByVal Rect As MSINKAUTLib.IInkRectangle, Allow As Boolean)
MsgBox ("Painting")
End Sub
Private Sub InkPicEvents_Resize(Left As Long, Top As Long, Right As Long, Bottom As Long)
MsgBox ("Resize")
End Sub
Private Sub InkPicEvents_Stroke(ByVal Cursor As MSINKAUTLib.IInkCursor, ByVal Stroke As MSINKAUTLib.IInkStrokeDisp, Cancel As Boolean)
MsgBox ("Stroke")
End Sub
然后,在" 模块1 "我有以下脚本来创建InkPicture对象并实例化该类。我真的不太熟悉这个,所以我不确定我这样做是否正确:
Private Sub AddInkPic()
Set oInkPic1 = Sheets("Sheet1").OLEObjects.Add _
(ClassType:="msinkaut.InkPicture.1", Left:=Sheets("Sheet1").Range("V12").Left, Top:=Sheets("Sheet1").Range("V12").Top, _
Width:=100, Height:=100)
End Sub
Private Sub Class_Initialize1()
Dim clsInkPic1 As clsInkPicEvents
Set clsInkPic1 = New clsInkPicEvents
Set clsInkPic1.InkPicEvents = Sheets("Sheet1").OLEObjects("InkPicture1").Object
clsInkPic1.InkPicEvents.SetEventInterest ICEI_Painting, True
MsgBox (clsInkPic1.InkPicEvents.GetEventInterest(ICEI_Painting))
End Sub
我试图手动将_Painting事件的事件兴趣设置为true,因为我怀疑它默认设置为false,但这似乎没有效果。我还怀疑InkPicture对象的一个属性可能是原因,因此尝试更改不同的属性,例如:
Private Sub EditVals()
Sheets("Sheet1").OLEObjects("InkPicture1").Object.Enabled = True
Sheets("Sheet1").OLEObjects("InkPicture1").Object.InkEnabled = True
Sheets("Sheet1").OLEObjects("InkPicture1").Object.AutoRedraw = True
Sheets("Sheet1").OLEObjects("InkPicture1").Object.CollectionMode = 2
Sheets("Sheet1").OLEObjects("InkPicture1").Object.EditingMode = 0
End Sub
但这似乎没有改变任何事情。现在我想知道它是否与设计模式有关,因为我注意到在创建InkPicture对象之后我无法将其打印,直到我再次点击设计模式,所以我&#39 ;我想知道这个物体是不是活跃的"直到我这样做,如果启用然后禁用设计模式可能会以某种方式"清除"类实例还是什么?真的不确定。无论如何,任何帮助将不胜感激!感谢。