ヒントは、ワークシート イベントを使用することです。
- Ctrl+R (プロジェクト エクスプローラー) をクリックし、ワークシートをクリックしてワークシート コードを表示します。
- 右上のコンボ ボックスで適切なイベント (Activate または Selection_Change) を選択します。
- コードを Private sub ... の間に入れます。イベント内の End Sub
- イベント コードの前に、変数宣言だけを配置します。
コードを以下に示します
Option Explicit
Const LBeg = 5 ' Line and column top left edit area
Const CBeg = 1
Const LEnd = 8 ' Line and column bottom right edit area
Const CEnd = 2
Public LCurr As Long
Public CCurr As Integer
' In every worksheet activation
Private Sub Worksheet_Activate()
Application.EnableEvents = False
LCurr = LBeg ' goes to top left cell
CCurr = CBeg
Cells(LBeg, CBeg).Select
Application.EnableEvents = True
End Sub
' In every change of selection inside this worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
'*****************************************************
' When current cell reach last edit line,
' goes to first line of edit area in the next column.
' When reach last column edit area resumes to
' first column in edit area
'*****************************************************
If LCurr = LEnd Then
If CCurr < CEnd Then
LCurr = LBeg
CCurr = CCurr + 1
Else
LCurr = LBeg
CCurr = CBeg
End If
Cells(LCurr, CCurr).Select
End If
LCurr = Target.Row
CCurr = Target.Column
Application.EnableEvents = True
End Sub