下面是我的excel数据:
A B c ... F G
1 test vb1 testing1 open
2 test1 vb2 testing1 close
2 test2 vb3 testing1
4 test3 vb4 testing1
我想将F列数据移动到B列,并将B列数据右移;所以B之前是C。
我该如何使用 Excel VBA 编程来做到这一点?
尝试这个:
Option Explicit
Sub MoveColumns()
With ActiveSheet
.Columns("F:F").Cut
.Columns("B:B").Insert Shift:=xlToRight
.Columns("C:C").Cut
.Columns("E:E").Insert Shift:=xlToRight
End With
Application.CutCopyMode = False
End Sub
要使用这个:
- 打开 Visual Basic 编辑器:工具 > 宏 > Visual Basic 编辑器
- 插入模块(右键单击 VBAProject 并插入 > 模块)
- 将上述代码粘贴到这个新模块中。
然后,您可以从 Excel 执行代码:工具 > 宏... > 宏...
[编辑]再次尝试不复制粘贴
Option Explicit
Sub copyWithArray()
Dim lLastrow As Long
Dim aValues As Variant
With ActiveSheet
lLastrow = .Cells(.Rows.Count, "AE").Row
'store data from the column F
aValues = .Range("F1:F" & lLastrow).Value
'"move" data a column further
.Range("C1:AE" & lLastrow).Value = .Range("B1:AD" & lLastrow).Value
'copy data from column C to column B
.Range("B1:B" & lLastrow).Value = .Range("C1:C" & lLastrow).Value
'restore data copied from column F to column B
.Range("B1:B" & lLastrow).Value = aValues
End With
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)