无需交换标题即可将数据从列切换到另一列

时间:2015-10-21 11:36:59

标签: excel vba excel-vba

如何创建Excel宏以将数据从列切换到另一列?但我不想交换标题。

代码:

Sub myCode()
Dim r1 As Range
Dim r2 As Range
Dim v As Variant
Set r1 = Columns("A:B")
Set r2 = Columns("B:B")
v = r1.Value
r1.Value = r2.Value
r2.Value = v
End Sub

我的问题是它也是标题。我只需要交换数据。

1 个答案:

答案 0 :(得分:1)

跳过标题行

Sub dural()
   Dim A2 As Range, B2 As Range
   Set A2 = Range("A2:A" & Rows.Count)
   Set B2 = Range("B2:B" & Rows.Count)

   v = A2
   A2.Value = B2.Value
   B2 = v
End Sub