Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在IF条件下对唯一值进行排序(在不同工作表之间工作)_Excel_Vba_Sorting_If Statement - Fatal编程技术网

Excel 在IF条件下对唯一值进行排序(在不同工作表之间工作)

Excel 在IF条件下对唯一值进行排序(在不同工作表之间工作),excel,vba,sorting,if-statement,Excel,Vba,Sorting,If Statement,我有一本工作簿,上面有以下两页。在表1中有数据 | Order | Date | Status | |-------|---------:|-------:| | 78 | 19-04-19 | OK | | 33 | 19-04-19 | OK | | 198 | 19-04-19 | CL | | 43 | 19-04-19 | CL | | 29 | 19-04-19 | CL | | 12 |

我有一本工作簿,上面有以下两页。在表1中有数据

| Order |     Date | Status | 
|-------|---------:|-------:|
| 78    | 19-04-19 | OK     | 
| 33    | 19-04-19 | OK     | 
| 198   | 19-04-19 | CL     | 
| 43    | 19-04-19 | CL     | 
| 29    | 19-04-19 | CL     | 
| 12    | 20-04-19 | CL     | 
| 169   | 20-04-19 | OK     | 
| 95    | 20-04-19 | OK     | 
| 54    | 20-04-19 | OK     | 
| 31    | 20-04-19 | OK     | 
我试图实现的是在Status=OK的条件下对“Order”唯一值进行排序。到目前为止,在一点帮助下,我成功地为没有条件的情况编写了代码。我试图整合下面的条件,但我不断得到错误

Sub SortUniqueValues2()

Dim i As Variant
Dim j As Variant

With Sheets("Sheet1")
LRow = .Cells(.Rows.Count, "P").End(xlUp).Row

For Each cell In .Range("P2:P" & LRow)
If cell.Value = "OK" Then
j = Application.Transpose(.Range("H2", .Range("H" & Rows.Count).End(xlUp)))

End With

    With CreateObject("Scripting.Dictionary")
        For Each i In j
            .Item(i) = i
        Next
     Cells(3, 21).Resize(.Count) = Application.Transpose(.Keys)
    End With

End If

Range("T3:AF100000").Sort Key1:=Range("T3"), Order1:=xlAscending, Header:=xlNo

End Sub
结果应该是这样的:

| Order |
|-------|
| 31    |
| 33    |
| 54    |
| 78    |
| 95    |
| 169   |
我试图找出以下几点:

如何将状态条件集成到代码中? 有没有办法创建某种下拉菜单来打开/关闭条件? 如何将A3:M100000转换为A3,直到M列中的最后一个值?
提前谢谢你

我不知道你在不同的工作表之间工作是什么意思。在你的问题和你的代码中,我只看到对一张纸的引用

但有一些内置函数可以完成大部分您想要的功能:

复制到结果表以保持源数据不变 移除的副本 分类 自动过滤器 我不确定你想要什么,但如果你设置了自动过滤,你可以使用箭头键过滤状态

例如:

Option Explicit
Sub sortUniqueOK()
    Dim wsSrc As Worksheet, wsRes As Worksheet, R As Range

Set wsSrc = Worksheets("Sheet3")
Set wsRes = Worksheets("Sheet4")

'Get the source data range
'Modify to your data location (Cells(1,1) = A1) for the upper left cell
With wsSrc
    Set R = .Range(.Cells(1, 1), .Cells(.Rows.Count, 3).End(xlUp))
End With

'Copy to a results worksheet
With wsRes
    .Cells.Clear
    R.Copy .Cells(1, 1)

    'set R to the range on the new worksheet
    Set R = .Range(.Cells(1, 1), .Cells(.Rows.Count, 3).End(xlUp))
End With

'Remove Duplicates
R.RemoveDuplicates Columns:=Array(1, 2, 3), Header:=xlYes

'Sort
With wsRes.Sort.SortFields
    .Clear
    .Add Key:=R(columnindex:=3), _
            SortOn:=xlSortOnValues, _
            Order:=xlAscending
    .Add Key:=R(columnindex:=1)
End With

With wsRes.Sort
    .SetRange R
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

'To just show the `OK` status features, you can use an autofilter
R.AutoFilter field:=3, Criteria1:="OK"

End Sub

您也可以使用Power Query实现相同的结果

这肯定不是您的代码吗?If有不匹配的If/end If,missing next,Lost end with,它将无法编译。@GSerg我知道它不起作用。问题是我如何做到这一点。谢谢,非常感谢!我最近开始研究VBA,这真的很有帮助!