使用Excel VBA向右移动列数据 [英] Column data moving to right with Excel VBA

查看:2181
本文介绍了使用Excel VBA向右移动列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的excl数据:

Below is my excl data:

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。

I want to move F column data to B column and shift B column data to right ,so Before B will be C.

请使用Excel VBA编程。

How can i do with this using Excel VBA programming.

谢谢,

Chaitu

推荐答案

尝试这样:

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执行代码:工具>宏...>宏...

You can then execute the code from Excel: Tools > Macro... > Macros...

另一个没有复制粘贴的尝试

Another try without copy-pasting

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

这篇关于使用Excel VBA向右移动列数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆