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”时,它应该转到“ABCD”而不是转到单元格 300。

这是一个更大问题的简化版本。我有几个专栏。我需要一个动态的 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 回答