排序升序栏B并更改匹配列F vba excel

时间:2014-07-24 10:52:58

标签: excel vba

如何对升序列B进行排序并相应地更改列F? (列B和列F具有对应关系)。我不想对列A,C,D,E或所有其他列进行排序。

这是我的代码,但是这只排序列B和列A或C(靠近我要排序的列的列)。

    Range("B1").CurrentRegion.Select

    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
    OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal

2 个答案:

答案 0 :(得分:0)

你可以移动专栏:

Columns("F:F").Select
Selection.Cut
Columns("C:C").Select
Selection.Insert Shift:=xlToRight

Range("B:C").Select

Selection.Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Columns("C:C").Select
Selection.Cut
Columns("G:G").Select
Selection.Insert Shift:=xlToRight

排序并再次移动。
或者建立一个合适的排序......

答案 1 :(得分:0)

如果您的行数 2 21 ,那么:

Sub SortIt()
    Dim i As Long, J As Long, Low As Long, Hi As Long
    Dim Temp As Variant
    Low = 2
    Hi = 21
      J = (Hi - Low + 1) \ 2
      Do While J > 0
        For i = Low To Hi - J
          If Cells(i, 2) > Cells(i + J, 2) Then
            Temp = Cells(i, 2)
            Cells(i, 2) = Cells(i + J, 2)
            Cells(i + J, 2) = Temp
            Temp = Cells(i, 6)
            Cells(i, 6) = Cells(i + J, 6)
            Cells(i + J, 6) = Temp
          End If
        Next i

        For i = Hi - J To Low Step -1
          If Cells(i, 2) > Cells(i + J, 2) Then
            Temp = Cells(i, 2)
            Cells(i, 2) = Cells(i + J, 2)
            Cells(i + J, 2) = Temp
            Temp = Cells(i, 6)
            Cells(i, 6) = Cells(i + J, 6)
            Cells(i + J, 6) = Temp
          End If
        Next i
        J = J \ 2
      Loop
End Sub