VBA - 将AutoFilter应用于所有表格,但在不同的列中

时间:2018-05-18 11:54:04

标签: excel vba excel-vba

我需要在工作簿中应用格式和过滤器4张 - 对于前3张,格式和过滤器需要应用于列B和C列中的最后一张。

所以我想这样做 - 我搜索列标题,如果找到,请将格式应用于该列,然后应用过滤器。

代码:

Sub DateFilter()

    'Initialize variables
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim c As Range
    Dim LastRow As Long
    Dim Current_Date As Date
    Dim y As Workbook
    Dim WshtNames As Variant, WshtNameCrnt As Variant

    Set y = Workbooks("workbook.xlsm")

    For Each ws In y.Worksheets

        With ws

            bFound = False

            'Search for the Word "Status" on the first row of the Sheet
            Set StatusFound = ws.Rows(1).Find(What:="Employee End Date", LookAt:=xlWhole)

            'If Status is found then apply filter
            If Not StatusFound Is Nothing Then

                For a = .UsedRange.Columns.Count To 2 Step -1

                    If .Cells(1, a).Value = "Employee End Date" Then

                        'Loop through all celss in column B and change format to date
                        'For Each c In ActiveSheet.Range("C2:C" & LastRow).Cells

                            Current_Date = CDate(a)
                            a.Value = Current_Date

                        'Next c


                        'Make all cells in column A "General" input
                        Columns(1).NumberFormat = "General"

                        ' Set the autofilter to display all dates other than yesterdays
                        ws.UsedRange.AutoFilter Field:=1, Criteria1:="<" & CLng(DateAdd("d", -1, Date)), Operator:=xlOr, Criteria2:=">" & CLng(DateAdd("d", -1, Date))

                        bFound = True

                    End If

                Next a

            End If
        End With
     Next

End Sub

但是在这一行a.Value = Current_Date上我也得到一个对象定义的错误。

注意:在最后一个例子中,我在If语句中注释了for循环,因为我认为这种情况不是必需的。

1 个答案:

答案 0 :(得分:2)

你的第一次尝试非常接近你想要的。只需清理一下,然后使用第二个宏根据需要多次调用它。

  • 明确参考范围。 Columns(1).NumberFormat = "General"正在活动工作表而非ws上运行。只是 在With语句中添加句点,因为您处于.Columns(1).NumberFormat = "General"语句中: Option Explicit Public Sub DateFilterAll() With Workbooks("workbook.xlsm") DateFilter .Sheets("Sheet1"), 2 DateFilter .Sheets("Sheet2"), 2 DateFilter .Sheets("Sheet3"), 2 DateFilter .Sheets("Sheet4"), 3 End With End Sub Public Sub DateFilter(ws As Worksheet, iCol As Integer) 'Initialize variables Dim rng As Range Dim lngLastRow As Long Dim lngCurrRow As Long Dim vDates() As Variant With ws 'Find the last row with contents lngLastRow = .Cells(Rows.Count, 1).End(xlUp).Row 'get the values from column iCol Set rng = .Range(.Cells(2, iCol), .Cells(lngLastRow, iCol)) vDates = rng.Value 'Loop through the values from column iCol and convert to date For lngCurrRow = 1 To lngLastRow - 1 vDates(lngCurrRow, 1) = CDate(vDates(lngCurrRow, 1)) Next lngCurrRow 'output the date values rng = vDates 'Make all cells in column A "General" input .Columns(1).NumberFormat = "General" ' Set the autofilter to display all dates other than yesterdays .Columns(iCol).AutoFilter Field:=1, Criteria1:="<>" & CLng(DateAdd("d", -1, Date)) End With End Sub
  • 对大范围的数据进行操作一次一个单元格更多 如果您将数据作为变量数组提取,请执行以下操作 操作,然后将结果作为数组输出。

这是我的抨击:

from functools import reduce
from operator import mul
from time import sleep

def a():
    print("I am A")

def b():
    print("I am B")

def c():
    print("I am C")

my_dict ={a:5,b:7,c:9}

numerator = reduce(mul, my_dict.values(), 1)
while True:
    for counter in range(numerator):
        for k, v in my_dict.items():
            if counter%v == 0:
                k()
        sleep(1)