Offsets based on key stroke or prior cursor posistion

lythander

New Member
Joined
May 4, 2005
Messages
37
All:

I am still attempting to get a set of 5 cells to work like radial buttons. The system I currently have *works* but only if you exit a cell by pressing enter or the down arrow. In an attempt to make the code more thorough, I thought it would be nice if they can push any button.

I believe there are two ways to do this. I would prefer the first solution, but I don't even know where to begin coding it.

Solution One: The Ideal Solution

First would be some bit of code that can tell me where I *was* focused. For example, if I was in A1, and preseed enter, or the right arrow key, the system would know it came from A1. This would be the ideal solution, but I don't believe it's possible in a Worksheet_Change situation. The reason it's ideal is it would work even with mouse clicks.

The code would start off with that location stored as a cell row and column, so that I could return to it and offset accordingly.

Solution Two: My Current Attempt

The other option I have, is to determine if the up arrow or right arrow was used. But, the code I currently have is giving me a variable not defined for this line (Private Sub Worksheet_Change(ByVal Target As Range)) But this solution wouldn't work for mouse clicks, it *is* better though.

As always, I thank you for your insight. I'm still a novice in the coding world, but I get better everytime I visit here!

Here's the first part of the code
Code:
Option Explicit

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

Private Const VK_UP = &H26
Private Const VK_RIGHT = &H27
Private Sub Worksheet_Change(ByVal Target As Range)

Application.EnableEvents = False
Application.ScreenUpdating = False

If GetKeyState(VK_RIGHT) = True Then
     MsgBox "You pressed the right arrow." 'this just here for validation
     Direction = 1 'direction is right
Else
If GetKeyState(VK_UP) = True Then
     MsgBox "You did not press the up arrow" 'this just here for validation
     Direction = 2 'direction is Up
Else
Direction = 3 'direction is down or they pressed enter
End If
End If
End If

If Target.Column = 3 Then

'Determine what you need to do based on direction pressed
If Direction = 3 Then 'down arrow or enter
    ActiveCell.Offset(-1, 1).Select
    ActiveCell.FormulaR1C1 = ""
Else
If Direction = 2 Then 'up
    ActiveCell.Offset(1, 1).Select
    ActiveCell.FormulaR1C1 = ""
Else
If Direction = 1 Then 'right
    ActiveCell.FormulaR1C1 = ""
End If
End If
End If

        For x = 1 To 3
            ActiveCell.Offset(0, 1).Select
            ActiveCell.FormulaR1C1 = ""
        Next x
    
    ActiveCell.Offset(1, -4).Select

'Repeat for columns 4, 5 and 6 with minor posistion changes, all of this 'code works
'Else
'If Target.Column = 4 Then
'    ActiveCell.Offset(-1, -1).Select
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this :-
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
    Static LastCell As String
    MsgBox ("Current Cell = " & Target.Address & vbCr & "Last Cell was " & LastCell)
    LastCell = Target.Address
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,369
Messages
6,130,209
Members
449,567
Latest member
ashsweety

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top