Consulting

Results 1 to 5 of 5

Thread: Determining if 'End Mode' is on

  1. #1
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location

    Determining if 'End Mode' is on

    I'm writing a special .OnKey for {Left} (plus others)

    I'd like to determine if End Mode is on, because the selected cell will be different

    There doesn't seem to be a status I can test, or at least that I can find

    The OnKey sub will look something like this

    [VBA]
    Sub My_Left()
    If ActiveSheet.ProtectContents Then
    If EndMode Then '<<<<<<<<<<<<<<<<<<<<
    ActiveCell.EntireRow.Cells(1, 2).Select
    Else
    ActiveCell.Offset(0, -1).Select
    End If
    Else
    ActiveCell.EntireRow.Cells(1, 1).Select
    End If
    End Sub
    [/VBA]

    Paul

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Have you tried GetKeyState?

    http://www.cpearson.com/excel/KeyTest.aspx

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    I use Chip's GetKeyState all the time, but as far as I can tell it will only tell you if the Alt, Ctrl, or Shift key is down.

    Excel has the End Mode 'state' where if you press [end] and then [down] it works like range.End(xlDown).Select

    Paul

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    This can be a bit wacky in the 2nd MsgBox. DoEvents can help a bit sometimes.

    [vba]Public Const VK_END = &H23

    Private Declare Sub keybd_event Lib "user32" _
    (ByVal bVk As Byte, ByVal bScan As Byte, _
    ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Declare Function GetKeyState Lib "user32" _
    (ByVal nVirtKey As Long) As Long

    Sub PressKey(theKey As Long)
    keybd_event theKey, 0, 0, 0 'press key
    keybd_event theKey, 0, &H2, 0 'release key
    End Sub

    Sub Test_IsEndModeOne()
    Dim tf As Boolean
    tf = IsEndModeOn
    MsgBox tf

    If tf = True Then PressKey VK_END
    tf = IsEndModeOn
    MsgBox tf
    End Sub

    Function IsEndModeOn() As Boolean
    IsEndModeOn = CBool(GetKeyState(VK_END))
    End Function
    [/vba]

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    I took the Msgbox's out, just be be sure.

    The subs below are the bare bones of what I'm trying to do

    It uses your suggestion, but the results are a little wacky (to use the technical term)

    Sometimes the correct cell is selected, sometimes not

    [vba]
    Option Explicit
    Public Const VK_END = &H23

    Private Declare Sub Keybd_Event Lib "user32" _
    (ByVal bVk As Byte, ByVal bScan As Byte, _
    ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Private Declare Function GetKeyState Lib "user32" _
    (ByVal nVirtKey As Long) As Long

    Sub PressKey(theKey As Long)
    Keybd_Event theKey, 0, 0, 0 'press key
    Keybd_Event theKey, 0, &H2, 0 'release key
    End Sub

    Function IsEndModeOn() As Boolean
    IsEndModeOn = CBool(GetKeyState(VK_END))
    End Function
    Sub init()
    Application.OnKey "{Left}", "My_Left"
    End Sub
    Sub done()
    Application.OnKey "{Left}"
    End Sub

    Sub My_Left()

    '[End] followed by [left] go to Col B in active row
    If IsEndModeOn Then
    DoEvents
    ActiveCell.EntireRow.Cells(1, 2).Select

    'Just [left] go to Col A in active row
    Else
    DoEvents
    ActiveCell.EntireRow.Cells(1, 1).Select
    End If
    End Sub
    [/vba]

    The actual application will be more elaborate than this, but it would be helpful to be able to differeniate between (for ex.) 'one cell left' and 'left to the first one with data' from the KB.

    Paul

Posting Permissions

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