Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Excel VBA在Excel工作表/表格网格中按enter键自定义光标流动/方向_Excel_Vba_Cell_Cursor Position_Worksheet - Fatal编程技术网

使用Excel VBA在Excel工作表/表格网格中按enter键自定义光标流动/方向

使用Excel VBA在Excel工作表/表格网格中按enter键自定义光标流动/方向,excel,vba,cell,cursor-position,worksheet,Excel,Vba,Cell,Cursor Position,Worksheet,我的专栏如下: Column A---------------Column B 100 ----------------- 500 200 ----------------- 600 AA ----------------- ABCD BB ----------------- DEFG CC ----------------- FF DD ----------------- GG EE -----------------II 300 ----------------- 700 400 -----

我的专栏如下:

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代码,而不是硬编相同的代码。

提示是使用工作表事件:

  • 单击Ctrl+R(项目浏览器)并单击工作表以显示工作表代码
  • 在右上组合框中选择适当的事件(激活或选择更改)
  • 将代码放在私有子。。。。在事件中结束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
选项显式
常量LBeg=5'行和列左上编辑区
常数CBeg=1
Const LEnd=8'行和列右下编辑区
常数CEnd=2
公共LCurr尽可能长
作为整数的公共CCurr
'在每个工作表激活中
专用子工作表_Activate()
Application.EnableEvents=False
LCurr=LBeg'转到左上角单元格
CCurr=CBeg
单元格(LBeg、CBeg)。选择
Application.EnableEvents=True
端接头
'在此工作表中每次更改所选内容时
专用子工作表\u选择更改(ByVal目标作为范围)
Application.EnableEvents=False
'*****************************************************
'当前单元格到达最后一个编辑行时,
'转到下一列中编辑区域的第一行。
'当到达最后一列时,编辑区域恢复为
'编辑区域中的第一列
'*****************************************************
如果LCurr=借出
如果CCurr
有人能帮我吗?