PDA

View Full Version : Solved: “Move Select After Enter” Problem



Cyberdude
03-13-2009, 05:17 PM
When I enter a value into a cell in column “C” I want the cursor to jump right 3 columns to column “F”. I use event processing to implement this abnormal cursor move:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ColNum&
Application.EnableEvents = False

ColNum = Target.Column
Select Case ColNum
Case 3: ActiveCell.Offset(0, 2).Select '<--Moves to column "F"
Case 6: ActiveCell.Offset(0, 2).Select
Case 9: ActiveCell.Offset(0, 2).Select
End Select

Application.EnableEvents = True
End Sub If I want the cursor to move right 3 columns, then the “OFFSET” command must specify 2 (not 3).
This code works as expected, except when I press the CLEAR button to clear the cell then the move to the right is one less cell (2 in this example) than I expected. I discovered that the problem is that when clearing the value from a cell it leaves the cursor over the cell. In this example, the number of cells that the cursor moves is one less than the “OFFSET” statement specifies.

So in normal operation, when I enter a value into a cell, then the cursor moves right by 1 cell. If I then clear the value from the cell, the cursor remains over the cell cleared.

Is this the way that Excel is supposed to work? How can I write the logic so that clearing a cell will cause the cursor to move the same number of cells as it does when I enter a value into the cell?

mikerickson
03-13-2009, 05:20 PM
Clear changes the cell, but doesn't move the cursor. This specifies that the cursor goes to a specific column.
Case Is = 3: Target.EntireRow.Range("F1").Select
Case Is = 6: Target.EntireRow.Range("I1").Select
Case Is = 9: Target.EntireRow.Range("L1").SelectI'd also add an If..EndIf so the code does not run if 1 < Target.Cells.Count

Cyberdude
03-13-2009, 07:40 PM
Hey, mik! Thanx loads. It works like a charm!

Sid

mdmackillop
03-14-2009, 04:30 AM
Hi Sid,
In these event codes, work with Target, rather than ActiveCell
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ColNum&
Application.EnableEvents = False

ColNum = Target.Column
Select Case ColNum
Case 3: Target.Offset(0, 3).Select '<--Moves to column "F"
Case 6: Target.Offset(0, 3).Select
Case 9: Target.Offset(0, 3).Select
End Select

Application.EnableEvents = True
End Sub