| 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. |
|
|
![]() |
![]() |
| 實用資訊 | |




