人气 3933

[Excel技巧] excel vba交换两个区域中的数据 [复制链接]

324664 2017-10-31 16:50:51
在excel工作表中,我们有时录入的数据可能出错,需要对两个区域的内容进行交换。可以通过另一个的区域,直接拖动移动数据。但是如果两个区域不相邻,拖动会相当麻烦。这里我们用vba代码来处理互换两区域中的数据。
在工作表中选择两个形状相同的、无公共部分的区域,然后执行代码即可

1509440516951992.gif

1509440516951992.gif

详细源码:SubTwoAreasSwap()
DimTheArea1,TheArea2AsVariant
IfSelection.Areas.Count2Then
MsgBox"请选择两个区域!"
ExitSub
ElseIfSelection.Areas(1).Cells.CountSelection.Areas(2).Cells.CountOrSelection.Areas(1).Rows.CountSelection.Areas(2).Rows.CountThen
MsgBox"请选择两个形状相同的区域!"
ExitSub
Else
TheArea1=Selection.Areas(1).Cells
TheArea2=Selection.Areas(2).Cells
Selection.Areas(1).Cells=TheArea2
Selection.Areas(2).Cells=TheArea1
EndIf
EndSub
您需要登录后才可以回帖 登录 | 立即注册

QQ|手机版|精益人 ( 沪ICP备19004111号-1 )

GMT+8, 2024-5-19 02:26 , Processed in 0.253422 second(s), 22 queries .

Powered by Lean.ren X3.5 Licensed  © 2001-2030 LEAN.REN