Excel 使SheetSelectionChange在其他工作簿上运行
我正在尝试为Excel构建一个“加载项”,它基于Excel 使SheetSelectionChange在其他工作簿上运行,excel,vba,Excel,Vba,我正在尝试为Excel构建一个“加载项”,它基于activecell突出显示当前行/列。我需要它能够在xlam工作簿(显然不是activeworkbook)中打开/关闭和运行 目前,我的代码可以工作,但只有当我将其粘贴到实际的ActiveWorkbook下的ThisWorkbook下时,我的代码才能工作。如何创建对所有活动工作簿或“打开”工作簿进行操作的工作簿\u sheet selection change。IE:任何工作簿中任何活动单元格的任何更改。这将是理想的,但我可以让它影响Active
activecell
突出显示当前行/列。我需要它能够在xlam
工作簿(显然不是activeworkbook
)中打开/关闭和运行
目前,我的代码可以工作,但只有当我将其粘贴到实际的ActiveWorkbook
下的ThisWorkbook
下时,我的代码才能工作。如何创建对所有活动工作簿
或“打开”工作簿进行操作的工作簿\u sheet selection change
。IE:任何工作簿中任何活动单元格的任何更改。这将是理想的,但我可以让它影响ActiveWorkbook
我通读了这个链接-->其中提到了一些关于创建“类模块”的内容,但我不熟悉这个术语/函数
以下是目前的代码:
Public ReadMode As Boolean
Public Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim Count As Integer
Count = 0
While Count < 1 And ReadMode = True
Count = 2
'https://www.excel-easy.com/vba/examples/highlight-active-cell.html
Dim rowNumberValue As Integer, columnNumberValue As Integer, i As Integer, j As Integer
Cells.Interior.ColorIndex = 0
rowNumberValue = ActiveCell.Row
columnNumberValue = ActiveCell.Column
For i = 1 To rowNumberValue
Cells(i, columnNumberValue).Interior.ColorIndex = 37
Next i
For j = 1 To columnNumberValue
Cells(rowNumberValue, j).Interior.ColorIndex = 37
Next j
Wend
End Sub
'Public Sub ReadModeToggle(control As IRibbonControl)
Public Sub ReadModeToggle()
If ReadMode = False Then
Call ReadModeEnable_Sub
ElseIf ReadMode = True Then
Call ReadModeDisable_Sub
End If
End Sub
Public Sub ReadModeEnable_Sub()
ReadMode = True
End Sub
Public Sub ReadModeDisable_Sub()
ReadMode = False
Dim rowNumberValue As Integer, columnNumberValue As Integer, i As Integer, j As Integer
Cells.Interior.ColorIndex = 0
rowNumberValue = ActiveCell.Row
columnNumberValue = ActiveCell.Column
For i = 1 To rowNumberValue
Cells(i, columnNumberValue).Interior.ColorIndex = 0
Next i
For j = 1 To columnNumberValue
Cells(rowNumberValue, j).Interior.ColorIndex = 0
Next j
End Sub
公共读取模式为布尔值
公共子工作簿\u Sheet SelectionChange(ByVal Sh作为对象,ByVal目标作为范围)
将计数设置为整数
计数=0
当计数<1且读取模式=真时
计数=2
'https://www.excel-easy.com/vba/examples/highlight-active-cell.html
Dim rowNumberValue为整数,columnNumberValue为整数,i为整数,j为整数
Cells.Interior.ColorIndex=0
rowNumberValue=ActiveCell.Row
columnNumberValue=ActiveCell.Column
对于i=1到rowNumberValue
单元格(i,columnNumberValue).Interior.ColorIndex=37
接下来我
对于j=1到columnNumberValue
单元格(rowNumberValue,j).Interior.ColorIndex=37
下一个j
温德
端接头
'公共子读取模式切换(控件作为IRIBONControl)
公共子ReadModeToggle()
如果ReadMode=False,则
调用ReadModeEnable_Sub
ElseIf ReadMode=True然后
调用ReadModeDisable\u Sub
如果结束
端接头
公共子读取模式启用_Sub()
ReadMode=True
端接头
公共子读取模式禁用_Sub()
ReadMode=False
Dim rowNumberValue为整数,columnNumberValue为整数,i为整数,j为整数
Cells.Interior.ColorIndex=0
rowNumberValue=ActiveCell.Row
columnNumberValue=ActiveCell.Column
对于i=1到rowNumberValue
单元格(i,columnNumberValue).Interior.ColorIndex=0
接下来我
对于j=1到columnNumberValue
单元格(rowNumberValue,j).Interior.ColorIndex=0
下一个j
端接头
好的,所以我修改代码如下:
插入“类模块”
有关更多详细信息,请查看应用程序级事件。感谢@BigBen所做的一切。我能够打开一个新工作簿,突出显示仍处于活动状态!耶
Public WithEvents appevent As Application ''New Code
Private Sub appevent_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) ''New Code
Dim Count As Integer
Count = 0
While Count < 1 And ReadMode = True
Count = 2
'https://www.excel-easy.com/vba/examples/highlight-active-cell.html
Dim rowNumberValue As Integer, columnNumberValue As Integer, i As Integer, j As Integer
Cells.Interior.ColorIndex = 0
rowNumberValue = ActiveCell.Row
columnNumberValue = ActiveCell.Column
For i = 1 To rowNumberValue
Cells(i, columnNumberValue).Interior.ColorIndex = 37
Next i
For j = 1 To columnNumberValue
Cells(rowNumberValue, j).Interior.ColorIndex = 37
Next j
Wend
End Sub
Public myobject As New Class1 ''New Code
Public ReadMode As Boolean
'Public Sub ReadModeToggle(control As IRibbonControl)
Public Sub ReadModeToggle()
Set myobject.appevent = Application ''New Code
If ReadMode = False Then
Call ReadModeEnable_Sub
ElseIf ReadMode = True Then
Call ReadModeDisable_Sub
End If
Debug.Print "ReadMode = " & ReadMode
End Sub
Public Sub ReadModeEnable_Sub()
ReadMode = True
End Sub
Public Sub ReadModeDisable_Sub()
ReadMode = False
Dim rowNumberValue As Integer, columnNumberValue As Integer, i As Integer, j As Integer
Cells.Interior.ColorIndex = 0
rowNumberValue = ActiveCell.Row
columnNumberValue = ActiveCell.Column
For i = 1 To rowNumberValue
Cells(i, columnNumberValue).Interior.ColorIndex = 0
Next i
For j = 1 To columnNumberValue
Cells(rowNumberValue, j).Interior.ColorIndex = 0
Next j
End Sub