Đổi chỗ hai hàng trong Excel với nhau
Thứ Hai, 2 tháng 4, 2012
Trong Excel không có sẵn tính năng đối chỗ hai hàng 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 swapRows()
Dim xlong As Long
If Selection.Areas.Count <> 2 Then
MsgBox "Phai co chinh xac hai vung the doi cho." & Chr(10) _
& "Ban co " & Selection.Areas.Count & " vung."
Exit Sub
End If
If Selection.Areas(1).Columns.Count <> Cells.Columns.Count Or _
Selection.Areas(2).Columns.Count <> Cells.Columns.Count Then
MsgBox "Must select entire Rows, insufficient columns"
Exit Sub
End If
Dim areaSwap1 As Range, areaSwap2 As Range, onepast2 As Range
If Selection.Areas(1)(1).Row > Selection.Areas(2)(1).Row 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(areaSwap2.Rows.Count, 0).EntireRow
areaSwap2.Cut
areaSwap1.Resize(1).EntireRow.Insert Shift:=xlShiftDown
areaSwap1.Cut
onepast2.Resize(1).EntireRow.Insert Shift:=xlShiftDown
Range(areaSwap1.Address & "," & areaSwap2.Address).Select
xlong = ActiveSheet.UsedRange.Columns.Count correct lastcell
End Sub
- Quay trở lại Excel và đánh dấu chọn 02 hàng dữ liệu cần chuyển . Chú ý phải đánh dấu tất cả hàng
- Chạy Macro có tên là swapRows
Tags:
Office-Excel 2007
Comments[ 0 ]
Đăng nhận xét