Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4347|回复: 5

[已解决]Private Sub woeksheet_selectionchange(ByVal Target As Range)看不懂

[复制链接]
发表于 2014-6-5 17:06 | 显示全部楼层 |阅读模式
Private Sub woeksheet_selectionchange(ByVal Target As Range)
Target.Value = Target.Address
End Sub
这个代码我选中单元格后为啥没有反映啊?不是可以在选中的单元格中自动写入地址吗?

Private Sub woeksheet_selectionchange(ByVal target1 As Range)
  target1.Value = target1.Address
    target1.Offset(1, 0).Select
End Sub
Option Explicit

Private Sub woeksheet_selectionchange(ByVal Target As Range)
Target.Value = Target.Address
Application.EnableEvents = False
    target.Offset(1, 0).Select
Application EnableEvents = True
End Sub
还有这两个程序说第一个会是个死循环,第二个是正常的。enableevents属性到底是禁用了哪个事件能给下详细讲解不,谢谢



最佳答案
2014-6-5 18:14
Application.EnableEvents = False
禁用了woeksheet_selectionchange 事件的触发
如果不禁用
target1.Offset(1, 0).Select 就会一直触发下去,导致死循环
或者加上个判断也可以比如
Private Sub woeksheet_selectionchange(ByVal target1 As Range)
  target1.Value = target1.Address
if target1.value<>0 then
    target1.Offset(1, 0).Select
end if
End Sub
发表于 2014-6-5 17:18 | 显示全部楼层
本帖最后由 suye1010 于 2014-6-5 17:22 编辑

关于这个问题,请按F1

You can write event procedures in Microsoft Office Excel at the worksheet, chart, query table, workbook, or application level. For example, the Activate event occurs at the sheet level, and the SheetActivate event is available at both the workbook and application levels. The SheetActivate event for a workbook occurs when any sheet in the workbook is activated. At the application level, the SheetActivate event occurs when any sheet in any open workbook is activated.

Worksheet, chart sheet, and workbook event procedures are available for any open sheet or workbook. To write event procedures for an embedded chart, QueryTable object, or Application object, you must create a new object using the WithEvents keyword in a class module.

Use the EnableEvents property to enable or disable events. For example, using the Save method to save a workbook causes the BeforeSave event to occur. You can prevent this by setting the EnableEvents property to False before you call the Save method.
回复

使用道具 举报

 楼主| 发表于 2014-6-5 17:44 | 显示全部楼层
suye1010 发表于 2014-6-5 17:18
关于这个问题,请按F1

You can write event procedures in Microsoft Office Excel at the worksheet, c ...

你英语真好!
回复

使用道具 举报

发表于 2014-6-5 18:14 | 显示全部楼层    本楼为最佳答案   
Application.EnableEvents = False
禁用了woeksheet_selectionchange 事件的触发
如果不禁用
target1.Offset(1, 0).Select 就会一直触发下去,导致死循环
或者加上个判断也可以比如
Private Sub woeksheet_selectionchange(ByVal target1 As Range)
  target1.Value = target1.Address
if target1.value<>0 then
    target1.Offset(1, 0).Select
end if
End Sub
回复

使用道具 举报

 楼主| 发表于 2014-6-6 10:15 | 显示全部楼层
后来我发现是我的代码有问题,
Private Sub woeksheet_selectionchange(ByVal Target As Range)
Target.Value = Target.Address
Application.EnableEvents = False
    target.Offset(1, 0).Select
Application EnableEvents = True
End Sub
写错了,我说咋 没有反应呢
回复

使用道具 举报

 楼主| 发表于 2014-6-6 10:16 | 显示全部楼层
qh8600 发表于 2014-6-5 18:14
Application.EnableEvents = False
禁用了woeksheet_selectionchange 事件的触发
如果不禁用

多谢讲解
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-1 03:11 , Processed in 0.404995 second(s), 13 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表