[Excel VBA] How to Automatically Move the Cursor Right After Input

目次

Background

Normally, when you enter a value in an Excel cell and press the Enter key, the cursor moves down (vertically). However, when entering data sequentially by row (e.g., filling out a record), it is often more efficient for the cursor to move horizontally.

In this article, I will implement a method using VBA to automatically move the cursor to the adjacent column (or a specific column) after entering a value.

Desired Specifications

  • Input in Column B $\rightarrow$ Cursor moves to Column C.
  • Input in Column C $\rightarrow$ D, D $\rightarrow$ E, etc. (Sequential move).
  • Input in Column G $\rightarrow$ Jump to Column L.

Implementation Code (VBA)

The following code is designed to be used within the Worksheet_Change event. You can place this directly in a Sheet module (e.g., Sheet1) or call it as a common subroutine from ThisWorkbook.

' This code goes into the Sheet module (e.g., Sheet1)
Private Sub Worksheet_Change(ByVal Target As Range)
    ' Call the logic to move the cursor
    MoveCursorRight Target
End Sub

' Logic to handle cursor movement
Public Sub MoveCursorRight(ByVal Target As Range)
    Dim nextColumn As String

    ' Check the column of the changed cell and determine the destination
    Select Case Target.Column
        Case 2 ' Column B
            nextColumn = "C"
        Case 3 ' Column C
            nextColumn = "D"
        Case 4 ' Column D
            nextColumn = "E"
        Case 5 ' Column E
            nextColumn = "F"
        Case 6 ' Column F
            nextColumn = "G"
        Case 7 ' Column G (Jump to L)
            nextColumn = "L"
        Case Else
            ' Do nothing for other columns
            Exit Sub
    End Select

    ' Select the target cell in the same row
    If nextColumn <> "" Then
        Target.Worksheet.Cells(Target.Row, nextColumn).Select
    End If
End Sub

Explanation of the Code

  1. Target.Column: Retrieves the column number of the cell that was just edited.
  2. Select Case: Sets the next destination column based on the current column.
  3. Target.Row: Keeps the row number the same while changing the column to move horizontally.
    • For example, entering data in B3 moves the selection to C3.

Tips for Application

  • Jumping Columns: As shown in the “Column G to L” example, you can use this method to skip over calculation columns or unused fields, jumping directly to the next input field.
  • Simple Offset: If you only need to move one cell to the right for every column, you can simplify the code using Offset:VBATarget.Offset(0, 1).Select
  • Form-like Experience: By combining this with data validation, you can create a user experience similar to filling out a form, where the cursor guides the user through the necessary fields.

Summary

  • VBA allows you to implement automatic horizontal cursor movement after cell entry.
  • Using Select Case enables flexible control, including jumping over specific columns.
  • Adjusting cursor movement to match the workflow significantly improves data entry efficiency.

I hope this helps optimize your Excel data entry tasks.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次