excel - 事件点击单元格
问题描述
我刚刚创建了一个新文件并放入了数据(A1 到 A10 行),我想知道每次单击单元格 A 时如何从单元格中获取数据,如果它不为空(例如:当我单击A列的第三行,所以我可以得到它的值)。我看到了“Worksheet_SelectionChange”方法,但它不适用于我的示例,因为我们必须打开文件代码并将函数放入其中,我想在创建文件时直接执行此操作。这是我的例子
sub test(name as string)
sheet.add
ActiveSheet.name=name
i=2
while i < 10
cells(i,1)=i
i=i+i
Wend
end sub
解决方案
A Worksheet_SelectionChange
event does not necessarily have to reside in a sheet-specific module. It could also for example reside in a class module
, and important for this instance, the ThisWorkbook module (although there it is named a Workbook_SheetSelectionChange
event).
If you always want to fire the Workbook_SheetSelectionChange event, regardless of in what sheet the selection changed, try the following
'place in the ThisWorkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
'Do stuff
MsgBox "Changes were made in worksheet: " & Sh.Name, vbInformation
End Sub
If you only want to fire the event on the latest made WS
1) You could make sure the latest made WS is always the last in the WB
'In a regular module
Sub addnwsheet()
Dim shtname As String
With ThisWorkbook
shtname = "temp" & .Sheets.Count
'add a sheet at the end
.Sheets.Add(after:=.Sheets(.Sheets.Count)).Name = shtname
End With
End Sub
'place in the ThisWorkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sh.Index = ThisWorkbook.Sheets.Count Then
'Do stuff
MsgBox "Selection changed in worksheet: " & Sh.Name, vbInformation
End If
End Sub
2) A more secure method is to declare the newly made sheet as a Global
variable. These are persistent and public.
'In a regular module
Global nwsht As Sheet
Sub addnwsheet()
Dim shtname As String
With ThisWorkbook
shtname = "temp" & .Sheets.Count
'add a sheet at the end
.Sheets.Add(after:=.Sheets(.Sheets.Count)).Name = shtname
Set nwsht = .Sheets(shtname)
End With
End Sub
'place in the ThisWorkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Sh = nwsht Then
'Do stuff
MsgBox "Selection changed in worksheet: " & Sh.Name, vbInformation
End If
End Sub
EDIT
If you want to specify the range the event should trigger on
'place in the ThisWorkbook module
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim r as Range
Set r = Workbooks(REF).Sheets(REF).Range("D2:D100") 'event will only get triggered by this range
If Not Intersect(Target, r) Is Nothing Then
'Do stuff
MsgBox "Selection changed in worksheet: " & Sh.Name, vbInformation
End If
End Sub
推荐阅读
- javascript - 在 jsontohtml 中使用 if (typeof(obj._source.url) !== 'undefined') 表达式
- python - 我怎样才能找到由列表和字典组成的字典的长度
- sql - SQL DB2 选择具有相同 ID 和多行的过滤值
- java - 空的 recyclerview 使我的应用程序崩溃。没有 Firebase 数据
- amazon-web-services - Cloudwatch 记录 Kinesis 订阅
- python - 如何将表情符号的 unicode 转换为 CLDR 短名称
- azure - 使用逻辑应用程序部署 Arm 模板引发错误:数据接收器不能在同一资源的同一类别的不同设置中重复使用
- scala - 如何使用 Circe 为某种类型的列表半自动派生解码器?
- javascript - 为什么我的点击事件不起作用?(ASP.NET MVC)
- android - 如何在 Android 中以编程方式启用 VoWiFi(WiFi 通话)?