0

i want to create a sub that records in a list how many times you pressed the return key in the current session (it should write on a new line each time you reopen the file) heres are my attempts: somehow it's not working

Option Explicit

Private Sub Worksheet_activated(ByVal target As Range)
  Target.Value = target.Value + 1
  Application.OnKey "~", "CountDT"
End Sub

Private Sub worksheet_deactivated()
   Application.OnKey "~"
End Sub

Option Explicit
Sub CountDT()
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = ActiveCell.Value & " " & Date & " " & Time
End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    "Somehow it is not working". Maybe share those details? –  Feb 14 '18 at 15:53
  • what are those `Worksheet_activated` and `Worksheet_activated` subs? They sound like `Worksheet_Activate` and `Worksheet_Deactivate` event handler... – DisplayName Feb 14 '18 at 17:12
  • This post may give some pointers: [is-there-any-event-that-fires-when-keys-are-pressed-when-editing-a-cell](https://stackoverflow.com/questions/11153995/is-there-any-event-that-fires-when-keys-are-pressed-when-editing-a-cell) – Darren Bartrup-Cook Feb 14 '18 at 17:28

1 Answers1

0

In the worksheet code module:

Option Explicit

Private Sub Worksheet_Activate()
    Application.OnKey "~", Me.CodeName & ".CountDT"
End Sub

Private Sub Worksheet_Deactivate()
   Application.OnKey "~"
End Sub


Sub CountDT()
    ActiveCell.Offset(1, 0).Select
    ActiveCell.Value = ActiveCell.Value & " " & Date & " " & Time
End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • i dont understand what does the ".Me Codename" does? it appears to me that it doesnt make any difference(after running it)? well i think my qustion is how to write the specific code that count the times of the event, i didnt manage to write it, so i am looking for help on that... – Francoise Eric Feb 15 '18 at 10:31
  • i dont understand what does the ".Me Codename" does? it appears to me that it doesnt make any difference(after running it)? well i think my qustion is how to write the specific code that count the times of the event, i didnt manage to write it, so i am looking for help on that... – Francoise Eric Feb 15 '18 at 10:31
  • CodeName is just so that Excel can locate the CountDT sub (since it's in the sheet module not in a regular module). Where do you want to put the count of key presses? – Tim Williams Feb 15 '18 at 15:46
  • yeah you are right! whatever works i guess! because i seem to need to use a loop ? i dont know if i should try writing on the sheet or the module....my idea was to tried to count the event but it was obviously wrong. sorry i am a beginner – Francoise Eric Feb 15 '18 at 18:10