Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.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 VBA中滤波器的操作_Excel_Vba - Fatal编程技术网

Excel VBA中滤波器的操作

Excel VBA中滤波器的操作,excel,vba,Excel,Vba,我最近开始用VBA写作,多年来我用各种其他语言写作。我目前在使用Excel VBA中的过滤器时遇到了一些奇怪的问题,我想知道是否有人能对我所经历的行为提供一些线索 我想按数据集按多个不同的列进行筛选,一次一列,我通过将数据集复制到一个新的工作表并在那里对数据进行排序来实现这一点。对于我使用的第一个过滤器: Sheets("Temp Data").Range("A:T").ClearContents Sheets("Main Sheet").Range("A1", "T" & CountL

我最近开始用VBA写作,多年来我用各种其他语言写作。我目前在使用Excel VBA中的过滤器时遇到了一些奇怪的问题,我想知道是否有人能对我所经历的行为提供一些线索

我想按数据集按多个不同的列进行筛选,一次一列,我通过将数据集复制到一个新的工作表并在那里对数据进行排序来实现这一点。对于我使用的第一个过滤器:

Sheets("Temp Data").Range("A:T").ClearContents
Sheets("Main Sheet").Range("A1", "T" & CountLV_Rows).Copy Sheets("Temp Data").Range("A1", "T" & CountLV_Rows)
Sheets("Temp Data").Range("A1", "T" & CountLV_Rows).Sort Key1:=Range("R1"), Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
这是成功的。现在我想用C列中的值进行过滤,我重复上面的代码(包括
clearcontents
命令,因为我认为这会提高我的成功几率…只需将
Key1
值交换到
C1

对于第二个(希望是新的过滤器),我使用了:

然而,我的数据在第一次按列R排序之后,是按列C排序的

如何擦除以前应用的任何排序


感谢您的帮助

我认为这可能与您没有限定工作表和范围有关,即明确指定它们所在的工作簿或工作表。这是您一直想要做的事情

我已经完成了以下工作,并在Excel 2010中对我有效:

Sub test()
Dim CountLV_Rows As Long
Dim wbActive As Excel.Workbook

Set wbActive = ActiveWorkbook
With wbActive
    .Sheets("Temp Data").Range("A:T").ClearContents
    CountLV_Rows = .Sheets("Main Sheet").Range("A" & Rows.Count).End(xlUp).Row
    .Sheets("Main Sheet").Range("A1", "T" & CountLV_Rows).Copy _
            Destination:=.Sheets("Temp Data").Range("A1", "T" & CountLV_Rows)
    With .Sheets("Temp Data")
        .Range("A1", "T" & CountLV_Rows).Sort Key1:=.Range("R1"), Order1:=xlAscending, Header:=xlGuess, _
                                              OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                                              DataOption1:=xlSortNormal
    .Activate
    MsgBox "Sorted by R"
        .Range("A1", "T" & CountLV_Rows).Sort Key1:=.Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
                                              OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                                              DataOption1:=xlSortNormal
    End With
End With
End Sub

OrderCustom:=1
与此有关吗?您的意思是进行自定义排序吗?您是否可以添加您复制并将“R1”更改为“C1”的额外代码?
Sub test()
Dim CountLV_Rows As Long
Dim wbActive As Excel.Workbook

Set wbActive = ActiveWorkbook
With wbActive
    .Sheets("Temp Data").Range("A:T").ClearContents
    CountLV_Rows = .Sheets("Main Sheet").Range("A" & Rows.Count).End(xlUp).Row
    .Sheets("Main Sheet").Range("A1", "T" & CountLV_Rows).Copy _
            Destination:=.Sheets("Temp Data").Range("A1", "T" & CountLV_Rows)
    With .Sheets("Temp Data")
        .Range("A1", "T" & CountLV_Rows).Sort Key1:=.Range("R1"), Order1:=xlAscending, Header:=xlGuess, _
                                              OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                                              DataOption1:=xlSortNormal
    .Activate
    MsgBox "Sorted by R"
        .Range("A1", "T" & CountLV_Rows).Sort Key1:=.Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
                                              OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                                              DataOption1:=xlSortNormal
    End With
End With
End Sub