简体   繁体   English

在 VBA excel 中运行排序功能

[英]Running sort function in VBA excel

I am running the following code below across multiple tabs, both criteria1 and criteria2 work fine but when I add crietria3 I get the error我在多个选项卡上运行以下代码, criteria1criteria2可以正常工作,但是当我添加crietria3时出现错误

"Named Argument not found" “未找到命名参数”

Basically, The first part of the code (before the criteria1 code) is just a simple division of figures from the first cell and that gets converted into percentages.基本上,代码的第一部分(在criteria1代码之前)只是对第一个单元格中的数字进行简单划分,然后将其转换为百分比。

  • criteria1 Sorts one column and extracts cells with the specific figures listed below. criteria1 1 对一列进行排序并提取具有下面列出的特定数字的单元格。
  • Criteria2 Hides unnecessary columns that I don't need. Criteria2隐藏我不需要的不必要的列。

  • Criteria3 then proceeds to Filter the AD column by largest to smallest and that is where the code breaks with the "Named Argument not found" error. Criteria3然后继续按从大到小的过滤AD列,这就是代码因“未找到命名参数”错误而中断的地方。

Can you please help?你能帮忙吗?

Sub Macro2()

    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        ws.Range("AD1").Value = "In %"
        ws.Range("AD1").Font.Bold = True
        With ws.Range("AD2:AD91")
            .FormulaR1C1 = "=RC[-2]/R2C28"
            .Style = "Percent"
            .NumberFormat = "0.0%"
            .Font.Bold = True
            ws.Range("A1:AD91").AutoFilter Field:=7, Criteria1:=Array("11", "21", "22""23", "31-33", "42", "44-45", "48-49", "51", "52", "53", "54", "55", "56", "61", "62", "71", "72", "81"), Operator:=xlFilterValues, Criteria2:=ws.Range("A:A,I1,F:F,C:E,I:AA").EntireColumn.Hidden = True, Criteria3:=ws.Range("AD1"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= xlSortNormal
        End With
    Next ws
    Application.ScreenUpdating = False
End Sub

I separated your first criteria into an array for better readability when filtering for these values.在过滤这些值时,我将您的第一个条件分成了一个数组,以提高可读性。

This should loop through sheets, hide columns (I removed this I1 , typo?), filter by your Arr1 , and then sort values in column AD in descending order.这应该遍历工作表,隐藏列(我删除了这个I1 ,错字?),按您的Arr1过滤,然后按降序对 AD 列中的值进行排序。

You also end your sub by turning off screen updating.您还可以通过关闭屏幕更新来结束您的订阅。 I'm not sure if that was intentional or not.我不确定这是故意的还是无意的。 Updated here to turn off ScreenUpdating before loop starts, and re-enabled at end of sub.在此处更新以在循环开始之前关闭ScreenUpdating ,并在子结束时重新启用。

Option Explicit
Sub Macro2()

Dim ws As Worksheet
Dim Arr1

Arr1 = Array("11", "21", "22", "23", "31-33", "42", "44-45", "48-49", "51", "52", "53", "54", "55", "56", "61", "62", "71", "72", "81")

'Application.ScreenUpdating = False
    For Each ws In ThisWorkbook.Worksheets
        ws.Range("A:A,C:F,I:AA").EntireColumn.Hidden = True
        ws.Range("AD1").Value = "In %"
        ws.Range("AD1").Font.Bold = True
            With ws.Range("AD2:AD91")
                .FormulaR1C1 = "=RC[-2]/R2C28"
                .Style = "Percent"
                .NumberFormat = "0.0%"
                .Font.Bold = True
            End With
        ws.Range("A1:AD91").AutoFilter Field:=7, Criteria1:=Arr1
        ws.Range("A1:AD91").Sort Range("AD2"), xlDescending, Header:=xlYes
    Next ws
'Application.ScreenUpdating = True

End Sub

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

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