gpt4 book ai didi

excel - Vba用按钮上下移动行,第二行除外

转载 作者:行者123 更新时间:2023-12-04 21:51:58 25 4
gpt4 key购买 nike

嘿,我希望你能帮助我做到这一点。

我需要一个 VBA 代码来向上或向下移动具有激活单元格的行。第二列应该被卡住不动。

数据看起来像 Data excel
优先级应该被卡住,其余的应该上下移动。

我有这段代码,但它移动了整行。

Sub MoveDown()
Selection.EntireRow.Select
Selection.Cut
ActiveCell.Offset(2, 0).Range("A1").Select
Selection.EntireRow.Select
Selection.Insert Shift:=xlDown
ActiveCell.Offset(-1, 0).Range("A1").Select
End Sub

Sub MoveUp()
Selection.EntireRow.Select
Selection.Cut
ActiveCell.Offset(-1, 0).Range("A1").Select
Selection.EntireRow.Select
Selection.Insert Shift:=xlDown
End Sub

先感谢您。
最好的,

最佳答案

B 列无需更改公式:

Public Sub moveRowUp()
If Selection.row <> 2 Then
'move the whole row
Selection.EntireRow.Cut
Selection.Offset(-1, 0).EntireRow.Insert shift:=xlDown
'move column 2 back
Selection.EntireRow.Cells(1, 2).Cut
Selection.EntireRow.Cells(1, 2).Offset(-1, 0).Insert shift:=xlDown
End If
End Sub

Public Sub moveRowDown()
If Selection.row <> 1 Then
'move the whole row
Selection.EntireRow.Cut
Selection.Offset(2, 0).EntireRow.Insert shift:=xlDown
'move column 2 back
Selection.EntireRow.Cells(1, 2).Cut
Selection.EntireRow.Cells(1, 2).Offset(2, 0).Insert shift:=xlDown
End If
End Sub

关于excel - Vba用按钮上下移动行,第二行除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52481019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com