设万维读者为首页 广告服务 技术服务 联系我们 关于万维
简体 繁体 手机版
分类广告
版主:
万维读者网 > 灵机一动 > 跟帖
Search "Excel Event Handler"
送交者: 零加一中 2009月04月08日07:43:02 于 [灵机一动] 发送悄悄话
回  答: 能否把答案贴过来,谢谢。侠行天涯 于 2009-04-07 17:16:16
in Google. There is a article giving detailed solution.
--------------------------------------------------------------
An event handler is associated with just a single worksheet. Start from that worksheet and hit alt-F11 to open the VB editor.
In the upper left window (Project - VBA Project) double click the name of your worksheet.
In the right pane, click the left drop down and change general to Worksheet.
In the right drop down, pick Change.
This will cause Excel to pre-enter the following macro shell for you:

Private Sub Worksheet_Change(ByVal Target As Range)
UserInput = Target.Value
If UserInput > 1 Then
NewInput = Left(UserInput, Len(UserInput) - 2) & ":" & Right(UserInput, 2)
Application.EnableEvents = False
Target = NewInput
Application.EnableEvents = True
End If
End Sub
Any time a cell is changed, the cell that was changed is passed to this program in the variable called "Target". When someone enters a time with a colon in the worksheet, it will evaluate to a number less than one. The If block makes sure to only change cells if they are greater than one. I use the left() and right() functions to break the user input into hours and minutes and insert a colon in between.

Whenever the user enters "2345", the program will change this entry to 23:45.
0%(0)
0%(0)
标  题 (必选项):
内  容 (选填项):
实用资讯
回国机票$360起 | 商务舱省$200 | 全球最佳航空公司出炉:海航获五星
海外华人福利!在线看陈建斌《三叉戟》热血归回 豪情筑梦 高清免费看 无地区限制
一周点击热帖 更多>>
一周回复热帖
历史上的今天:回复热帖
2008: 数学题: 折纸
2008: 简单球面几何问题