简体   繁体   English

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

[英]sort ascending column B and change matched column F vba excel

how can I sort ascending column B and accordingly change column F? 如何对B列进行升序排序并相应地更改F列? (column B and column F have a correspondence). (列B和列F具有对应关系)。 I don't want to sort columns A,C,D,E or all other columns. 我不想对A,C,D,E列或所有其他列进行排序。

Here is my code, but this sorts only column B and columns A or C (the columns close to the column I want to sort). 这是我的代码,但是只对B列和A或C列(靠近我要排序的列)进行排序。

    Range("B1").CurrentRegion.Select

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

you can move the column: 您可以移动该列:

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

Sort and move again. 排序并再次移动。
Or build a proper Sort... 或建立适当的排序...

If you have data in rows 2 thru 21 then: 如果在第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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM