VBA,电子表格,如何使光标下移一个单元格,而不论光标在什么位置?

2025-01-05 07:57:55
推荐回答(3个)
回答1:

===============

Sub 上移()
On Error Resume Next
i = Selection.Rows.Count
Selection.Cut
Selection.Offset(-1, 0).Insert
Selection.Offset(-1, 0).Select
End Sub
Sub 下移()
On Error Resume Next
i = Selection.Rows.Count
Selection.Cut
Selection.Offset(i 1, 0).Insert
Selection.Offset(1, 0).Select
End Sub
Sub 左移()
On Error Resume Next

Selection.Cut
Selection.Offset(0, -1).Insert
Selection.Offset(0, -1).Select
End Sub
Sub 右移()
On Error Resume Next
i = Selection.Columns.Count
Selection.Cut
Selection.Offset(0, i 1).Insert
Selection.Offset(0, 1).Select
End Sub
Sub addc()
For Each menua In Application.CommandBars '遍历工具栏,判断工具是否存在,有则删除
If menua.Name = "DT" Then Application.CommandBars("DT").Delete
Next
Dim 主菜单 As CommandBar
Set 主菜单 = Application.CommandBars.Add(temporary:=True)
With 主菜单
.Visible = True
.Name = "DT"
.Position = msoBarTop

'******************开始添加文件子菜单*********************

Set 命令按钮 = .Controls.Add
With 命令按钮
.Caption = "上移"
.FaceId = 134
.OnAction = "上移"
.Style = msoButtonIconAndCaptionBelow
End With
Set 命令按钮 = .Controls.Add
With 命令按钮
.Caption = "下移"
.FaceId = 135
.OnAction = "下移"
.Style = msoButtonIconAndCaptionBelow
End With
Set 命令按钮 = .Controls.Add
With 命令按钮
.Caption = "左移"
.FaceId = 132
.OnAction = "左移"
.Style = msoButtonIconAndCaptionBelow
End With
Set 命令按钮 = .Controls.Add
With 命令按钮
.Caption = "右移"
.FaceId = 133
.OnAction = "右移"
.Style = msoButtonIconAndCaptionBelow
End With
End With
End Sub

===============

  使用该功能,可以轻松实现数据的移动,免去了复制→粘贴的麻烦,值得大家学习

回答2:

1、可以通过VBA中的OFFSET方法,对当前选中单元格下移定位,然后选中新的单元格地址。

2、具体代码为:

Sub 下移一行()
    Selection.Offset(1, 0).Select
End Sub

3、代码解释:Selection为当前选中的单元格区域。Offset(1, 0)意思是向下偏移1行,向右偏移0列。Select是选中。

回答3:

有什么方法能让停在1行最后一个4的光标自动跳到2行最后一个数字2的后面,Private Sub Worksheet_SelectionChange(ByVal Target As Range) r = Active