双重组织不起作用

时间:2012-08-28 14:01:49

标签: excel excel-vba vba

我有一个脚本:

Sub wsPOT()
    Dim wsPOT As Worksheet
    Dim cel As Range
    Dim lastrow As Long, lastrow2 As Long, fstcell As Long, i As Long, Er As Long, lstCol As Long, lstRow As Long

    Set wsPOT = Sheets("PO Tracking")

    With wsPOT
        wsPOT.Range("Q1:U1").Copy
        lastrow = wsPOT.Cells(Rows.Count, "B").End(xlUp).Row
            wsPOT.Range("V1:X1").Copy wsPOT.Range("H3:J" & lastrow)
            wsPOT.Range("N2:O2").Copy wsPOT.Range("N3:O" & lastrow)
            wsPOT.Range("P1:V1").Copy
            wsPOT.Range("B3:H" & lastrow).PasteSpecial xlPasteFormats
            wsPOT.Range("K3:K" & lastrow).Borders.Weight = xlThin


        lastrow = wsPOT.Cells(Rows.Count, "B").End(xlUp).Row
            wsPOT.Range("H:J").Calculate
            wsPOT.Range("B3:K" & lastrow).Sort key1:=Range("H3:H" & lastrow), order1:=xlAscending
        lastrow2 = wsPOT.Cells(Rows.Count, "H").End(xlUp).Row
            wsPOT.Range("B3:K" & lastrow2).Sort key1:=Range("H3:H" & lastrow2), order1:=xlDescending
    End With

End Sub

这意味着最终组织工作表,以便后期工作位于最前面,然后由最老的工作组织到最年轻。

似乎没有用。似乎发生的事情是第一次组织工作正常,但它似乎忽略了第二个标准。

附件是带有脚本的工作表。

https://dl.dropbox.com/u/3327208/Excel/Orga.xlsm

如果有人可以帮助我们看看它,那么它就会被贬低。

1 个答案:

答案 0 :(得分:2)

这是你在尝试的吗?

Option Explicit

Sub wsPOT()
    Dim wsPOT As Worksheet
    Dim cel As Range
    Dim lastrow As Long, lastrow2 As Long, fstcell As Long
    Dim i As Long, Er As Long, lstCol As Long, lstRow As Long

    Set wsPOT = Sheets("PO Tracking")

    With wsPOT
        .Range("Q1:U1").Copy
        lastrow = .Cells(.Rows.Count, "B").End(xlUp).Row
        .Range("V1:X1").Copy .Range("H3:J" & lastrow)
        .Range("N2:O2").Copy .Range("N3:O" & lastrow)
        .Range("P1:V1").Copy
        .Range("B3:H" & lastrow).PasteSpecial xlPasteFormats
        .Range("K3:K" & lastrow).Borders.Weight = xlThin
        lastrow = .Cells(.Rows.Count, "H").End(xlUp).Row
        .Range("H:J").Calculate

        .Sort.SortFields.Clear

        '~~> Sort on Red Icon First, putting it on top
        .Sort.SortFields.Add(.Range("J3:J" & lastrow), _
        xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
        IconSets(4).Item(1)

        '~~> Sort on Values of Red icon in descending order
        .Sort.SortFields.Add Key:=Range( _
        "J3:J" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal

        '~~> Sort on Green Icon Next, putting it on top after Red
        .Sort.SortFields.Add(.Range("I3:I" & lastrow), _
        xlSortOnIcon, xlAscending, , xlSortNormal).SetIcon Icon:=ActiveWorkbook. _
        IconSets(4).Item(3)

        '~~> Sort on Values of Green icon next in descending order
        .Sort.SortFields.Add Key:=Range( _
        "I3:I" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal

        With .Sort
            .SetRange wsPOT.Range("B2:K" & lastrow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub