+ Reply to Thread
Results 1 to 7 of 7

why doesn't SendKeys work consistently?

Hybrid View

Guest why doesn't SendKeys work... 12-05-2005, 05:30 PM
Guest Re: why doesn't SendKeys work... 12-05-2005, 10:55 PM
Guest Re: why doesn't SendKeys work... 12-06-2005, 09:45 AM
tony h send keys is dificult to get... 12-06-2005, 10:04 AM
Flopy75 Re: why doesn't SendKeys work... 11-12-2022, 12:23 PM
FDibbins Re: why doesn't SendKeys work... 11-13-2022, 12:58 AM
JEC. Re: why doesn't SendKeys work... 11-12-2022, 12:49 PM
  1. #1
    crimsonkng
    Guest

    why doesn't SendKeys work consistently?

    In my macro, I need to use the SendKeys command for various reasons. But
    it's wierd: sometimes SendKeys works like I programmed them, sometimes just
    some of the SendKeys commands work, sometimes Excel "calls" the SendKeys in a
    different order than from what I've programmed, and sometimes SendKeys
    doesn't work at all. All of the above can happen when
    1. I run the macro using a keyboard command or
    2. if I run the macro by merely clicking "Run" on the macro list form or
    3. if I do an F8 within Visual Basic. (Actually, it seems like SendKeys
    never works when I do the F8 thing.)

    For example, here's a simple Macro that just won't run consistently/correctly:

    SendKeys "%(=)" 'inputs an AutoSum (Alt-equals)
    SendKeys "{ENTER}"
    ActiveCell.Offset(-1, 1).Range("A1").Select
    Selection.Copy
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveSheet.Paste
    ActiveCell.Offset(0, -1).Range("A1").Select
    SendKeys "{END}"
    SendKeys "{UP}"
    SendKeys "{UP}"

    I can't get a handle on it. Obviously, I'm doing something wrong. Any
    hints/suggestions?

    Dan

  2. #2
    Helmut Weber
    Guest

    Re: why doesn't SendKeys work consistently?

    Hi,

    sendkeys is notoriously unreliable anway.

    It sends keystrokes to the active window.

    And when you are in the VBA editor,
    the keystrokes are sent to the editor window,
    as this is the active window.

    Helmut Weber



  3. #3
    K Dales
    Guest

    Re: why doesn't SendKeys work consistently?

    You can try an AppActivate statement just before the SendKeys, e.g:
    AppActivate "Microsoft Excel"
    SendKeys "{END}"
    etc...

    But this is still not reliable in a multitasking environment since other
    apps may steal focus even after the AppActivate and then the SendKeys goes to
    the wrong app (the one with focus at the time the statement is called). I
    have big headaches with my email notifications coming up and stealing focus,
    so I have learned to avoid SendKeys.

    Please note (if you were not aware) that your code could all be done with no
    SendKeys:
    ActiveCell. "%(=)" 'inputs an AutoSum (Alt-equals
    With ActiveCell
    Set SumRange = Range(.Cells(0,1),.Cells(0,1).End(xlUp))
    .Formula = "=SUM(" & SumRange.Address & ")"
    .Offset(-1, 1).Copy
    .Offset(1, 0).Range("A1").PasteSpecial xlPasteAll
    End With
    SumRange.Cells(0,1).Select ' equivalent to your {END}{UP}{UP} due to def'n
    of SumRange
    --
    - K Dales


    "Helmut Weber" wrote:

    > Hi,
    >
    > sendkeys is notoriously unreliable anway.
    >
    > It sends keystrokes to the active window.
    >
    > And when you are in the VBA editor,
    > the keystrokes are sent to the editor window,
    > as this is the active window.
    >
    > Helmut Weber
    >
    >
    >


  4. #4
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    send keys is dificult to get right but judicious use of ensuring the window to which you want to send is ACTIVE and activating it if not.

    The thing is that SENDKEYS sends to the active window so if you are doing an F8 I presume you are in the VBA window and so it will be the VBA window that recieves your typing.

    It won't be a problem with the code not doing things the same

  5. #5
    Registered User
    Join Date
    04-16-2019
    Location
    Lyon
    MS-Off Ver
    2016
    Posts
    4

    Re: why doesn't SendKeys work consistently?

    How do I activate the Excel sheet I am on please?
    I use SendKeys "{F2}" but it opens the VBA library

  6. #6
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: why doesn't SendKeys work consistently?

    Quote Originally Posted by Flopy75 View Post
    How do I activate the Excel sheet I am on please?
    I use SendKeys "{F2}" but it opens the VBA library
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  7. #7
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,505

    Re: why doesn't SendKeys work consistently?

    Close vba editor first

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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