+ Reply to Thread
Results 1 to 5 of 5

ONKEY ony fires once

  1. #1
    Registered User
    Join Date
    03-23-2024
    Location
    nyc
    MS-Off Ver
    365
    Posts
    2

    ONKEY ony fires once

    I want to re-map the numpad decal key to a colon. it's close to working but only fires once. from everything I've read, i was under the impression that if i assign a key to a sub() it stays that way until it is re-assigned again. do i need an endless loop?

    i put this in the worksheet that i want the key remapped in:
    Please Login or Register  to view this content.
    and this in the module of the workbook:
    Please Login or Register  to view this content.
    if i go to the sheet i get the "hello" and it will send 1 ":" then its back to a decimal when i go to a different sheet i get the "bye" and its back to a decal as intended.

    the spreadshett im working on uses mostly time format so it would be nice to have the decmal make a colon.

    thank you,

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: ONKEY ony fires once

    Hi there,

    As soon as the "SendKeys" command is executed the colon character is entered into the active cell - this places the cell in Edit mode. As far as I know, it is not possible to execute a routine while a cell is in Edit mode, so this is probably why the routine executes only a single time.

    Something along the lines of the following code might help to get you moving in the right direction - it takes a four-digit numeric value (e.g. 12.34) and converts it into a time value which "looks like" the numeric value (e.g. 12:34):

    Please Login or Register  to view this content.
    The highlighted may be altered to suit your own requirements.


    Hope this helps.

    Regards,

    Greg M
    Last edited by Greg M; 03-24-2024 at 01:24 PM. Reason: Code added

  3. #3
    Registered User
    Join Date
    03-23-2024
    Location
    nyc
    MS-Off Ver
    365
    Posts
    2

    Re: ONKEY ony fires once

    That works! thank you very much. i didn't realize that routine cant run routines in edit mode. I think you're right about that because the macros get greyed out when a cell is selected. Not only did you solve my problem you showed me 2 new operators [like] & [me]. that I'm sure will be useful in the future.
    and some other things too. I did some searching to try and understand.

    If Target.Cells.CountLarge = 1 Then ' <-- applies the code to selected cells? might not need this since only one cell will be edited at a time. ?

    If Not Intersect(Target, Me.Range(sRANGE_TO_CHANGE)) Is Nothing Then ' this i don't understand at all. is this a double negative? would "if Intersect(Target, Me.Range(sRANGE_TO_CHANGE)) then" be the same?
    me.range(range is the const declared) ... me being this worksheet. makes sense... but 'not intersect' & 'is nothing' is confusing. wouldn't we want the range to intersect?

    sTextValue = Target.Text ' <-- does this save the cell value to the var as text format?
    why not selection.value ?

    and this "If sTextValue Like "##.##" Then" // i understand this and usually use 24-hour time. the code didn't work for 1 digit hours. so i tried a single digit wildcard (Like "?#.##") and also (Like "##.##" or Like "#.##") and got an error.

    I realize I'm asking a lot here. if you don't have time to elaborate, I understand. this has opened a lot of doors for me and in the end it does what i need it to/can be used as is. thanks again.

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: ONKEY ony fires once

    Hi again,


    If Target.Cells.CountLarge = 1 Then ' <-- applies the code to selected cells? might not need this since only one cell will be edited at a time. ?

    I always include this to ensure that the subsequent code is NOT executed in cases where a range of cells is selected and the Delete key is pressed to clear the range.



    If Not Intersect(Target, Me.Range(sRANGE_TO_CHANGE)) Is Nothing Then ' this i don't understand at all. is this a double negative? . . . wouldn't we want the range to intersect?


    Yes, it IS a logical double negative, but it does exactly what we need. Yes, we DO want the range to intersect, and that is why we execute the code when the intersection is NOT Nothing.

    The Intersect method returns a Range object if the Ranges represented by its arguments do in fact overlap, otherwise it returns Nothing - however, Nothing doesn't have any properties, so the only test you can perform is to check for NOT Nothing.



    sTextValue = Target.Text ' <-- does this save the cell value to the var as text format?

    Yes, it does, and yes you are right, Target.Value appears to work correctly also.



    and this "If sTextValue Like "##.##" Then" . . . . so i tried a single digit wildcard (Like "?#.##") and also (Like "##.##" or Like "#.##") and got an error.

    Sorry - mea culpa! To allow for the situation where a one-digit hour is entered as (e.g.) 01.23 or 1.23 further modifications to the code are required.


    The following code allows times to be entered with formats such as 12.34, 00.23, 12.30, 00.20, 22.00 and 02.00:

    Please Login or Register  to view this content.
    I've attached a workbook which incorporates the above code.


    Hope this helps - as before, please let me know how you get on.

    Regards,

    Greg M
    Last edited by Greg M; 03-25-2024 at 12:04 PM. Reason: Typo corrected

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Enter Time Values Using Decimal Instead Of Colon

    Hi again,

    Upon "mature reflection", the following approach might be a bit more straightforward:

    Please Login or Register  to view this content.

    Hope this helps also.

    Regards,

    Greg M
    Last edited by Greg M; 03-25-2024 at 12:55 PM. Reason: More Meaningful Title Added

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Message Box Fires Multiple Times
    By kyle4570 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-15-2021, 10:40 AM
  2. [SOLVED] My event macro fires once and then quits
    By dmcgov in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 10-18-2017, 12:02 PM
  3. TextBox_Enter() Only Fires Once
    By nva2k4 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 08-11-2016, 02:25 PM
  4. [SOLVED] Mouse fires SpinButton once, keyboard fires SpinButton twice
    By 79590 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-10-2015, 04:33 PM
  5. Worksheet_Change only fires when worksheet is unprotected
    By rlbush2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-28-2012, 01:36 PM
  6. Sub Fires on Worksheet Close
    By germ in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-21-2010, 02:31 PM
  7. Weekly graph of hires and fires
    By rpernack in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 10-22-2008, 11:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1