PDA

View Full Version : Controlling where cursor goes to next after using Enter key



stevespauldi
03-01-2009, 06:11 PM
I now have all of the following ‘sheet’ code in Sheet1:

****************************************************************
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
If (Module106.isReady = True) Then
Call Module106.GetGValue
End If
End Sub

'*******************

Private Sub Worksheet_Activate()
Range("A10").Select
End Sub

'**************

Private Sub Worksheet_Change(ByVal Target As Excel.Range)

If Range("x_total").Value = 2 Then
Run ("Ctrl_t_macro")
End If

If Range("xx_total").Value = 2 Then
Run ("Ctrl_t_macro")
End If

If Target.Cells.Count = 1 And Not Application.Intersect(Target, Range("O15:O58")) Is Nothing Then
ActiveCell.Offset(0, -5).Select
End If


End Sub

'*************

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

Dim strTitle As String
Dim strMsg As String
Dim lDVType As Long
Dim sTemp As Shape
Dim ws As Worksheet


If Not Intersect(Target, Range("F1:I4")) Is Nothing Then
ActiveSheet.Unprotect "password"

Application.EnableEvents = False
Set ws = ActiveSheet
Set sTemp = ws.Shapes("txtInputMsg")
On Error Resume Next
lDVType = 0
lDVType = Target.Validation.Type
On Error GoTo errHandler
If lDVType = 0 Then
sTemp.TextFrame.Characters.Text = ""
sTemp.Visible = msoFalse
Else
If Target.Validation.InputMessage <> "" Then
strTitle = Target.Validation.InputTitle & Chr(10)
strMsg = Target.Validation.InputMessage
With sTemp.TextFrame
.Characters.Text = strTitle & strMsg
.Characters.Font.Bold = False
.Characters(1, Len(strTitle)).Font.Bold = True
End With
sTemp.Visible = msoTrue
Else
sTemp.TextFrame.Characters.Text = ""
sTemp.Visible = msoFalse
End If
End If
GoTo Line2
End If



Application.EnableEvents = False
Set ws = ActiveSheet
Set sTemp = ws.Shapes("txtInputMsg")
sTemp.Visible = msoFalse

GoTo Line2



errHandler:
Application.EnableEvents = True
GoTo Line2

Exit Sub


Line2:
Application.EnableEvents = True

If Not Intersect(Target, Range("D16:D44, H16:H36, I15:I58, O15:O58")) Is Nothing Then
Application.MoveAfterReturnDirection = xlDown
Else
Application.MoveAfterReturnDirection = xlToRight
End If
Application.Protect "password"
End Sub

'***********************************************************

When the cursor gets to any field/cell between “O15 and O58” I want the cursor to go 1 row down and 5 columns to the left [thus the ActiveCell.Offset(1, -5).Select statement] after I’ve had the opportunity to enter data in the “O” field. This code works fine provided I actually make a change in the “O” field. If I don’t actually make a change in the “O” field (i.e. just press the Enter key to leave what’s there) then the cursor moves 1 row down only. I can live with this if I have to.

But, is there a way to get the cursor to move the way I want??

Note: I am NOT trying to contol where the cursor goes when using the arrow keys.

Many thanks,
Steve

nst1107
03-01-2009, 10:15 PM
Hi, Steve. Welcome to the forum. I believe this will get you what you want:Option Explicit
Sub ByPassControls()
'Run when workbook opens.
Application.OnKey "{ENTER}", "AlternateENTER"
Application.OnKey "~", "AlternateENTER"
End Sub
Sub AlternateENTER()
If Intersect(ActiveCell, [O15:O58]) Is Nothing Then
ActiveCell.Offset(1).Select
Else
ActiveCell.Offset(1, -5).Select
End If
End Sub
Sub RevertControls()
'Run when workbook closes.
Application.OnKey "{ENTER}"
Application.OnKey "~"
End Sub

Bob Phillips
03-02-2009, 02:13 AM
Just change the Line2 code to



Line2:

If Not Intersect(Target, Range("D1644, H16:H36, I15:I58, O15:O58")) Is Nothing Then
Target.Offset(1, 0).Select
Else
Target.Offset(0, 1).Select
End If
Application.EnableEvents = True
ActiveSheet.Protect "password"