0

次のような列があります。

Column A---------------Column B
100 ----------------- 500
200 ----------------- 600
AA ----------------- ABCD
BB ----------------- DEFG
CC ----------------- FF
DD ----------------- GG
EE -----------------II
300 ----------------- 700
400 ----------------- 800

ワークシートを開いたときに常に「AA」のセル位置にカーソルを表示し、「DD」に到達すると、セル 300 ではなく「ABCD」に移動する必要があります。

これは、より大きな問題の単純化されたバージョンです。いくつかのコラムがあります。同じものをハードコーディングするのではなく、動的な Visual Basic コードが必要です。

4

1 に答える 1

0

ヒントは、ワークシート イベントを使用することです。

  • 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
于 2010-11-06T05:13:34.333 に答える