[[VBA] How to Automatically Move to a Specified Cell After Pressing Enter]

目次

Background

In Excel, when input items are scattered, moving back and forth between cells can be tedious. In my case, the cells to be entered were scattered like “A1”, “C4”, and “F3”, and I felt it was a hassle to move with the mouse or arrow keys every time. Therefore, I implemented a method using VBA to automatically move to the next specified cell after entering a value and pressing the Enter key.

Specifications to Achieve

  • Enter a value in cell A1 and press Enter -> Automatically move to cell C4.
  • Enter a value in cell C4 and press Enter -> Automatically move to cell F3.
  • The order of cell movement can be set arbitrarily thereafter.
  • Works with VBA code for each worksheet (e.g., Sheet1).

Implementation Code (VBA)

Write the following code in the “Sheet1” module of the VBA editor.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$A$1" Then
        Me.Range("C4").Select
    ElseIf Target.Address = "$C$4" Then
        Me.Range("F3").Select
    End If
End Sub

This code is executed automatically whenever a cell value is changed.

Code Explanation

  • Worksheet_Change: This is an event that reacts when any cell in the target sheet is changed.
  • Target.Address: Retrieves the address of the changed cell.
  • If the cell matches the condition, you can move to the next input cell using Range("C4").Select.

Notes

  • Since it is executed when the content of the cell changes, it is not strictly linked to the operation of “pressing Enter”, but practically it works almost the same way.
  • If you want to use it on another worksheet, you need to add similar code to each sheet module.
  • Even if the destination cell already has a value, only the selection is executed.

Summary

Using VBA, you can create an action to automatically move to a specified cell after input is complete. Since you can freely set the order of cells, it can also be used to create input forms tailored to your work. It leads to improved input efficiency and reduced input errors. If you want to save the trouble of mouse operation or smooth the flow of template input, please try introducing it.

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

この記事を書いた人

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

目次