Đổi chỗ hai cột trong Excel với nhau
Thứ Bảy, 31 tháng 3, 2012
Trong Excel không có sẵn tính năng đối chỗ hai cột mà người dùng phải sử dụng Macro , cách thức tiến hành như sau
- Mở Excel
- Bấm tổ hợp phím Alt-F11 , xuất hiện cửa sổ MS Visual Basic
- Bấm chọn menu Insert , chọn Module và bạn Copy nội dung toàn bộ dưới đây
Sub swapcolumns()
Dim xlong As Long
If Selection.Areas.Count <> 2 Then
MsgBox "Phai co hai vung chinh xac de doi cho." & Chr(10) _
& "Ban co " & Selection.Areas.Count & " vung."
Exit Sub
End If
If Selection.Areas(1).Rows.Count <> Cells.Rows.Count Or _
Selection.Areas(2).Rows.Count <> Cells.Rows.Count Then
MsgBox "Must select entire Columns, insufficient rows " _
& Selection.Areas(1).Rows.Count & " vs. " _
& Selection.Areas(2).Rows.Count & Chr(10) _
& "You should see both numbers as " & Cells.Rows.Count
Exit Sub
End If
Dim areaSwap1 As Range, areaSwap2 As Range, onepast2 As Range
If Selection.Areas(1)(1).Column > Selection.Areas(2)(1).Column Then
Range(Selection.Areas(2).Address & "," & Selection.Areas(1).Address).Select
Selection.Areas(2).Activate
End If
Set areaSwap1 = Selection.Areas(1)
Set areaSwap2 = Selection.Areas(2)
Set onepast2 = areaSwap2.Offset(0, areaSwap2.Columns.Count).EntireColumn
areaSwap2.Cut
areaSwap1.Resize(, 1).EntireColumn.Insert Shift:=xlShiftToRight
areaSwap1.Cut
onepast2.Resize(, 1).EntireColumn.Insert Shift:=xlShiftToRight
Range(areaSwap1.Address & "," & areaSwap2.Address).Select
xlong = ActiveSheet.UsedRange.Rows.Count correct lastcell
End Sub
- Quay trở lại Excel và đánh dấu chọn 02 cột dữ liệu cần chuyển . Chú ý phải đánh dấu tất cả cột này từ trên xuống dưới
- Chạy Macro có tên là swapcolumns
Tags:
Office-Excel 2007
Comments[ 0 ]
Đăng nhận xét