您的位置:程序门 -> vb -> vba



vba怎么交换两行数据?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vba怎么交换两行数据?
发表于:2007-04-14 22:10:06 楼主
excel中的两行数据,在不占用其他行的情况下,交换它们的数据?

或者,我有300行数据,想交换每隔3行的数据,但只交换两行中前面n个cell的数据
怎么写,谢谢
发表于:2007-04-16 09:40:501楼 得分:0
剪切第二行,粘到第一行前面,剪切第一行,粘到第二行的位置、
发表于:2007-04-16 09:43:142楼 得分:0
如果表不是很大干脆把整个表读到数组里然后再往表里写
这时候次序就由你掌控了
发表于:2007-04-16 10:19:173楼 得分:0
每隔3行?
1 <--> 4
2 <--> 5
3 <--> 6
4 <--> 7
...
这样换?
sub   dd()
        dim   c1,   c2,   maxrow   as   long
        maxrow   =   300
        for   x   =   1   to   maxrow   -   3
                c1   =   sheet1.cells(x,   1)
                c2   =   sheet1.cells(x,   2)
                sheet1.cells(x,   1)   =   sheet1.cells(x   +   3,   1)
                sheet1.cells(x,   2)   =   sheet1.cells(x   +   3,   2)
                sheet1.cells(x   +   3,   1)   =   c1
                sheet1.cells(x   +   3,   2)   =   c2
        next
end   sub
发表于:2007-04-16 10:20:564楼 得分:0
这样写   运行大概会快点
sub   dd()
        dim   c1,   c2,   maxrow   as   long
        maxrow   =   300
        with   sheet1
        for   x   =   1   to   maxrow   -   3
                c1   =   .cells(x,   1)
                c2   =   .cells(x,   2)
                .cells(x,   1)   =   sheet1.cells(x   +   3,   1)
                .cells(x,   2)   =   sheet1.cells(x   +   3,   2)
                .cells(x   +   3,   1)   =   c1
                .cells(x   +   3,   2)   =   c2
        next
        end   with
end   sub
发表于:2007-04-16 10:21:125楼 得分:0
sub   dd()
        dim   c1,   c2,   maxrow   as   long
        maxrow   =   300
        with   sheet1
        for   x   =   1   to   maxrow   -   3
                c1   =   .cells(x,   1)
                c2   =   .cells(x,   2)
                .cells(x,   1)   =   .cells(x   +   3,   1)
                .cells(x,   2)   =   .cells(x   +   3,   2)
                .cells(x   +   3,   1)   =   c1
                .cells(x   +   3,   2)   =   c2
        next
        end   with
end   sub


快速检索

最新资讯
热门点击